1 / 26

ECE 1305 Introduction to Engineering and Computer Programming

ECE 1305 Introduction to Engineering and Computer Programming. Section 11 IF – ELSE Structures. Statement 1. Condition ?. Conditional Statement. Statement 2. Simple IF Structure. Yes. No. IF Statement. The parentheses ( ) operator returns a Boolean value of “true” or “false.”. { .

Télécharger la présentation

ECE 1305 Introduction to Engineering and Computer Programming

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. ECE 1305Introduction to Engineering and Computer Programming Section 11IF – ELSE Structures

  2. Statement 1 Condition ? Conditional Statement Statement 2 Simple IF Structure Yes No

  3. IF Statement The parentheses ( ) operator returns a Boolean value of “true” or “false.” { . . statement 1 ; if (condition) conditional statement ; statement 2 ; . . }

  4. IF Statement If the condition is true, the conditional statement is executed. { . . statement 1 ; if (condition) conditional statement ; statement 2 ; . . }

  5. IF Statement If the condition is false, the conditional statement is not executed { . . statement 1 ; if (condition) conditional statement ; statement 2 ; . . }

  6. IF Statement The program then continues to execute the following statements. { . . statement 1 ; if (condition) conditional statement ; statement 2 ; . . }

  7. Conditions • The condition operator ( ) typically compares the numerical value of two things. • Two numbers (integer or floating point) may be compared. 10 < 50 • Two letters of the alphabet may be compared. 'D' < 'G'

  8. Statement 1 Condition ? Conditional Statement 1 Conditional Statement 2 Statement 2 Simple IF – Else Structure Yes No

  9. IF – Else Statement The parentheses ( ) operator returns a Boolean value of “true” or “false.” { . statement 1 ; if (condition) conditional statement 1 ; else conditional statement 2 ; statement 2 ; . }

  10. IF – Else Statement If the condition is true, conditional statement 1 is executed and conditional statement 2 is not executed. { . statement 1 ; if (condition) conditional statement 1 ; else conditional statement 2 ; statement 2 ; . }

  11. IF – Else Statement If the condition is false, conditional statement 1 is not executed and conditional statement 2 is executed. { . statement 1 ; if (condition) conditional statement 1 ; else conditional statement 2 ; statement 2 ; . }

  12. IF – Else Statement The program then continues to execute the following statements. { . statement 1 ; if (condition) conditional statement 1 ; else conditional statement 2 ; statement 2 ; . }

  13. Statement 1 Condition1? Condition2? Conditional Statement 1 Conditional Statement 2 Conditional Statement 3 Statement 2 Nested IF – Else Structure Yes No No Yes

  14. IF – Else Statement If the condition1 is true, conditional statement 1 is executed. The program then skips beyond the else statement. { statement 1 ; if (condition1) conditional statement 1 ; else if (condition2) conditional statement 2 ; else conditional statement 3 ; statement 2 ; }

  15. IF – Else Statement If the condition1 is false, conditional statement 1 is not executed. If the condition2 is true, conditional statement 2 is executed. The program then skips beyond the else statement. { statement 1 ; if (condition1) conditional statement 1 ; else if (condition2) conditional statement 2 ; else conditional statement 3 ; statement 2 ; }

  16. IF – Else Statement If the condition1 and condition2 are false, conditional statements 1 & 2 are not executed. Conditional statement 3 is executed. { statement 1 ; if (condition1) conditional statement 1 ; else if (condition2) conditional statement 2 ; else conditional statement 3 ; statement 2 ; }

  17. Braces in IF – Else Statements {statement 1 ; if (condition1) { conditional statements 1 ; } else { conditional statements 2 ; } statement 2 ; }

  18. if (testScore < 60) grade = 'F'; if (testScore < 70) grade = 'D'; if (testScore < 80) grade = 'C'; if (testScore < 90) grade = 'B'; if (testScore < 100) grade = 'A'; if (testScore < 60) grade = 'F'; else if (testScore < 70) grade = 'D'; else if (testScore < 80) grade = 'C'; else if (testScore < 90) grade = 'B'; else if (testScore < 100) grade = 'A'; IF – ELSE Statements What is the result of each if testScore = 75? What is the result of each if testScore = 100?

  19. if (testScore < 60) grade = 'F'; else if (testScore < 70) grade = 'D'; else if (testScore < 80) grade = 'C'; else if (testScore < 90) grade = 'B'; else if (testScore <= 100) grade = 'A'; else cout << testScore << " is invalid "; IF – ELSE Statements

  20. Example Program

  21. Output Screen

  22. Output Screen

  23. Output Screen

  24. Output Screen

  25. Output Screen

  26. Output Screen

More Related