New course! Every coder should learn Generative AI!
Try a free lesson+ 5
do & for
when we use do and when we use for ??
4 Answers
+ 5
Please use propper tags for programming language. Thanks!
+ 2
In c and c++ , the concept and syntax of loops are same, in c/c++ the 'for' loop is for(init; expression; increment);
That's a syntax of for loop, in this case: init - init is the base condition, or staring point of the loop, expression - that's repeating time/turns of loop, and the increment for incrementation in for loop.
Do while loop- that's have similarities to while loop, only one difference in it is that, that should be execute at least once, without check any condition,
Syntax (do while) : do {
// Statements;
} while (condition);
+ 1
For is an entry controlled loop meaning the condition will be checked first before executing for looop.
Do is an exit controlled loop meaning it will execute once before checking the condition.
The usage depends on your need.
0
Purpose of the loops are the same, you just write loops on different way. There are while, do-while and for loops.