While loop
While loop:
The while loop is a conditional loop, which
repeat the code written in the body of the loop. The while is used to repeat
the code until the given conditional remain true. There are many uses of loops
such as repeat a piece of codes, repeat any logic for number of times and print
the series of number as our own desire. The loop is also used to save our time
and space, such that if we repeat a same code for 100 times it takes more time
to write and more space to store the coding. The loop is easy to debug and
control the flow of program.
Syntax:
The
general syntax for write the while loops.
While (condition)
{
Body of loop;
increment if needed;
}
Example:
Write the
program that print counting from 1 to 10 by using while loop.
#include <iostream>
using namespace std;
void main (void)
{
int loop=1; // variable initialization
while (loop<=10) //
conditional
{
cout<<loop<<endl;
// write the statement for counting
loop++;
// increment in the variable which is repeating
}
}
Comments
Post a Comment