Download presentation
Presentation is loading. Please wait.
1
Computer Programming -1-
Lecture 8
2
Iteration Statements for loop while loop do-while loop
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
3
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.
4
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 : **********
5
Examples : * Print numbers from 1 to 20 : for ( int j=1 ; j<=20 ; j++) cout << j << "\t" ; Output : …………….. 20 * Print numbers from 20 to 1 : for ( int j=20 ; j>=1 ; j--) Output : …………….. 1 * Even number : for ( int i=2 ; i<=20 ; i+=2 ) cout << i << "\t" ; Output : …………….. 20 * Odd number : for ( int i=1 ; i<=20 ; i+=2) Output : …………….. 19
6
- 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 ; }
7
- 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 ; }
8
- 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; }
9
- Write a program to calculate the sum of numbers from 1 to n numbers
- 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;
10
Nested for loops Output: ***** **** *** ** *
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: ***** **** *** ** *
11
Nested for loops ***** Output :
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 : *****
12
While Loop The while loop : allows programs to repeat a statement or series of statements, as long as a certain test condition is true.
13
While Loop a=1 while (a<=10) cout << " * " ; a++;
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 : **********
14
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) i--;
15
- 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;
16
- 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;
17
- 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;
18
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);
19
- 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;
20
- 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;
21
- 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;
22
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
23
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; }
24
Lines from Program 5-16
25
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.
26
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
27
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.