Computer Science

Conditional Statement

A conditional statement in computer science is a programming construct that allows for different actions to be taken based on whether a certain condition is true or false. It typically consists of an "if" clause that specifies the condition to be evaluated, and may also include an "else" clause to define an alternative action if the condition is not met.

Written by Perlego with AI-assistance

4 Key excerpts on "Conditional Statement"

Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.
  • Coding + Math
    eBook - ePub

    Coding + Math

    Strengthen K–5 Math Skills With Computer Science

    ...Zhang and Nouri’s (2019) review found that Scratch users, by and large, utilized events in the vast majority of their projects—particularly in the more recent studies cited. They did note, however, that students did not demonstrate sufficient use of complicated events beyond the “flag clicked event” often used to start a program or a “key pressed event”. Given the extensive use of events in programming the kinds of interactive experiences computer users have come to expect, it is important for beginning programmers to gain a good understanding of how they work. Conditionals Conditional Statements are at the heart of the decision-making processes that programs must navigate. We covered one type of Conditional Statement, the While Loop, in the previous chapter, but its decision-making was limited to whether or not to continue repeating the same block of code. Many programming decisions require that a choice be made between several different options based on multiple conditions. In more complex decision trees, more powerful Conditional Statements are required. The most commonly used decision-making statement is the “if” statement (also referred to as an “if-then” statement). They are often called to respond to an event, and their structure is such that certain blocks of code are only executed under the condition that a Boolean expression evaluates to true. Table 5.1 Conditional If Structure Examples Table 5.1 depicts four examples of what the structure of if statements can look like in code. Example one is the simplest of the if statements, and it would only run the code between the curly braces if the condition evaluated to true; otherwise, the program would just continue to the next code below it. Example two runs the code below the if statement if the condition is true, but runs the code below the else statement if the condition is false...

  • Building Winning Trading Systems with Tradestation
    • George Pruitt, John R. Hill(Authors)
    • 2012(Publication Date)
    • Wiley
      (Publisher)

    ...But, remember back in Chapter 1 when we discussed the precedence of operators and how parentheses can change their order. In this example, we have True and (False or True), which reduces down to True and True. We first evaluate the information inside the parentheses and then do the next comparison. Inside the parentheses, we have False or True, which is True. We then compare True and True; and as we all know, this is True. The if-then statement is the simplest form of conditional branching. This statement causes the program to execute a single statement or a block of code if a condition is True. When the compiler first encounters an if-then statement, it evaluates the information that is provided by the Conditional Statement. The evaluation produces one of two possible results—True or False. If the statement is True, the line or block of code immediately following the if-then is executed. If the result is false, the program skips the line or block entirely. The if-then construct has the following syntax (syntax is the body of rules governing which statements and combinations of statements in a programming language will be acceptable to a compiler for that language): if(Conditional Statement) then [single statement]; if(Conditional Statement) then begin [multiple statements] end; You must always include the word “then” after the if Conditional Statement. The parentheses around the Conditional Statement(s) are optional, but they do help in clarification. If you want more than one statement to be executed after a conditional branch, you must use the words “begin” and “end.” “Begin” marks the beginning point of the block of code, and “end” literally marks the end of the block of code...

  • Hack Audio
    eBook - ePub

    Hack Audio

    An Introduction to Computer Programming and Digital Signal Processing in MATLAB

    • Eric Tarr(Author)
    • 2018(Publication Date)
    • Routledge
      (Publisher)

    ...Similar to an if statement, only certain commands are executed during the program depending on the conditions of the switch statement. A switch statement evaluates whether a variable is equal to the variable in each case. If it is TRUE, the statements in that case are executed. Otherwise, the program moves on to testing the next case. Examples: Save and run the following m-file in MATLAB. % This script is an example of using a "switch" statement % as a conditional statement a = 5; % Variable for switch cases switch a case -5 % "a" does not equal -5 b = 1 % Skip this case case 5 % "a" does equal 5 b = 2 % Execute this case end Example 5.8: switch Conditional Statement Comparison of if and switch Statements The if and switch Conditional Statements can be used to accomplish identical flow in a computer program. Therefore, in most cases it is the preference of the programmer to decide which type of statement to use. As a matter of best practice, a programmer should use the Conditional Statement that allows for the easiest interpretation of the written code. 5.3.2 Loops One task computers excel at performing is the repetition of instructions. As a programmer, if you want to repeat a command multiple times, you could copy and paste the command manually. Another option is to create a loop. A loop repeats the included commands until certain requirements are met. Requirements are defined in the command used to create the loop, also called the loop declaration. for Statement One type of loop is declared in MATLAB using the for statement. Following the for keyword, an expression is needed to define a counting variable to set the number of times the loop repeats. This variable can be referenced during the repetitions of the loop...

  • PLC Controls with Structured Text (ST)
    eBook - ePub

    PLC Controls with Structured Text (ST)

    IEC 61131-3 and best practice ST programming

    ...9 Conditional Statement The following chapter consists of the central statement concepts in ST. In the general format descriptions, <Condition> and <Statement> must be replaced by variables, expressions and PLC codes 9.1 IF-THEN-ELSE An IF-THEN-ELSE statement – or a sentence – is the most used expression in ST programming. An IF statement can e.g. be used to control whether a digital sensor shows a signal (e.g. an electrical start contact, an ON/OFF switch or a level contact in a pump well). If the digital sensor shows a signal, an action is to be taken, being e.g. starting a pump or switch on a lamp. An IF statement can be used both when using analogue and digital input-signals. Further, the IF statement can be used for internal variables. The general format of the IF statement is as follows: IF <Condition> THEN <Statement> END_IF ; Where: <Statement> = Can contain one or more lines of PLC code, always ended by END_IF and semicolon. <Condition> = An expression, always being TRUE or FALSE. If the expression is true, the PLC code in <Statement> is carried out. <Condition> can e.g. be an input signal from an electrical contact and <Statement> can be an output signal to switching on or off a lamp. The statement ELSE can be added to the expression: IF <Condition> THEN <Statement> ELSE <Statement1> END_IF ; As can be seen, the ELSE part is optional and notice that the lines including <Statement> is tabulated (2 x blanks) to make the whole expression more readable. The mode of operation is as follows: If <condition> is fulfilled (TRUE), the PLC code in <Statement > will be carried out. If <condition> is not fulfilled (FALSE), the PLC code in <Statement1> is carried out. Flowchart of the IF-ELSE statement NOTICE: If <Condition> section contains colon “:”, it indicates that it is controlled if the variable assignment is carried out well, which is not the intention! Therefore, remember that the sign “=” must stand alone. It is...