1 / 14

Chapter 5

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. The simple if statement.

rhoda
Download Presentation

Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. Compound Statements • To execute multiple statements (block of statements) • Syntax : if (conditions) { statement 1; statement 2; … … … … … … statement n; } Professor Jodi Neely-Ritz CGS3460

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

More Related