Presentation is loading. Please wait.

Presentation is loading. Please wait.

Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )

Similar presentations


Presentation on theme: "Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )"— Presentation transcript:

1 Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )

2 LOOPS STATEMENT A statement or set of statements that is executed repeatedly is called loop. The statements in a loop are executed for a specified number of times or until some condition remain true. in C++ there are three kinds of loop statements. These are The while loop The do while loop The for loop 2

3 THE WHILE LOOP It is a conditional loop statement. It is used to execute a statement or set of statements as long as the given condition remains true. The syntax of the while loop is while (condition) statement; Condition: it consists of a relational expression. If it is true statements given in while loop is executed. 3

4 Conti…. statement: it represents the body of loop. The compound statements are written in braces { }. syntax for compound statements. while(condition) { statement-1; statement -2; } 4

5 Program to print statement using while loop # include Void main() {clrscr(); int a; a=1; while(a<=5) { cout<<“I am a student”<<endl; a=a+1; } cout<<“program Ends” ; getch(); } 5

6 Conti………… when “while” loop is executed, the computer first evaluates the given condition. If the given condition is true the statement or set of statements under while is executed. After executing the statements under while the control shifts back to while and the condition is again tested. If the given condition becomes false at any stage the execution of the 6

7 Conti…. body of loop is terminated. And control shifts to the statement that comes immediately after the body of the loop. The body of the loop must contain a statement so that so that the condition on the loop becomes false during execution of the loop. If the condition of the loop never becomes false, the loop never ends. It becomes an infinite. 7

8 Flow chart for while loop Condition Body of loop Statement after while loop false true 8

9 Program to calculate the sum of first ten odd numbers. also print the odd number. #include void main() { int s, n; s=0; n=1; while (n<=10) { s=s + n; cout<<n<<endl; n=n+2; } cout<<“sum =”<<s<<endl; getch(); } 9

10 Program to print natural numbers 1 to 10 in descending order #include void main() { int n=10; while(n!=0) cout<<n--<<endl; } 10

11 The “do-while” loop The “do-while ” loop is also conditional loop statement. It is like a while loop but in this loop the condition is tested after executing the statement of the loop. the syntax of the “do while ”loop. do { statement; } while(condition); 11

12 Conti…. #include void main() { int n; n=1; do { cout<<n<<endl; n++; } while(n<=10); getch(); } 12

13 Flow chart for while loop Condition Body of loop Statement after while loop false true 13

14 #include void main() { int marks; char name[25],address[15],op; do { clrscr(); cout<<“Enter name of student”<<endl; cin>>name; cout<<“Enter address of student”<<endl; cin>>address; cout<<“Enter marks obtained”<<endl; cin>marks; cout<<“more record [Y/N]”; cin>>op; } while(op==‘y’|| op==‘Y’); getch() } 14

15 The for loop The for loop statement is used to execute a set of statements repeatedly for a fixed number of times. It is also known as counter loop. The structure of this loop is different from both the while and do-while loop. It has following parts o initialization o condition o increment o body of loop 15

16 Conti…….. for (initialization;conditio;increment/decrement) for(int a=5;a<10;a++) In initialization part the value in the variable is assigned when control enters into the loop first time. The initialization part of “for loop” is optional. If this part is committed then a semicolon is used in its place. the syntax of the for loop without initialization part is for(; a<=10;a++) 16

17 Conti………… in condition part the test condition is given. The body of the loop executes as long as this condition remains true. for example a<=10. in increment part the value in the variable is increased. the statement under the for loop are the body of loop. If more than one statement are to be executed these are enclosed in braces 17

18 Write program to print first ten natural number #include void main() {int c; for (c=1;c<=10;c++) cout<<c<<endl; getch(); } this program can also be written as void main() { int c=1; for(;c<=10;) { cout<<c<<endl; c++ } 18

19 FLOW CHART The for loop 19 Initialize variable Body of loop Incremental expression condition exit false true

20 20


Download ppt "Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )"

Similar presentations


Ads by Google