Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Loops

Loops come
inprogramming use
into
whenweneedtorepeatedly
execute
a
ofstatements.
block

Suppose "Hello
wewanttoprint World"
5
times. canbedoneintwoways
This
1.
Iterative
methobd
Aniterative
methodtodothis
isto
print
hello
world
5 times

siostream>
#include
using std;
namespace
intmain0

««"Hello
cout World
\n";
<<"Hello
cout World
\n";
"HelloWorld
cout \n":
<<"Hello
cout World\n";
<
cout "Hello
World\n":
0;
return
loops
2.Using
Loop ofinstruction
isa sequence that
is
a certain
repeateduntil is
condition
reached.
There two
are ofloops:
types
controlled
1.Entry loops
while
loop
forloop
conttolled
2.Exit loops
do-while
loop
While
loop
Thewhile
looploops througha block
of
codeaslongasa specified is
condition
true.

#include
ciostream>
using std;
namespace
intmain0

int =1;
i

while(i « 6)

«<"Hello
cout World
\n";

return
0;
Forloop
howmanytimes
Whenyouknowexactly
youwanttoloopthrougha block
ofcode,
use
theforloopinstead
ofa while
loop.

<iostream>
#include
using std;
namespace
intmain0

i<6;it+)
i = 1;
for(int
{

<<"Hello
cout World\n":
}

return
0;
DoWhile loop
loop
This willexecutethecodeblock
once, is
ifthecondition
before checking
repeattheloopaslongas
thenitwill
true,
istrue.
thecondition

*************

intmain()

=
int 1;
i

do
{
«<"Hello
cout World\n:
it+;
}while
(i <6);

return
0;

You might also like