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

Cl9 C++ While

The document describes while and do-while loops in C++. While loops check a terminating condition first and execute the loop body only if the condition is true. Do-while loops always execute the body at least once before checking the terminating condition. Both loops can use increment/decrement to iterate over a range of values from low to high or high to low. Examples are provided printing numbers with while and do-while loops.

Uploaded by

Anurag Goel
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Cl9 C++ While

The document describes while and do-while loops in C++. While loops check a terminating condition first and execute the loop body only if the condition is true. Do-while loops always execute the body at least once before checking the terminating condition. Both loops can use increment/decrement to iterate over a range of values from low to high or high to low. Examples are provided printing numbers with while and do-while loops.

Uploaded by

Anurag Goel
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

While loop:

syntax of while:

initial value;
while(terminating condition)
{
statements...
increment/decrement;
}

here initial value is the start value with which the loop starts
terminating condition is that value which is checked everytime to proceed further
and as soon as it surpasses, loop exits and control is transferred to the statement
immediately after loop body
increment or decrement may be used depending upon the requirement, increment is
used in positive loops (low to high value) and decrement is used in negative loops
(high to low value)

example:
int i=1;
while(i<=10)
{
cout<<i<<"\t";
i++;
}
prints all the numbers starting from 1 till 10 in one line with gaps in between
(positive loop)

int i=10;
while(i>=1)
{
cout<<i<<"\t";
i--;
}
prints all the numbers starting from 10 till 1 in one line with gaps in between
(negative loop)

int i=2;
while(i<=10)
{
cout<<i<<"\t";
i+=2;
}
prints all the even numbers starting from 2 till 10 in one line with gaps in
between, here we have given increment of 2 everytime, i+=2 means i=i+2

int i=1;
while(i<=10)
{
cout<<i<<"\n";
i++;
}
prints all the numbers starting from 1 till 10 in different lines

int i=1;
while(i<=10)
{
cout<<i;
i++;
}
prints all the numbers starting from 1 till 10 without any gaps in between

Do while loop:

syntax of do while:

initial value;
do
{
statements...
increment/decrement;
}while(terminating condition);

here initial value is the start value with which the loop starts
increment or decrement may be used depending upon the requirement, increment is
used in positive loops (low to high value) and decrement is used in negative loops
(high to low value)
terminating condition is that value which is checked everytime to proceed further
and as soon as it surpasses, loop exits and control is transferred to the statement
immediately after loop body but here the loop executes at least once since the
condition is checked at the end

example:

int i=1;
do
{
cout<<i<<"\t";
i++;
}while(i<=10);

prints all the numbers starting from 1 till 10 in one line with gaps in between
(positive loop)

int i=10;
do
{
cout<<i<<"\t";
i--;
}while(i>=1);

prints all the numbers starting from 10 till 1 in one line with gaps in between
(negative loop)

int i=2;
do
{
cout<<i<<"\t";
i+=2;
}while(i<=10);

prints all the even numbers starting from 2 till 10 in one line with gaps in
between, here we have given increment of 2 everytime, i+=2 means i=i+2

int i=1;
do
{
cout<<i<<"\n";
i++;
}while(i<=10);

prints all the numbers starting from 1 till 10 in different lines

int i=1;
do
{
cout<<i;
i++;
}while(i<=10);

prints all the numbers starting from 1 till 10 without any gaps in between

You might also like