0% found this document useful (0 votes)
46 views

1) The For Loop 2) The While Loop 3) The Do While Loops: Bject O Become Familiar With Loops

The document introduces loops and the three types of loops in C++: for loops, while loops, and do-while loops. It explains that loops allow code to be executed repeatedly without writing it multiple times. It provides the syntax and flow of each loop type, and examples to calculate even numbers, numbers divisible by 5, and factorial. Finally, it lists exercises involving loops to generate series and patterns.

Uploaded by

Darya Memon
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

1) The For Loop 2) The While Loop 3) The Do While Loops: Bject O Become Familiar With Loops

The document introduces loops and the three types of loops in C++: for loops, while loops, and do-while loops. It explains that loops allow code to be executed repeatedly without writing it multiple times. It provides the syntax and flow of each loop type, and examples to calculate even numbers, numbers divisible by 5, and factorial. Finally, it lists exercises involving loops to generate series and patterns.

Uploaded by

Darya Memon
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

21 32

OBJECT: TO

BECOME FAMILIAR WITH LOOPS

INTRODUCING

LOOPING AND LOOPS

Looping is the process of executing a particular portion of program multiple times with out writing that code multiple number of times. Suppose if you have to print your name infinite number of times then you can not write the output statement infinite number of time. To overcome this major hurdle loops are introduced in almost all the programming languages. Loops are techniques, predefined in any programming language which are used to execute the particular block of code multiple number of times. The major advantage of using loops is that you dont have to write the code multiple number of times which can save your much time, another advantage is that using loops can reduce the size of the program dramatically. The loops are constructed in such a way that when ever the control reaches the end of the loop then the loop transfers the control to the top of the loop and this process of transferring control from bottom to top and top to bottom continues until a certain condition of the loop becomes false. Figure 3.1, shows the operation of the loop. In C++ there are three types of loops: 1) 2) 3) The for loop The while loop The do while loops

False

Transfer of control

Start of the Loop

Termination of the Loop

True

End of the Loop

Figure 3.1, Operation of the loop.

2 33
1)
The for loop

The for loop is used to execute the particular block of code fixed number of times with out writing that block of code multiple number of time. The for loop is used when we know that how many times the loop will be executed. The for loop uses the keyword for followed by the three expressions in the parentheses. The three expressions in parentheses are Initialization Expression, Test Expression and Increment Expression. The Initialization Expression initializes the loop variable and it is executed first and only once in the loop. It tells that from which value the loop will start. The Test Expression is executed each time the loop is repeated and it contains relational operators which tests the value of the loop variable. If the condition becomes true in the test expression then the loop will be executed once more else the loop will be terminated. The Increment Expression increments or decrements the value of the loop variable. It is always executed at the end of the loop. In single statement for loop body you dont have to enclose the body of the loop in the braces but in case of multiple statement for loop body you have to enclose the body of the loop in the braces. The structure of the for loop is given below:

Initialization Expression Test Expression Increment Expression

for (i=1; i<=10; i++) statement;

Note: no semicolon here Single Statement for loop body

for (i=1; i<=10; i++) { statement; statement; statement; }


Note: no semicolon here

Note: no semicolon here

Multiple Statement for loop body

34
FLOW
CHART OF FOR LOOP

Initialization Expression

False Test Expression

Exit

True

Body of loop

Increment Expression

Program (forloop.cpp)
#include <iostream.h> #include <conio.h> void main() { int a; for(a=1; a<=10; a++) { cout<<a; cout<<endl; } getch(); }

2 35
2) The while loop

The while loop is used to execute the particular block of code multiple number of times until the certain condition becomes false. The while loop is used when we dont know that how many times the loop will be executed. The while loop uses the keyword while followed by the test expression in the parentheses. The test expression in parentheses contains relational operators which tests the value of the loop variable. If the condition becomes true then the loop will be executed once more else the loop will be terminated. In single statement while loop body you dont have to enclose the body of the loop in the braces but in case of multiple statement while loop body you have to enclose the body of the loop in the braces. The structure of the while loop is given below:

Test Expression

while (a!=0) statement;

Note: no semicolon here Single Statement while loop body

while (x<=y) { statement; statement; statement; }

Note: no semicolon here

Multiple Statement while loop body

Note: no semicolon here

36
FLOW
CHART OF WHILE LOOP

False Test Expression

Exit

True

Body of loop

Program (whileloop.cpp)
#include <iostream.h> #include <conio.h> void main() { int a=1; while(a!=10) { cout<<a<<endl; a++; } getch(); }

37
3)
The do while loop

The do while loop is used to execute the particular block of code atleast once. The do while loop is used when we dont know that how many times the loop will be executed but we know that atleast once the loop will be executed. The do while loop uses the keywords do and while. The do while loop starts with the keyword do and then the body of the loop is to be written and at the last the keyword while is used followed by the test expression in the parentheses. The semicolon ; is used after the keyword while so that the loop will be terminated when the test expression fails. The test expression in parentheses contains relational operators which tests the value of the loop variable. If the condition becomes true then the loop will be executed once more else the loop will be terminated. In single statement do while loop body you dont have to enclose the body of the loop in the braces but in case of multiple statement do while loop body you have to enclose the body of the loop in the braces. The structure of the do while loop is given below:

