Download presentation
Presentation is loading. Please wait.
1
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo
2
Lecture Review if statement Provides the means of choosing Between two or more execution paths in a program E.g. In ATM, if user key in correct password? correct: allow user to cash-out money not correct: force user to quit
3
Lecture Review Syntax 1. if (condition) {action;} 2. if (condition) {action_1;} else {action_2;} 3. if (condition_1) {action_1;} else if (condition_2) {action_2;} else if (condition_3) {action_3;} else {action_4;}
4
Lecture Review Example 1. if ( exam mark is larger than 49 ) { exam passed ;} 2. if ( exam mark is larger than 49 ) { exam passed ;} else { retake exam ;}
5
Lecture Review Example 3. if ( exam mark is larger than 89 ) { A grade ;} else if ( exam mark is larger than 69 ) { B grade ;} else if ( exam mark is larger than 59 ) { C grade ;} else if ( exam mark is larger than 49 ) { D grade ;} else { F grade ;}
6
Lecture Review Relational Operators == is equal to < is smaller than <= is smaller than or equal to > is larger than >= is larger than or equal to != not equal to E.g. (10 > 8) True (8 == 6) False (8 <= 8) True
7
Lecture Review Example 3. if (exam_mark > 89) {cout<<“A grade”;} else if (exam_mark >= 70) {cout<<“B grade”;} else if (59 < exam_mark) {cout<<“C grade”;} else if (50 <= exam_mark) {cout<<“D grade”;} else {cout<<“F grade”;}
8
Lecture Review Boolean Operators && and || or ! Not E.g. if (is_102TA && is_girl) {cout <<“Hello Jackie!”<<endl;} if (is_102TA || is_helpYou) {cout <<“You are so nice, Thank you!”<<endl;
9
Leeture Review Nested if Statement if statement is inside another if if (condition_1) { if (condition_2) { if (condition_3) { action_1; } action_2a; } else { action_2b; } action_3; }
10
Summary By the end of this lab, you should be able to: Implement if statement Set condition with Relational operators Boolean operators Use shortcut assignment
11
Lab3 Use if-statement cout<<“…”; cin>>greater_than_18; if (greater_than_18 == ‘y’ || greater_than_18 == ‘Y’) { cout<<“…”; cin>>name; //… input height and weight BMI = /*…*/; cout<<“Dear ”<<name<<“,”<<endl; //… output the 1 st paragraph of advice letter //use if-statement to give different advice according to BMI /* … */ //… output the rest of the advice letter } else cout<<“…”<<endl;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.