140 likes | 243 Views
This chapter focuses on understanding conditional statements in programming, including relational and logical operators, the simple and compound if statements, else clauses, nested if statements, cascaded if statements, and the switch statement. It explains the syntax and functionality of each, providing examples for clarity. Additionally, it discusses the importance of the break clause within switch statements to control the flow of execution. This guide is essential for mastering control transfer in programming, facilitating better decision-making in code.
E N D
Chapter 5 • See chapter 4 notes for the following topics: • Relational operators • Logical operators & expressions The if statement …conditional statement for control transfer The switch statement …multiple-conditional statement for control transfer Professor Jodi Neely-Ritz CGS3460
The simple if statement • Allows a program to pick one of two alternatives… • The simplest form of the if statement has the syntax if (conditions) statement; • Examples : if ( a > 0 ) a = a + 1; if ( b ! = 2 ) b--; if ( ( a == 1) || (b == 0 ) ) i = i + 2; Professor Jodi Neely-Ritz CGS3460
Compound Statements • To execute multiple statements (block of statements) • Syntax : if (conditions) { statement 1; statement 2; … … … … … … statement n; } Professor Jodi Neely-Ritz CGS3460
Compound Statements – an example i = 5; if ( ( a > b ) && ( a ! = 0 ) ) { a = a + 1; printf(“The value of ‘a’ has been incremented… \n”); b = b – 1; printf(“The value of ‘b’ has been decremented… \n”); i = i + 1; k = k + 3; } printf(“The value of i is %d\n”,i); a = 5, b = 3 i = ?? a = 0, b = -2 i = ?? a = 2, b = 7 i = ?? Professor Jodi Neely-Ritz CGS3460
The else clause • This clause is used to provide an alternate segment of code • Syntax of the if… else…statement if (conditions) { statement 1; statement 2; } else { statement 3; statement 4; } Professor Jodi Neely-Ritz CGS3460
The else clause – an example if ((a >= b) && (b >= c)) { printf(“the value of a = %d\n”,a); printf(“executed if clause\n”); i = i + 1; } else { printf(“executed else clause”); k = k - 4; } Professor Jodi Neely-Ritz CGS3460
Nested If statements Any level of nesting is allowed with the if… else… statement if (conditions 1) { if (conditions 2) statement 2; else statement 3; } else { if (conditions 3) { statement 3; statement 4; } } Professor Jodi Neely-Ritz CGS3460
Cascaded If statements • Provision to check multiple conditions, providing alternatives for each of the cases… if (conditions 1) { segment 1 } else if (conditions 2) { segment 2 } else if (conditions 3) { segment 3 } … … else { default segment } Professor Jodi Neely-Ritz CGS3460
Dangling else… if (conditions 1) if (conditions 2) statement 1; else statement 2; • Here, the else corresponds to the second if statement though it appears to correspond to the first if statement • The compiler uses such a dangling (ambiguous) else to correspond to the last unbalanced if statement if (conditions 1) { if (conditions 2) statement 1; } else statement 2; /*remedied program*/ Professor Jodi Neely-Ritz CGS3460
Conditional operators if ( a > b) i = i + 1; else i = i – 1; The above segment can be written in this format : ( a > b ) ? i = i + 1 : i = i – 1 (or) i = (a > b) ? i + 1 : i – 1 Professor Jodi Neely-Ritz CGS3460
Boolean values • TRUE and FALSE are defined by most compilers as 1 and 0 • While checking a condition, the if statement checks to see if the condition is “logically true” int lock; … … if ( lock == TRUE ) statement 1; else statement 2; Professor Jodi Neely-Ritz CGS3460
The switch statement • This statement is used where there may be multiple input values for a single condition and different output segments correspond to each individual case int i; … … switch ( i ) { case 1 : statement 1; break; case 2 : statement 2; break; … … … … … … … … … … … … … … … … … … … … default : statement; break; } Professor Jodi Neely-Ritz CGS3460
The switch statement – an example int month; … … switch ( month ) { case 1 : printf(“Jan\n”); break; case 2 : printf(“Feb\n”); break; … … … … … … … … … … … … … … … … … … … … case 12 : printf(“Dec\n”); break; default : printf(“Error : Invalid month no.\n”); } Professor Jodi Neely-Ritz CGS3460
The break clause in the switch statement • The break clause in used in the switch statement to exit the normal control of that segment and exit the entire switch construct • It returns control to the next line after the switch statement • Each case keyword is merely a label to which control jumps • Condition checking is done when the compiler sees the switch statement and control is passed to the corresponding case • Without the break clause, control will ‘fall through into’ other sections that follow that required section Professor Jodi Neely-Ritz CGS3460