Download presentation
Presentation is loading. Please wait.
Published byMaud Atkinson Modified over 9 years ago
1
Lecture 2 Control Structure
2
Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater than <= is less than or equal to >= is greater than or equal to != is not equal to
3
Logical Operators (&&, ||, !) Used for more complex conditions than the other operators Helps combine multiple conditions –Logical AND – Condition1 && Condition2 –Logical OR – Condition1 || Condition2 Logical Negation – to reverse condition –if (!(grade == “A”)) Can be replaced with relational op (!=) –if (grade != “A”) 1
4
Control Structures Control Structures Selection Structures Looping Structures
5
Control Structures Algorithms –Procedure for solving a problem in terms of The actions to be executed The order in which actions are to be executed Pseudocode –Artificial and informal language that helps programmers develop algorithms Flowchart –Graphical representation of an algorithm
6
Control Structures Sequential Execution –Statements executed one after the other in order in which they are written Add grade to total Add 1 to counter total = total + grade; counter = counter + 1; 3
7
Control Structures Selection Structures –Used to choose among alternative courses of action –if selection structure Single selection structure –if /else selection structure Double selection structure –switch selection structure Multiple selection structure 4
8
Control Structures Repetition Structures –Used to repeat an action while a condition remains true –for repetition structure –while repetition structure –do/while repetition structure 5
9
Selection Structures if selection structure –Performs an indicated action when the condition is true. “Passed” grade >= 60 true false if (grade >= 60) cout<<“Passed”; 6
10
Statement 1 expression Statement 2 Statement 3 true false
11
Selection Structures if/else selection structure –Performs an action when the condition is true and another when the condition is false. if (grade >= 60) printf (“Passed”); else printf (“Failed”); print “Passed” grade >= 60 truefalse print “Failed” 7
12
Form and Syntax If ( ) else
13
int main() { int num, neg,pos; cout<<“Please enter the no.”; cin>> num; if (num < 0) neg= neg +1; else pos= pos +1; }
14
Conditional Operator Closely related to if/else structure ?: (Conditional Operator) Only ternary operator in C –Takes three operands Operands with conditional operator form conditional expression –Operand 1– condition –Operand 2 – action if condition true –Operand 3 – action if condition false 8
15
Conditional Operator print “Passed” grade >= 60 truefalse print “Failed” grade >= 60 ? “Passed”: “Failed” 9
16
if (Expression1 ) Statement1 else if (Expression2 ) Statement2... else if (ExpressionN ) StatementN else Statement N+1 EXACTLY 1 of these statements will be executed. Nested if statements
17
Nested if/else //nested if/else //printing grades of students if (grade >= 90) cout<<“A”; else if (grade >= 80) cout<<“B”; else if (grade >= 70) cout<<“C”; else if (grade >= 60) cout<<“D”; else cout<<“Failed”; 10
18
Nested if/else //nested if/else //printing grades of students if (grade >= 90) cout<<“A”; else if (grade >= 80) cout<<“B”; else if (grade >= 70) cout<<“C”; else if (grade >= 60) cout<<“D”; else cout<<“Failed”; If grade of a student is 84, the conditions for “B”, “C” and “D” are all true. What will be printed? 11
19
Selection Structures switch selection structure –Multiple selection structure is useful when an algorithm contains a series of decisions –Variable or expression is tested separately for one of several possible values –Each value represents a different action 12
20
Switch Selection Structure // counting grades assigned int aCount, bCount, cCount, dCount, fCount; switch (grade) { case ‘A’: case ‘a’: ++aCount; case ‘B’: case ‘b’: ++bCount; case ‘C’: case ‘c’: ++cCount; case ‘D’: case ‘d’: ++dCount; default: ++fCount; } If grade has value ‘A’, which variable is incremented? 13
21
switch Selection Structure // counting grades assigned int aCount, bCount, cCount, dCount, fCount; switch (grade) { case ‘A’: case ‘a’: ++aCount; break; case ‘B’: case ‘b’: ++bCount; break; case ‘C’: case ‘c’: ++cCount; break; case ‘D’: case ‘d’: ++dCount; break; default: ++fCount; } 14
22
break Statement break statement causes program control to move to the first statement after the selection/ repetition structure Switch cases would otherwise run together 15
23
switch Selection Structure case a action case a true false break case b action case b true break default action false 16
24
Summary of precedence order (revised) OperatorAssociativity () left to right ++ -- ! right to left * / % left to right + - left to right >= left to right == != left to right && left to right || left to right ?: left to right = += -= *= /= %= right to left, left to right 25
25
Control Structures C++ keywords –Cannot be used as identifiers or variable names
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.