Control Structures Repetition Dr. Khizar Hayat Associate Prof. of Computer Science
Repetition Algorithm to sum five numbers input by the user Start count 0 sum 0 Repetition Read Num count count + 1 sum sum + Num count = 5? No Yes Display sum as total Stop
Loops Loops have as a purpose to repeat some statement(s) a certain number of times if a given condition is true. There are three types of loops in C++: while loops do-while loops for loops
The while loop Its format is: while (condition) { statement 1; statement 2; statement 3; : } This while loop executes as long as the logical expression between parenthesis (condition) is true. When the condition evaluates to false, the loop is exited , i.e. the execution continues with the statement following the loop block. The condition is tested at the beginning of the loop and if false, the loop body will not be executed at all. For just one statement inside the loop, no need of{}. Note that statement(s) inside a while loop may execute 0 or more times
The while loop Example //custom countdown using while #include<iostream> using namespace std; int main() { int n; cout<<"Enter the starting number"<<endl; cin>>n; while(n>0) cout<<n<<“,"; --n; //n=n-1; } cout<<“FIRE"; return(0);
Repetition Algorithm to sum five numbers input by the user begin Start sum 0 begin set sum0 set count0 while count!=5 get num set countcount+1 set sumsum+num end while display sum as the total end count 0 count = 5? Yes No Read Num Display sum as total count count + 1 Repetition sum sum + Num Stop
Algorithm to sum five numbers input by the user using the while loop #include<iostream> using namespace std; int main() { int sum=0,count=0,num; while(count!=5) cout<<“\nEnter the next number\n”; cin>>num; sum=sum+num; count=count+1; } cout<<“\nThe sum is"<<sum<<endl; return(0);
The for loop Note that statement(s) inside a for loop may execute 0 or more times
Repetition Algorithm to sum five numbers input by the user begin Start sum 0 begin set sum0 set count0 for (count=0;count!=5;count++) get num set countcount+1 set sumsum+num end for display sum as the total end count 0 count = 5? Yes No Read Num Display sum as total count count + 1 Repetition sum sum + Num Stop
The for loop for ( /*initialization*/ ; /*condition*/; /*increment or decrement*/) { statement(s); } The initialization step is executed first, and only once. This step allows you to optionally declare and initialize any loop control variables. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop. After the body of the for loop executes, the flow of control jumps back up to the increment/decrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
The for loop Example //custom countdown using while #include<iostream> using namespace std; int main() { int n; cout<<"Enter the starting number"<<endl; cin>>n; for(;n>0;--n) cout<<n<<“,"; } cout<<“FIRE"; return(0);
Algorithm to sum five numbers input by the user using the for loop #include<iostream> using namespace std; int main() { int sum=0,count=0,num; for(;count!=5;count++) cout<<“\nEnter the next number\n”; cin>>num; sum=sum+num; } cout<<“\nThe sum is"<<sum<<endl; return(0);
Algorithm to sum five numbers input by the user using the for loop #include<iostream> using namespace std; int main() { int sum=0,num,count; for(count=0;count!=5;count++) cout<<“\nEnter the next number\n”; cin>>num; sum=sum+num; } cout<<“\nThe sum is"<<sum<<endl; return(0);
Algorithm to sum five numbers input by the user using the for loop #include<iostream> using namespace std; int main() { int sum=0,num; for(int count=0;count!=5;count++) cout<<“\nEnter the next number\n”; cin>>num; sum=sum+num; } cout<<“\nThe sum is"<<sum<<endl; return(0);
The do-while loop do { statement(s); //executed at least once } while( /*condition*/) ; // Do not forget the semicolon here Unlike the for and while loops, the do-while loop guarantees at least one execution, even if the condition is false at the start. The do-while loop is usually used when the condition is within the loop statement itself.
Repetition Algorithm to sum five numbers input by the user begin Start sum 0 begin set count0 set sum0 do get num set countcount+1 set sumsum+num while count!=5 end do display sum as the total end count 0 Read Num count count + 1 sum sum + Num count = 5? No Repetition Yes Display sum as total Stop
Algorithm to sum five numbers input by the user using the do-while loop #include<iostream> using namespace std; int main() { int sum=0,count=0,num; do cout<<“\nEnter the next number\n”; cin>>num; sum=sum+num; count=count+1; } while(count!=5); cout<<“\nThe sum is"<<sum<<endl; return(0);
The for loop The countdown Example //custom countdown using while #include<iostream> using namespace std; int main() { int n; cout<<"Enter the starting number"<<endl; cin>>n; for(;n>0;--n) cout<<n<<“,"; } cout<<“FIRE"; return(0);
The while loop The countdown Example //custom countdown using while #include<iostream> using namespace std; int main() { int n; cout<<"Enter the starting number"<<endl; cin>>n; while(n>0) cout<<n<<“,"; --n; //n=n-1; } cout<<“FIRE"; return(0);
The do-while loop The countdown Example //custom countdown using do-while #include<iostream> using namespace std; int main() { int n; cout<<"Enter the starting number"<<endl; cin>>n; do cout<<n<<“,"; --n; //n=n-1; } while(n>0);//don’t forget the semicolon cout<<“FIRE"; return(0);
Examples Factorial of a number Ex 10 from notes (page 28) Max/min among n numbers