Control Structures, If..else, switch..case.pptx

doncreiz1 14 views 21 slides Apr 27, 2024
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

Control Structures, If..else, switch..case


Slide Content

Algorithms and programming basics Boolean operations Control structures i f..else switch..case

Binary logic b ool variable 1/0 – variable keep one of two variants It interpreter like true or false Binary logic is a statements, that gives one result – true or false (0 or 1) 14>2 = 1(true) Example: i nt x = 12; x == 12 Output: 1

Equality and relational operators equality opera tor or rela tional opera tor C++ equality or rela tional Opera tor Exa mple of C++ condition Meaning of C++ condition Relational operators > > x > y x is greater than y < < x < y x is less than y  >= x >= y x is greater than or equal to y  <= x <= y x is less than or equal to y Equality operators = == x == y x is equal to y  != x != y x is not equal to y

Logical Operators Used as conditions in loops, if statements && (logical AND ) ▫ true if both conditions are true if ( gender == 1 && age >= 65 ) ++seniorFemales; || (logical OR ) ▫ true if either of condition is true i f ( se m e st er A v e ra g e > = 9 | | f in al E x a m > = 9 ) cout << "Student grade is A" << endl;

Logical Operators ! (logical NOT , logical negation) ▫ Returns true when its condition is false , & vice versa if ( !( grade == sentinelValue ) ) cout << "The next grade is " << grade << endl; Alternative: if ( grade != sentinelValue ) cout << "The next grade is " << grade << endl;

Control Structures Sequential execution ▫ Statements executed in order Transfer of control ▫ Next statement executed not next one in sequence 3 control structures ▫ Sequence structure Programs executed sequentially by default ▫ Selection structures if , if/else , switch ▫ Repetition structures while , do/while , for

if Selection Structure Selection structure ▫ Choose among alternative courses of action ▫ Pseudocode example: ▫ If the condition is true Print statement executed, program continues to next statement ▫ If the condition is false Print statement ignored, program continues ▫ Indenting makes programs easier to read C++ ignores whitespace characters (tabs, spaces, etc.)

if Selection Structure i f (logical statement) Action1; Else Action2;

if Selection Structure Translation into C++ If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cou t < < "Passed"; Diamond symbol (decision symbol) ▫ Indicates decision is to be made ▫ Contains an expression that can be true or false Test condition, follow path if structure ▫ Single-entry/single-exit

if/else structure “if” structure ▫ Make decision based on truth or falsity of condition ▫ If condition met – body is executed, else - body is not executed ▫ If there is an “else” structure Then if condition is false the body of “else” structure is executed

if/else structure if (x > 10){ cout << "x is more than ten"; y = x * 2; } else cout << "x is smaller"; If there is more than one statements or expressions in “if/else” structure use curly brackets { }

if/else Selection Structure if ▫ Performs action if condition true if/else ▫ Different actions if conditions true or false Pseudocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” C++ code if ( grade >= 50 ) c o u t < < " P a s s ed "; else cout << "Failed";

if/else Selection Structure Ternary conditional operator ( ?: ) ▫ Three arguments (condition, value if true , value if false ) Code could be written: cout << ( grade >= 60 ? “Passed” : “Failed” ); true false print “Failed” print “Passed” grade >= 60 Condition Value if true Value if false

if/else Selection Structure Nested if/else structures ▫ One inside another, test for multiple cases ▫ Once condition met, other statements skipped if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”

if/else Selection Structure

if/else Selection Structure if (logical statement){ Action1; Action2; Action3; } Else Action4 ; if (logical statement){ Action1; Action2; } Else{ Action3; Action4 ; }

if/else Selection Structure Compound statement ▫ Set of statements within a pair of braces if ( grade >= 50 ) c o u t < < "Pas s ed . \ n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } ▫ Without braces, cout << "You must take this course again.\n"; always executed Block ▫ Set of statements within braces

Switch structure Used for testing variable for multiple values For each value the case keyword is used If no cases matched then optional default is used switch (){ ▫ case value1: statements; break; ▫ case value2: statements; break; ▫ …… ▫ default: statements; break; }

Switch and nested if else Switch structure is the same as nested if/else structure that uses only equality comparison

Switch diagram

52 Thanks for your attention!