do

Note: no semicolon here

statement; while (ch!=n);

Single Statement do while loop body Note: semicolon here

Test Expression

do {

Note: no semicolon here

statement; statement; statement; } while (ch!=n);


Note: semicolon here Multiple Statement do while loop body

Test Expression

38
FLOW
CHART OF DO WHILE LOOP

Body of loop

False Test Expression

Exit

True

Program (dowhileloop.cpp)
#include <iostream.h> #include <conio.h> void main() { int a=1; do { cout<<a<<endl; a++; } while(a!=10); getch(); }

39
THE
DIFFERENCE BETWEEN THE THREE LOOPS

The three loops for, while and do while execute particular block of code multiple number of times. The difference between these three is only of how many times they execute the particular block of code. In case of for loop we already know that how many times the loop will be executed but in case of while and do while loops we dont know that how many times will the loop be executed but in do while loop we know that the block of code will be executed atleast once. In for and while loops the test expression is at top and executed first but in do while loop firstly the body of the loop is executed and then the condition is tested. The while and do while loops can executed certain block of code infinite number of times but the for loop can only execute the block of code finite number of times.

Finite number of times

Infinite number of times

Infinite number of times

40

EXERCISE
1.
a. b. 2. 3. * * * * * * * * * With the help of For, While, Do While loops, generate the following series. Even numbers b/w 1 and 40. Numbers that are divided by 5, from 1 to 1000. Write a program using for loop which inputs as integer and prints its factorial? Write a program that will generate the following output * * * * * * * * * * * * * * * * * * * *

4. Write a program that counts blanks, digits, uppercase, lowercase letters, new lines and other characters entered through the keyboard, use character t to terminate the input. 5. Write a program using two nested loops to generate the following output. Note that the last column contains the sum of the corresponding row. 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 10 26 42 58

41

EXERCISE SOLUTIONS
===================================================

Program #01 (a) With for loop


#include <constream.h> void main() { clrscr(); for(int i=1; i<=40; i++) { if(i%2==0)cout<<i<<endl; getch(); }

Program #01 (a) With while loop


#include <constream.h> void main() { clrscr(); int i=1; while(i<=40) { if(i%2==0)cout<<i<<endl; i++; getch(); }

Program #01 (a) With do while loop


#include <constream.h> void main() { clrscr(); int i=1; do{ if(i%2==0)cout<<i<<endl; i++; }while(i<=40); getch(); }
===================================================

42
===================================================

Program #01 (b) With for loop


#include <constream.h> void main() { clrscr(); for(int i=1; i<=1000; i++) { if(i%5==0)cout<<i<<endl; } getch(); }

Program #01 (b) With while loop


#include <constream.h> void main() { clrscr(); int i=1; while(i<=1000) { if(i%5==0)cout<<i<<endl; i++; } getch(); }

Program #01 (b) With do while loop


#include <constream.h> void main() { clrscr(); int i=1; do{ if(i%5==0)cout<<i<<endl; i++; }while(i<=1000); getch(); }
===================================================

43
===================================================

Program #02
#include <constream.h> void main() { clrscr(); int num, fact=1;; cout<<"Enter the number to calculate its factorial : "; cin>>num; for(int i=1; i<=num; i++) fact*=i; cout<<num<<"! = "<<fact; getch(); }
===================================================

Program #03
#include <constream.h> void main() { for(int i=5; i>=1; i--) { for(int j=1; j<=i; j++) cout<<"*"; cout<<endl; } for(i=2; i<=5; i++) { for(int j=1; j<=i; j++) cout<<"*"; cout<<endl; } getch(); }
===================================================

44
===================================================

Program #04

#include <constream.h> #include <ctype.h> void main() { clrscr(); int lower=0, upper=0, digits=0, escape=0, newlines=0, spaces=0, tabs=0; char ch; while((ch=getche())!='t') { cout<<endl; if(isalpha(ch)!=0) { if(islower(ch)!=0)lower++; else upper++; } else if(isdigit(ch)!=0) digits++; else if(isspace!=0) { escape++; if(ch==13)newlines++; else if(ch==9)tabs++; else if(ch==32)spaces++; } } cout<<endl<<endl; cout<<"Lower: "<<lower<<endl; cout<<"Upper: "<<upper<<endl; cout<<"Digits: "<<digits<<endl; cout<<"Escape Sequences: "<<escape<<endl; cout<<"New Lines: "<<newlines<<endl; cout<<"Tabs: "<<tabs<<endl; cout<<"Spaces: "<<spaces; getch(); }
===================================================

45
===================================================

Program #05
#include <constream.h> void main() { clrscr(); int a=0,b=0; for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { b++; a+=b; cout<<b<<" "; } cout<<a<<endl; a=0; } getch(); }
===================================================

You might also like