Upload
maud-atkinson
View
214
Download
0
Embed Size (px)
Citation preview
Lecture 2
Control Structure
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
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
Control Structures
Control Structures• Selection Structures• Looping Structures
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
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
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
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
Selection Structures
• if selection structure– Performs an indicated action when the
condition is true.
“Passed”grade >= 60true
false
if (grade >= 60)cout<<“Passed”;
6
Statement 1
expression Statement 2
Statement 3
true
false
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”);
elseprintf (“Failed”);
print “Passed”grade >= 60truefalse
print “Failed”
7
Form and Syntax
If (<Expression>)
<statement>
else
<statement>
int main()
{
int num, neg ,pos;
cout<<“Please enter the no.”;
cin>> num;
if (num < 0)
neg= neg +1;
else
pos= pos +1;
}
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
Conditional Operator
print “Passed”grade >= 60truefalse
print “Failed”
grade >= 60 ? “Passed”: “Failed”
9
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
Nested if/else//nested if/else//printing grades of students
if (grade >= 90)cout<<“A”;
elseif (grade >= 80)
cout<<“B”;else
if (grade >= 70)cout<<“C”;
elseif (grade >= 60)
cout<<“D”;else
cout<<“Failed”;
10
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”;
elsecout<<“Failed”;
If grade of a student is 84, the conditionsfor “B”, “C” and “D”are all true. What will be printed?
11
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
Switch Selection Structure
// counting grades assignedint 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
switch Selection Structure// counting grades assignedint 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
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
switch Selection Structure
case a actioncase atrue
false
break
case b actioncase btrue
break
default action
false
16
Summary of precedence order (revised)
Operator Associativity() 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
Control Structures
• C++ keywords– Cannot be used as identifiers or variable namesC++ Keywords
Keywords common to the C and C++ programming languages
auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while C++ only keywords
asm bool catch class const_cast delete dynamic_cast explicit false friend inline mutable namespace new operator private protected public reinterpret_cast static_cast template this throw true try typeid typename using virtual wchar_t