Computer Programming -1Lecture 8 Iteration Statements There may be a situation, when you need to execute a block of code several number of times. In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. Iteration Statements : for loop while loop do-while loop The for statement (for loop) The For Loop Statement : is an iteration statement that executes a set of code repeatedly, given the initial value, the condition ,increment or decrement value. The for statement (for loop) The syntax of a for loop in C++ is: for(initialization ; condition ; increase or decrease) { statement; } Example : For ( int i=1 ; i<=10 ; i++) cout << " * “ ; Output : ********** Examples : * Print numbers from 1 to 20 : for ( int j=1 ; j<=20 ; j++) cout << j << "\t" ; Output : 1 2 3 4 5 …………….. 20 * Print numbers from 20 to 1 : for ( int j=20 ; j>=1 ; j--) cout << j << "\t" ; Output : 20 19 18 17 …………….. 1 * Even number : for ( int i=2 ; i<=20 ; i+=2 ) cout << i << "\t" ; Output : 2 4 6 8 10 …………….. 20 * Odd number : for ( int i=1 ; i<=20 ; i+=2) cout << i << "\t" ; Output : 1 3 5 7 9 …………….. 19 - Write a C++ program by using for statement to print only odd numbers from 1 to 100 #include <iostream.h> int main () { for ( int i=1 ; i<100 ; i+=2 ) cout << i << endl ; return 0 ; } - Write a C++ program by using for statement to print characters from A to Z #include <iostream.h> int main () { for ( char i='A' ; i<='Z' ; i++ ) cout << i << " , " ; return 0 ; } - Write a C++ program by using for statement to print the Even numbers from 0 to (n) numbers. #include <iostream.h> int main() { int n; cout<<"Enter the number : "; cin>>n; for (int i=0 ; i<=n ; i+=2 ) cout << i << "\t"; return 0; } - Write a program to calculate the sum of numbers from 1 to n numbers? (n: any number enter by user ) #include <iostream.h> int main() { int n; float sum; sum=0; cout<<"Enter the number : "; cin>>n; for (int j=1 ; j<=n ; j++ ) { sum=sum+j ; } cout << "the sum of numbers = "<< sum << endl; return 0; } Nested for loops Example : #include <iostream.h> int main() { for(int i=5 ; i>=1 ; i--) { for(int j=1 ; j<=i ; j++) cout << " * " << " " ; cout << endl; } return 0 ; } Output: ***** **** *** ** * Nested for loops Example : #include <iostream.h> int main() { for ( int i=5 ; i>=1 ; i--) { for(int x=1 ; x<=5 ; x++) cout << "*" <<" "; cout << endl; } return 0 ; } Output : ***** ***** ***** ***** ***** While Loop The while loop : allows programs to repeat a statement or series of statements, as long as a certain test condition is true. While Loop The syntax of a While loop in C++ is: initializing while (condition) { statements; increment or decrement value; } Example : a=1 while (a<=10) { cout << " * " ; a++; } Output : ********** Examples : * Print numbers from 1 to 10 : int i=1; while(i<=10) { cout<<i<<" ,"; i++; } * Print numbers from 10 to 1 : int i=10; while(i>=1) { cout<<i<<" ,"; i--; } - Write a C++ program by using while loop statement to print only even numbers from 0 to 100 #include<iostream.h> int main() { int i=0; while(i<=100) { cout<<i<<endl; i+=2; } return 0; } - Write a C++ program by using while loop statement to find the sum of odd numbers from 1 to 100. #include<iostream.h> int main() { int i=1 , sum=0; while(i<=100) { sum =sum + i; i+=2; } cout<<" the sum is : " << sum << endl; return 0; } - Write a C++ program by using while statement to find the square and cubic values of the numbers ( the numbers is entered by user ..until the user ask for termination ) #include<iostream.h> int main() { int a; cout << " To stop work of the program enter the number (0) " <<endl ; while(a != 0) { cout << " enter the number : " ; cin >> a ; cout << " square = " << a*a <<endl; cout << " cubic = " << a*a*a <<endl; } return 0; } Do while Loop The do-while loop: is similar to the while loop, except that the test condition occurs at the end of the loop. The syntax of a While loop in C++ is: do { statement; increment or decrement value; } while(condition); -Write a C++ program by using do while statement to print numbers from 1 to 10 #include<iostream.h> main() { int i=1; do { cout<<i<<endl; i++; } while(i<=10); return 0; } -Write a C++ program by using do while statement to print only even numbers from 20 to 1 #include<iostream.h> main() { int i=20; do { cout<<i<<endl; i-=2; } while(i>=1); return 0; } - Write a C++ program by using do while statement to request the user to enter any number and exit if input is equal 6 .. #include<iostream.h> main() { int a; do { cout << " enter the number : " ; cin >> a ; } while(a != 6); return 0; } Deciding Which Loop to Use while: pretest loop; loop body may not be executed at all do-while: posttest loop; loop body will always be executed at least once for: pretest loop with initialization and update expression; useful with counters, or if precise number of repetitions is needed 5-22 Nested Loops A nested loop is a loop inside the body of another loop Inner (inside), outer (outside) loops: for (row=1; row<=3; row++) //outer { for (col=1; col<=3; col++)//inner cout << row * col <<“\t”; cout<<endl; } 5-23 Lines from Program 5-16 5-24 Nested Loops - Notes Inner loop goes through all repetitions for each repetition of outer loop Inner loop repetitions complete sooner than outer loop Total number of repetitions for inner loop is product of number of repetitions of the two loops. 5-25 Breaking Out of a Loop Can use break to terminate execution of a loop Use sparingly if at all – makes code harder to understand and debug When used in an inner loop, terminates that loop only and goes back to outer loop 5-26 The continue Statement Can use continue to go to end of loop and prepare for next repetition while, do-while loops: go to test, repeat loop if test passes for loop: perform update step, then test, then repeat loop if test passes Use sparingly – like break, can make program logic hard to follow 5-27