Visual Basic loop structures allow you to run one or more lines of code repetitively. You can repeat the statements in a loop structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection.
2. WHIE DO WHILE
FOR NESTED
WHILE
FOR
DO WHILE
NESTED
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if you
specifically know start and end
position of the loop counter.
used for executing a block of
statements repeatedly until a
given condition returns false.
it executes the statements
inside the body of do-while
before checking the condition.
2
3. WHILE DO WHILE
FOR NESTED
WHILE
FOR
Here you can add some short text that is
important to explain the tittle text
DO WHILE
NESTED
Here you can add some short text that is
important to explain the tittle text
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if
you specifically know start and end
position of the loop counter.
used for executing a block
of statements repeatedly
until a given condition
returns false.
it executes the statements
inside the body of do-while
before checking the
condition.
3
4. WHILE DO WHILE
FOR NESTED
WHILE
FOR
Here you can add some short text that is
important to explain the tittle text
DO WHILE
NESTED
Here you can add some short text that is
important to explain the tittle text
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if
you specifically know start and end
position of the loop counter.
used for executing a block
of statements repeatedly
until a given condition
returns false.
it executes the statements
inside the body of do-while
before checking the
condition.
4
5. WHILE DO WHILE
FOR NESTED
WHILE
FOR
Here you can add some short text that is
important to explain the tittle text
DO WHILE
NESTED
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if
you specifically know start and end
position of the loop counter.
used for executing a block
of statements repeatedly
until a given condition
returns false.
it executes the statements
inside the body of do-while
before checking the
condition.
5
6. WHILE DO WHILE
FOR NESTED
WHILE
FOR
Here you can add some short text that is
important to explain the tittle text
DO WHILE
NESTED
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if
you specifically know start and end
position of the loop counter.
used for executing a block
of statements repeatedly
until a given condition
returns false.
it executes the statements
inside the body of do-while
before checking the
condition.
6
7. WHILE DO WHILE
FOR NESTED
WHILE
used for executing a block of
statements repeatedly until a
given condition returns false.
FOR
Here you can add some short text that is
important to explain the tittle text
DO WHILE
NESTED
LOOP
Loops are control structures used to repeat a given section of code a certain number
of times or until a particular condition is met.
Writing a loop inside another loop is
known as nested loop. We can
write while , do...while , for loop in a
nested loop.
for loop is easy to implement if you
specifically know start and end
position of the loop counter.
it executes the statements
inside the body of do-while
before checking the condition.
7
8. WHILE LOOP
While loop is the simplest loop of c++ language. This loop executes one
or more statement while the given condition remain true. It is useful
when the number of iteration is not known in advance.
In most computer programming languages, a while loop is a control
flow statement that allows code to be executed repeatedly based on a
given Boolean condition. The while loop can be thought of as a
repeating if statement
8
9. SYNTAX
• The syntax of while loop is as follow:
While (condition)
Statement;
Condition:
The condition is given as a relational expression. The statement is
executed only if the given condition is true. If the condition is false, the statement is
never executed.
Statement:
statement is the instruction that is executed when the condition is true.
If two or more statement are used, these are given in braces { }. It is called the body
of the loop.
9
10. SYNTAX FOR COMPOUND SATATEMENT
• The syntax for compound statement is as follow:
• While (condition)
{
statement 1;
statement 2;
:
:
statement N;
10
11. WORKING OF ‘WHILE ‘ LOOP
• First of all, the condition is evaluated.
• If it is true, the control enters the body of the loop and executes all statement in
the body.
• After executing the statements, it again moves to the start of the loop and
evaluates the condition again.
• This process continuous as long as the condition remains true.
• when the condition becomes false, the loop is terminated.
• while loop terminates only when the condition becomes false.
• If the condition remains true, the loop never end.
• The loop that has no end point is known as an infinite loop.
11
12. EXAMPLE:
Write a program that displays “ Pakistan” for five times using while loop.
#include<iostream.h>
#include<conio.h>
void main ()
{
int n;
clrscr();
n=1
while(n<=5)
{
cout<<“Pakistan”<<endl;
n++;
}
getch();
} 12
13. What is do –while loop
• Do while loop is a variant of While loop where the
condition is not checked at the top but at the end of
the loop.
• It is known as exit controlled loop.
DO WHILE LOOP
13
14. Uses of do while loop
• It is used to execute a statement or set of statements
repeatedly as long as the given condition remains true.
• It is mostly used for menu selection(or to enter records).In
menu selection we have to execute the body of loop at least
once.
Syntax
do
{
body of the loop;
}while (condition);
14
15. Working of do while loop
• When the do while statement is executed first the body of the loop is executed
and then the condition is evaluated
• If condition is True ,execution control goes back to the beginning of the do –while
loop. This process is repeated.
• When the given condition is false during execution ,the loop is terminated.
15
17. DIFFERENCE
While loop
• Test condition comes before the body
of loop.
• At the beginning of loop condition is
evaluated and then body of the loop
is executed if the condition is true.
• Semicolon(;)is not given after the
while(condition).
Do while loop
• Test condition comes after the body of
the loop.
• The condition is evaluated after
executing the body of loop. The body
of the loop must be executed at least
once even if the condition is false.
• Semicolon(;) is given after the while
(condition).
17
18. FOR LOOP
The “FOR” loop is used to execute a statement or set of statements repeatedly for a
specified number of times.
Syntax
for (int; condition; increment )
{
statement;
}
18
19. Example : Write a program that displays counting from 1 to 10 using
for-loop
#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
for(n=1;n<=10;n++)
cout<<n<<endl;
cout<<“ok”;
getch();
}
19
20. Example: Write a program that uses a “for” loop structure and the
following series:
1,2,4,8,16,32,64
#include<iostream.h>
#include<conio.h>
main( )
{
int,n,res;
clrscr ( );
for (n=1;n<=64;n+=n)
count<<n<<“t”;
getch ( );
}
20
21. NESTED WHILE LOOP
A while loop inside another while loop is called nested while loop
Syntax of Nested while loop
21
22. EXAMPLE OF NESTED LOOP
Example : C program to print the number pattern.
#include<iostream.h>
#include<conio.h>
Void main()
{
int i=1,j;
clrscr();
while(i<=5)
{
j=1;
while(j<=i)
{
cout<<j;
j++;
}
cout<<“n”;
i++;
}
getch();
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
22
23. A do-while loop inside another do-while loop is called nested do-while loop.
NESTED DO WHILE LOOP
Syntax of Nested do-while loop
23
24. EXAMPLE OF NESTED DO WHILE
LOOPExample : C program to print the given star pattern. *
* *
* * *
* * * *
* * * * *
#include<iostream.h>
#include<conio.h>
Void main()
{
Int i=1,j;
Clrscr();
do
{
j=1;
do
{
cout<<“*”;
j++;
}while(j<=i);
i++;
cout<<“n”;
} while(i<=5);
getch();
} 24
25. NESTED FOR LOOP
A for loop inside another for loop is called nested for loop.
Syntax of Nested for loop
25
26. EXAMPLE OF NESTED FOR LOOP
Example: C program to print the 2x2 matrices.
#include<iostrem.h>
#include<conio.h>
void main()
{
int A[2][2];
clrscr();
for(int i=0; i<2; i++)
{
for(int j=0; j<2; j++)
{
cin>>A[i][j];
cout<<“t”<<A[i][j];
}
cout<<“n”;
}
getch();
} 26