Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bools & Ifs.

Similar presentations


Presentation on theme: "Bools & Ifs."— Presentation transcript:

1 Bools & Ifs

2 Control Structures Normal execution is sequential Control structures
Selection (branching): making a choice Repetition (iteration): looping

3 Relational Operators Relational operators :
Binary operators – compare two values Evaluate to true or false

4 Examples Samples: Notes: 8 < 15 evaluates to true
6 != 6 evaluates to false 2.5 > 5.8 evaluates to false 5.9 <= 7.5 evaluates to true Notes: ! means "not" < or > always before = (no => operator)

5 True/False C++ bool type Two values: true (1) & false (0)
bool enrolled = true; //same as = 1 cout << enrolled;

6 True/False C has no bool C++ converts numbers to bools in same way:
Uses integers : 0 = false, all others = true C++ converts numbers to bools in same way: bool enrolled = 3.2; //true bool enrolled = 23; //true bool enrolled = -4; //true bool enrolled = 2 - 2; //false

7 Relational Evaluation
Can store result of relational operator: bool result = 5 < 9; cout << result; Outputs 1 (true) bool result = 5 == 9; cout << result; Outputs 0 (false)

8 Relational Operators Conditional statements: only executed if certain conditions are met if( score is greater than or equal to 90 ) print "Grade is A"

9 One-Way Selection One-way selection syntax:
Expression true  execute statement Expression false  skip statement

10 If Example Indent statement! Example: int grade; //read grade
if(grade < 60) cout << "F"; Indent statement!

11 Evil Error 1 if(grade < 60); cout << "F"; Always prints F
If grade < 60 do empty statement (;) No matter what, print F

12 Evil Error 2 if (x = 5) cout << "Is 5" << endl;

13 Evil Error 2 if (x = 5) cout << "Is 5" << endl;
Changes x to 5 Value of assignment is 5  true Linux Hack Attempt: if ((options == (__WCLONE|__WALL)) && (current->uid = 0))

14 Evil Error 2 Yoda style (constant first) prevents this error: if (5 == x) cout << "Is 5" << endl;

15 Two-Way Selection Two-way selection syntax:
Expression true  execute statement1 Expression false  execute statement2 Only 1 of the 2 statements executes

16 If Example Example: int grade; //read grade if(grade < 60)
cout << "F"; else cout << "Pass";

17 Compound (Block of) Statements
Compound statement (block of statements): Surrounded by { } Acts as one statement

18 If Example Indent statements! Example:
int grade; double credits; //read grade if(grade < 60) cout << "F"; else { credits += 4; cout << "Pass"; } Indent statements!

19 Bad Block Example: Need { } to group instructions:
if(grade < 60) credits += 0; cout << "F"; //always executes Need { } to group instructions: if(grade < 60) { credits += 0; cout << "F"; }

20 Block Guides When in doubt, use { } Same line { prevents accidental ;
if(grade < 60) { cout << "F"; //this is fine } Same line { prevents accidental ; if(grade < 60); { cout << "F"; //always happens }


Download ppt "Bools & Ifs."

Similar presentations


Ads by Google