View
217
Download
6
Embed Size (px)
Citation preview
1
Engineering Problem Solving With C++
An Object Based ApproachChapter 3
Control Structures
2
Structured Programming
• Sequence
• Selection
• Repetition
yesno
yes
no
3
Boolean Expressions
4
Relational Operators
== equality != non equality < less than > greater than <= less than equal to >= greater than equal to
5
Logical Operators
! not && and || or
6
Operator Precedence
1. < <= > >= (relational operators)
2. == != (logical operators)
3. && (…)
4. || (…)
7
(-6<0)&&(12>=10)
(3.0 >= 2.0) || (3.0 >= 4.0)
(3.0 >= 2.0) && (3.0 >= 4.0)
true && true results in true
Practice! - evaluate
true || false results in true
true && false results in false
8
Selection Statements
9
Selection Statements
• if
• if else
• switch
10
The if statement
if(expression)statement; //single statement executed
//if expression is true
if(expression){ //statements inside {} are
//executed if expression is truestatement1;statement2;…statement n;
}
11
The if statement - examples
if (x>0)k++;
if(x>0) {
x=sqrt(x);k++;
}
12
The if - else statement
if(expression)statement;
elsestatement;
if(expression) {
statement block }else {
statement block }
13
The nested if-else
if(x > y)if(y < z)
k++;else
m++;else
j++;
14
Practice!
int x=9, y=7, z=2, k=0, m=0, j=0;if(x > y)
if(y >z && y>k)k++;
elsem++;
elsej++;
What are the values of j, k and m?
j is 0, k is 1, m is 0
15
The switch statement
switch(expression){
case constant:statement(s);break;
case constant:statement(s);break;
/* default is optional*/default:
statement(s);}
16
The switch statement
• Expression must be of type integer or character
• The keyword case must be followed by a constant
• break statement is required unless you want all subsequent statements to be executed.
17
switch statement examplechar ch;int ecount=0, vowels=0, other=0;cin.get(ch);while(!cin.eof()){ switch(ch) { case ‘e’:
ecount++; case ‘a’: case ‘i’:case ‘o’: case ‘u’:
vowels++;break;
default:other++;
}//end switch cin.get(ch);}//end whilecout << ecount << “ e’s, ” << vowels << “ vowels, and ” << other << “ other characters. “ << endl;
18
Practice!
Convert these nested if/else statements to a switch statement:if (rank==1 || rank==2) cout << "Lower division \n";else{ if (rank==3 || rank==4) cout << "Upper division \n"; else { if (rank==5) cout << "Graduate student \n"; else cout << "Invalid rank \n"; }}
19
Practice Solution!
switch(rank)
{ case 1: case 2: cout << "Lower division \n"; break; case 3: case 4: cout << "Upper division \n"; break; case 5: cout << "Graduate student \n"; break; default: cout << "Invalid rank \n"; }//end switch
20
Repetition Statements
21
The while statement
while (condition)
statement;
while (condition)
{
statement block
}
yes
no
22
The do/while statement
do
statement;
while (condition)
do
{
statement block
} while (condition)yes
no
23
Practice!
#include <iostream>using namespace std;int main(){
int n=4;while(n>0){
cout << n << endl;n--;
}cout << “value of n outside while is “ << n << endl;return 0;
}
Output?Program Trace
24
The for statement
initalizetest
increment/decrement
true
statement(s)statement(s)
25
The for statement
for(initialization; test; increment/decrement)
statement;
for(initialization; test; increment/decrement)
{
statement;
statement;
}
26
The for statement - examples
//sum integers from 1 to 10int sum =0;for(int I=1;I<=10;I++)
sum = sum + I;
//sum odd integers from 1 to 10int sum =0;for(int I=1;I<=10;I+=2)
sum = sum + I;
27
Practice!
Determine the number of times that each of the following for loops is executed.
for (k=3; k<=10; k++){ statements;}
for (k=3; k<=10; ++k){ statements;}
for (count=-2; count<=10; count++){ statements;}
28
The break statement
• break;– terminates loop– execution continues with the first statement
following the loop
29
The continue statement
• continue;– forces next iteration of the loop, skipping any
remaining statements in the loop
30
Practice! What is the output?#include<iostream>using namespace std;int main(){ for(int i=0; i<10; i++) { if(i%2) { continue; }//end if cout << i << endl; }//end for return 0;}//end main
02468
31
Practice! What is the output?#include<iostream>using namespace std;int main(){ for(int i=0; i<10; i++) { if(i%2) { break; }//end if cout << i << endl; }//end for return 0;}//end main
0
32
Practice!
//This while loop calculates n!int nfact=1, n;cout << “enter positive integer “;cin >> n;while(n > 0){
nfact = nfact*n;n--;
}cout << n “! = “ << nfact;..
Write a for loop to replace the while loop