Loops:
Loops are used to execute a set of statements again and again by writing once. Eg. we want to print ABC 100 times on the output scree then it will be better to use loops instead of writing cout<<"ABC"; 100 times in program.
Types of Loops:
1) for() 2) while()3) do-while()
1) for() loop:
it execute statements till the condition is true. It ist checks the condition if true then enters within the body of loop.
Syntax:
for(initialization;condition;updating)
{
body
}
2) while() loop:
like for loop while loop also execute statements till the condition is true. Like for loop it also checks the condition ist, if true then enters within the body of loop, so we can say that for can be replaced with while and vice versa.
Syntax:
initialization;
while(condition)
{
body
updating;
}
3) do-while() loop:
like for and while loops do-while also executes statements till the condition is true. But unlike other loops it ist execute the statements within the loop body and then checks the condition, so it always executes at least once.
Syntax:
initialization;
do
{
body
updating;
}
while(condition);
No comments:
Post a Comment