Flow control and Conditions : Overview : Control flow (when referring to computer programming) is the order function calls, instructions, and statements are executed or evaluated when a program is running. Many programming languages have what are called control flow statements; used to determine what section of code is run in a program at a given time. An example of a control flow statement is an if-else statement which executes a particular set of statements in the if-clause if the condition provided in it is true. If false, the statements following the else-clause get executed. Similarly the available control flow statements are switch-case, nested if-else, goto statements and the conditional operator which can be used to replace the if-else statement. Description : C language generally provides two types of flow control depending on the number of times the statements in a program are to be executed : Branching Looping Branching : Branching is deciding what actions to take and the statements are generally executed only once, depending on the condition. Branching is so called because the program chooses to follow one branch or another. This can be carried out using the if, if-else, switch-case, goto statements and the conditional operator. 8.1 If statement : This is the most simple form of the branching statements. It takes an expression in parenthesis and an statement or block of statements. If the expression is true then the statement or block of statements gets executed otherwise these statements are skipped. Expression will be assumed to be true if its evaluated values is non-zero. Syntax : if(condition) { /* statement(s) to be executed if the condition is true */ } If the condition evaluates to true, then the block of code inside the if statement will be executed. If condition evaluates to false, then the first set of code after the end of the if statement(after the closing curly brace) will be executed. SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org) Figure 8.1 The above figure 8.1 shows the execution of a given if statement which prints the string 'Hello World' if the condition provided is true. The value of the variable 'a' is checked if it is equal to zero and if it is true, then the string provided inside the printf() function is printed as the output. Figure 8.2 The above figure 8.2 shows the execution of a given if statement which prints the string 'Hello World' if the SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org) condition provided is true. The value of the variable 'a' here is equal to one and hence the condition becomes false. Since there are no other statements to be executed below the if-statement, no output is printed on the screen and displayed to the user. 8.2 If-else statement : An if-else statement in programming is a conditional statement that runs a different set of statements depending on whether an expression is true or false. An if statement can be followed by an optional else statement, which executes when the condition provided in the if clause is false. C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value. Syntax : if(condition) { /* statement(s) to be executed if the condition is true */ } else { /* statement(s) to be executed if the condition is false */ } If the condition evaluates to true, then the if block of code will be executed, otherwise the statements in the else block of code will be executed. Figure 8.3 SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org) The above figure 8.3 shows the execution of a given if…else statement which prints the string 'Hello World' and 'Test' if the condition provided is true, if not, the statement provided in the else block is executed. The value of the variable 'a' is checked if it is equal to zero and if it is true, then the strings 'Hello World' and 'Test' are printed as the output. Figure 8.4 The above figure 8.4 shows the execution of a given if…else statement which prints the string 'Hello World' and 'Test' if the condition provided is true, if not, the statement provided in the else block is executed. The if statement contains the value '1' which is non-zero and hence the condition is evaluated to true. Hence the strings 'Hello World' and 'Test' are printed as the output. SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org) Figure 8.5 The above figure 8.5 shows the execution of a given if…else statement which prints the string 'Hello World' and 'Test' if the condition provided is true, if not, the statement provided in the else block is executed. The if statement checks two conditions in this case, whether the value of a is equal to 1 and the value of b is equal to 10. The statements inside the if block are executed only if both the conditions become true. Figure 8.6 SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org) The above figure 8.6 shows the execution of a given if…else statement which prints the string 'Hello World' and 'Test' if the condition provided is true, if not, the statement provided in the else block is executed. The if statement checks two conditions in this case, whether the value of a is equal to 1 and the value of b is equal to 11. Here, since one of the conditions is evaluated to false, the statement in the else block gets executed. Figure 8.7 The above figure 8.7 shows the execution of a nested if…else statement which prints the string 'Hello World' and 'Test' if the condition provided is true, if not, the condition in the next else if block is checked. And if this also proves to be false, the statements in the else block gets executed. The if statement checks two conditions in this case, whether the value of a is equal to 1 and the value of b is equal to 10. Here, since one of the conditions is evaluated to false, the control shifts to the else-if block. In this case, the condition is evaluated to be true and hence the statement 'a is 100' is printed. Consider the given example : #include "stdafx.h" int func() { return 0; } int _tmain(int argc, _TCHAR* argv[]) { if( !func()) { printf("Function not working"); SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org) } } Here, the given value returned from the function is evaluated inside the conditional part of the if statement where the value of 0 becomes !0 which is 1. Hence the given condition becomes true and the statement 'Function not working' is printed on the output screen. 8.3 Switch-case statement: It is commonly seen in applications that the value of a variable is successively compared against different values. It becomes cumbersome to write a number of if and else if statements and readability of a program reduces. A more elegant way to handle this is by using the switch statement. A switch statement allows a variable to be tested for equality against a list of values. Syntax : switch (expression1) { case constant-expression1 : statements; break; case constant-expression2 : statements; break; default : statements; } If a condition is met in switch case, then execution continues on into the next case clause also if it is not explicitly specified that the execution should exit the switch statement. This is achieved by using break keyword. If none of the listed conditions are met then default condition is executed. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org) Figure 8.8 In the above figure 8.8, the expression 'a' given inside the switch case is compared against values 0 and 1 till a match is found. Then the corresponding program statements are executed. Every set of statements needs a break statement otherwise the program execution will continue into the next case statement that satisfies the expression. There is a special default statement at the end which gets executed when the no match for the cases is found. Figure 8.9 In the above figure 8.9, the expression 'a' given inside the switch case is compared against values 0 and 1 till a SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org) match is found. Then the corresponding program statements are executed. Here, it matches with case 1 but since there is not break statement to stop the execution, it continues on to the default case and prints the statement present inside that block as well. 8.4 Goto statement : C language includes the less used goto statement. Same as the break and continue statements, generally it is recommended to avoid using this statement. A goto statement causes a branch to be made to a specified point in a program. This point is denoted by a label which is a name followed by a colon. A label is formed with the same rules as variable names. This can be located anywhere in a program, either before or after the goto statement. However, using too many goto statements in a program reduces its readability and the program becomes difficult to debug or maintain. Hence the if-else statement is recommended in such cases. Syntax : goto Label_Name; Statements; Label_Name: statements; Figure 8.10 The above figure 8.12 shows the working of the Goto statement in the given program. The statement goto Label2 shifts the flow of the program to execute the statements which follow Label2 which then ends with transferring control back to Label1. This ends in an infinite loop being executed. Hence it is generally not SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org) advisable to work with goto statements as they decrease efficiency. Figure 8.11 The above figure 8.11 shows the working of the Goto statement in the given program. The statement goto Label2; shifts the flow of the program and the statements which follow the Label1 do not get executed. Only statements following Label2 get printed as the output. 8.5 Conditional Operator : The ? : operator is also used to control the flow of a program and works similar to an if - else statement, except that because it is an operator it can only be used within expressions. It is a ternary operator and hence takes three values/expressions and is the only ternary operator in C. Syntax : variable_name = (condition) ? value_if_true : value_if_false; SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org) Figure 8.12 The given code in figure 8.10 shows the working of the conditional operator. This operator checks whether the value of a is zero. If the condition is true, then the statement 'a is 0' is printed, if not, the statement 'a is something else' is printed as the output.Here, since the given condition is false, the statement 'a is something else' is printed as the output. SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)