Control Structures Flow of Control: Flow of control is implemented with three basic types of control structures: Sequential : default mode. Sequential execution of code statements (one line after another) Selection : used for decisions, branching. choosing between 2 or more alternative paths. In C, these are the types of selection statements: if if/else Switch Repetition : used for looping, i.e. repeating a piece of code multiple times in a row. In C, there are three types of loops: while do/while For The function construct, itself, forms another way to affect flow of control through a whole program.
Control Structures True and False: Selection and repetition statements typically involve decision steps. These steps rely on conditions that are evaluated as true or false. Important : ANY C expression that evaluates to a value (i.e. any R-value) can be interpreted as a true/false condition. The rule is: If an expression evaluates to 0, its truth value is false If an expression evaluates to non-zero, its truth value is true Short Circuit Evaluation: The && and || operators also have a feature known as short-circuit evaluation . In the Boolean AND expression (X && Y), if X is false, there is no need to evaluate Y (so the evaluation stops). Example: Let a = 0, b=20 then (a!=0 && b > 5) Similarly, for the Boolean OR operation (X || Y), if the first part is true, the whole thing is true, so there is no need to continue the evaluation. The computer only evaluates as much of the expression as it needs. This can allow the programmer to write faster executing code.
Selection Statement The if/else Selection Statement The most common selection statement is the if/else statement. Basic syntax: if ( expression ) statement else statement The else clause is optional, so this format is also legal: if ( expression ) statement The expression part can be any expression that evaluates a value (an R-value), and it must be enclosed in parintheses ( ). The statement parts are the "bodies" of the if-clause and the else-clause. The statement after the if or else clause must be either: an empty statement with ; a single statement expression; a compound statement (i.e. a block). Can have multiple statements enclosed in set braces { }
Flowchart of if…else Statement
Selection Statement Examples: if (grade >= 68) printf ( "Passing“); if (x == 0) printf ("Nothing here“); else printf ("There is a value“); if (y != 4) { printf ("Wrong number“); y = y * 2; counter++; } else { printf ("That's it!“); success = 1; }
Other Selection Statements Nesting if/else and else-if lader statements together: if (a > 100) if (score >= 90) { grade = ‘A’ if (b <= 0) else if (score >= 80) temp = 1; grade = ‘B’ else else if (score >= 70) temp = 0; grade = ‘C’ } else if (grade >= 60) else grade = ‘D’ x = 1; else grade = ‘F’
The Conditional Operator There is a special operator known as the conditional operator that can be used to create short expressions that work like if/else statements. test_expression ? true_expression : false_expression . How it works: The test_expression is evaluated for true/false value. This is much like the test expression of an if-statement If the test expression is true, the operator returns the true_expression If the test expression is false, the operator returns the false_expression Note that this operator takes three operands. It is the one ternary operator in the C language. Example: Let x = 5, y=6 Printf (x > y ? "x is greater than y" : "x is less than or equal to y"); // Note that this expression gives the same result as the following if (x > y) printf ( "x is greater than y“); else printf ("x is less than or equal to y“);
MCQs on if…else Structure What is the output of the below program? #include < stdio.h > int main() { int i; if ( printf ("0")) i = 3; else i = 5; printf ("%d", i); return 0; } A)3 B)5 C)0 3 D)0 5 Ans : C
MCQs on if…else Structure What is the output of the below program? #include < stdio.h > int i; int main() { if (i); else printf (" Ëlse "); return 0; } A) Blank Message B) 1 C) 0 D) Else Ans : D
MCQs on if…else Structure What is the output of the below program? #include< stdio.h > void main() { int x = 3; if(x==2); x=0; if(x==3) x++; else x+=2; printf ("x=% d",x ); } A) 3 B) 1 C) 2 D) 0 Ans : C
MCQs on if…else Structure Which combination of the integer variables x, y and z makes the variable a get the value 4 in the following expression? a = ( x > y ) ? (( x > z ) ? x : z) : (( y > z ) ? y : z ) (A) x = 3, y = 4, z = 2 (B) x = 6, y = 5, z = 3 (C) x = 6, y = 3, z = 5 (D) x = 5, y = 4, z = 5 Ans : (A)
MCQs on if…else Structure Consider the following program fragment if(a > b) if(b > c) s1; else s2; s2 will be executed if A) a<=b B ) b>c C ) b>=c and a<=b D) a>b and b<=c Ans : D
The Switch statement A switch statement is often convenient for occasions in which there are multiple cases to choose from. The syntax format is: switch ( expression ) { case constant : statements case constant : statements ... (as many case labels as needed) default: // optional label statements }
The Switch statement The switch statement evaluates the expression , and then compares it to the values in the case labels. If it finds a match, execution of code jumps to that case label. The values in case labels must be constants , and may only be integer types , which means: Only integer types, type char. Case label must be a literal or a variable declared to be const Note: You may not have case labels with regular variables, strings, floating point literals, or function calls If you want to execute code only in the case that you jump to, end the case with a break statement, otherwise execution of code will "fall through" to the next case. The default part is optional. In case no case label is matched and default part is not provided then no action will take place.
The Switch statement
The Switch statement If break is not provided, the system automatically enters into the next case block and executes the statements there. switch(2) { case 1: printf ("A"); case 2: printf ("B"); case 3: printf ("C"); break; default: printf ("D"); break; } Same case labels: switch(5) switch(65) { case 5: statementA ; { case 65: statement1; case 5: statementB ; case ‘A’: statement2; } }
The Switch statement Cannot have fraction part values. switch(5) { case 5.0: statement1; case 6: satement2; } Variables not allowed int a = 5, b = 6; switch(a) { case a: statement 1; case b: statement 2; }
The Switch statement default can be placed any where. Switch(expression) { case 11: statement 1; default: statement default; case 22: statement 2; case 33: statement 3; }
The Switch statement Nested Switch Construction: Similar to nesting of if-else, we can nest switch construction also. switch (expression1) { case labelA : switch (expression2) { case label 1: statement1; case label 2: statement2; } break; case labelB : switch (expression3) { case label X: statement3; case label Y: statement4; } break; }
The Switch statement If we want to execute a same code for different values then we can write code as follows. switch (day) { case 1: case 2: case 3: q=1; break; case 4: case 5: case 6: q=2; break; }
The Switch statement Limitations of switch: Good for equality comparisons but not for range comparisons. Works good for int type data only and not good for other types of data. Works good with constants but not with variables (as case labels).
Example Programs on Switch Structure Grade Calculation Program Link to Grade Program Menu Driven Application Link to Menu Program
MCQs on Switch statement #include< stdio.h > void main() { switch(1) { case 1: printf ("One"); case 2: printf ("Two"); default: printf ("Default"); } } A) Two B) One C) Defalut D) OneTwoDefault Ans : D
MCQs on Switch statement #include< stdio.h > void main() { switch(1) { case 1: printf ("One"); case 2: printf ("Two"); break; default: printf ("Default"); } } A) Two B) One C) Defalut D) OneTwo Ans : D
MCQs on Switch statement #include< stdio.h > void main() { switch(3) { default: printf ("Default"); case 1: printf ("One"); case 2: printf ("Two"); } } A) Two B) One C) DefalutOneTwo D) OneTwo Ans : C
MCQs on Switch statement #include< stdio.h > void main() { switch(3) { default: printf ("Default"); break; case 1: printf ("One"); case 2: printf ("Two"); } } DefaultOneTwo B)Default C) One D)Two Ans : B
MCQs on Switch statement #include < stdio.h > void main() { int i = 0; switch (i) { case '0': printf (“ Raju "); break; case '1': printf (“Ravi"); break; default: printf (“ Gopi "); } } A) Raju B) Gopi C) Ravi D) Error Ans : B ‘0’ ascii code is 48 ‘1’ ascii code is 49
MCQs on Switch statement #include < stdio.h > void main() { int i = 3; switch (i) { case 0+1: printf (“Good"); break; case 1+2: printf (“Very Good"); break; default: printf (“Excellent"); } } A) Good B) Excellent C) Very Good D) Error Ans : C
MCQs on Switch statement #include < stdio.h > #define EVEN 0 #define ODD 1 void main() { int i = 3; switch (i & 1) { case EVEN: printf ("Even"); break; case ODD: printf ("Odd"); break; default: printf ("Default"); } } A) Odd B) Even C) Defalut D) Error Ans : A
MCQs on Switch statement char inchar = 'A'; switch ( inchar ) { case 'A' : printf (" A ") ; case 'B' : printf (“ B ") ; case 'C' : case 'D' : case 'E' : default: printf (“ All") ; } A) A B All B) A C) B D) All Ans : A
MCQs on Switch statement #include < stdio.h > void main() { int i = 3; switch(i) { printf ("Outside "); case 1: printf (“Stanley"); break; case 2: printf (“Engineer"); break; default: printf (“Welcome"); } } Stanley B) Engineer C) Welcome D) Nothing is printed Ans : C //Warning: statement never be executed.
MCQs on Switch statement #include < stdio.h > int main() { char check = 'a'; switch (check) { case 'a’ || 1: printf (“A "); case 'b' || 2: printf (“B "); break; default: printf (“C"); } A) Compile time Error B) A C) B D) C Ans : A compile-time error: duplicate case value, as duplicated cases are not allowed.
MCQs on Switch statement #include < stdio.h > void main() { int check = 20, arr [] = {10, 20, 30}; switch (check) { case arr [0]: printf (“Hello "); case arr [1]: printf (“Hi"); case arr [2]: printf (“Good night"); } } Hello B) Hi C) Good night D) Error Ans : D compile-time error: case label does not reduce to an integer constant