Chapter 2 Section 2.3 if and if-else statements CS 50 Hathairat Rattanasook
if Statement An if-statement is a statement with a condition that can be evaluated to true or false. If it evaluates to true, the following statement or compound statements are executed If it evaluates to false, the following statement or compound statements are skipped if ( expression ) // condition statement with control expression statement CS 50 - Hathairat Rattanasook 1
if Statement if ( expression ) // condition statement with control expression statement A condition statement: allows a program to test a condition and then choose which code to execute next. A control expression: Its value will be interpreted as either true (nonzero) or false (zero). Statement: A statement can be a block, an executable statement, or another if statement. CS 50 - Hathairat Rattanasook 2
if Statement Example An if-statement with a statement: If number is smaller than 10, the printf(..) method call will be executed. if (number < 10) printf("number is less than 10"); An if-statement with a block: If number is smaller than 10, the printf(..) method call will be executed. if (number < 10) { printf("number is less than 10"); CS 50 - Hathairat Rattanasook 3
if-else Statement The code will execute the statement1 when the expression is true (nonzero). The code will execute the statement2 when the expression is false (zero). if ( expression ) statement 1 else statement 2 CS 50 - Hathairat Rattanasook 4
if-else Statement Example If age is more than 15, the code will print "you can drive". Otherwise, it will print "you can NOT drive". if (age > 15) printf("you can drive"); else printf("you can NOT drive); If age is more than 15, the block with "you can drive" is executed. Otherwise, the block after else is executed. if (age > 15) { printf("you can drive"); else { printf("you can NOT drive); CS 50 - Hathairat Rattanasook 5
if-else Statement Example The following code will produce a compile time error. Remember: if you don't declare a block, you can only have a single statement in the if or the else part. If you want to have multiple statements, you must declare a block. if (age > 15) printf("you can drive"); printf("drive safely"); else printf("you can NOT drive); CS 50 - Hathairat Rattanasook 6
if-else Statement Example The following code will NOT produce a compile time error. However: it is likely a logical error! The printf("you can NOT drive") of the else-part will only be executed if age > 15 evaluates to false. The printf("see you later") is ALWAYS executed because it is a regular statement that just follows the if-else statement. if (age > 15) { printf("you can drive"); printf("drive safely"); else printf("you can NOT drive); printf("see you later"); CS 50 - Hathairat Rattanasook 7
Hint A good programmer always uses { and to define a block in if and in if-else statements. A block improves code clarity and reduces the likelihood of bugs CS 50 - Hathairat Rattanasook 8
Compound if-else Statement Many times you may want to test several conditions and execute different statements depending on the result of each condition. That's what compound if-else statements are used for. if (expression 1) statement 1; else if (expression 2) statement 2; else if (expression 3) statement 3; else statement n; CS 50 - Hathairat Rattanasook 9
Nested if-else Statement It is also possible to nest if or if-else statements if (expression 1) { if (expression 2) { statement 2; else if (expression 3) { if (expression 4) { statement 3; else { statement n; CS 50 - Hathairat Rattanasook 10
Question 1. Which statement gets executed in the below code if a = 3 and b = 9? 2. Which if-statement does the else in line 4 belong to? Line 1 or line 2? 1 if (a > 5) 2 if (b < 10) 3 statement1; 4 else if (b > 10) 6 statement2; 7 else 8 statement3; CS 50 - Hathairat Rattanasook 11
Answer 1. If a = 3 and b = 9, no statement is executed (see answer 2) 2. The else in line 4 belongs to the if condition in line 2 and even else in line 8 belongs to the compound if-else in line 2. Therefore, if a is 5 or smaller, the ifcondition evaluates to false and lines 2-8 are skipped. 1 if (a > 5) 2 if (b < 10) 3 statement1; 4 else if (b > 10) 6 statement2; 7 else 8 statement3; This is a good example why declaring blocks are important! CS 50 - Hathairat Rattanasook 12
Compound if-else Example The code segment below will cause a compile time error. In case, you want a compiler to execute multiple statements, those statements should be inside a block. This code will produce a compile error for statement 1! if (expression 1) if (expression 2) statement 2; statement 1 else if (expression 3) statement 3; else statement n; if (expression 1) { if (expression 2) { statement 2; statement 1 else if (expression 3) { statement 3; else { statement n; CS 50 - Hathairat Rattanasook 13
Code Clarity Many of the last examples show that it is very important to define blocks! Defining blocks: makes the code more clear Helps others to better understand what you are trying to do Reduces compile errors and logical bugs! Helps to maintain code (eg. when you come back in a year and need to update your code) CS 50 - Hathairat Rattanasook 14