260 likes | 366 Views
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.”. { .
E N D
ECE 1305Introduction to Engineering and Computer Programming Section 11IF – 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.” { . . statement 1 ; if (condition) conditional statement ; statement 2 ; . . }
IF Statement If the condition is true, the conditional statement is executed. { . . statement 1 ; if (condition) conditional statement ; statement 2 ; . . }
IF Statement If the condition is false, the conditional statement is not executed { . . statement 1 ; if (condition) conditional statement ; statement 2 ; . . }
IF Statement The program then continues to execute the following statements. { . . statement 1 ; if (condition) conditional statement ; statement 2 ; . . }
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'
Statement 1 Condition ? Conditional Statement 1 Conditional Statement 2 Statement 2 Simple IF – Else Structure Yes No
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 ; . }
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 ; . }
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 ; . }
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 ; . }
Statement 1 Condition1? Condition2? Conditional Statement 1 Conditional Statement 2 Conditional Statement 3 Statement 2 Nested IF – Else Structure Yes No No Yes
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 ; }
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 ; }
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 ; }
Braces in IF – Else Statements {statement 1 ; if (condition1) { conditional statements 1 ; } else { conditional statements 2 ; } statement 2 ; }
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?
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