+ 1
At what condition do we use do while loop ?
5 Réponses
+ 2
Do while loops are used to execute the instructions at least once. For example if I want to get the input of a number and accept it if it is grater than 0, I should write
//this is C language
int i;
do{
scanf("%d",&i);
}while(i<=0);
So if "i" is less than 0 (or equal), the user will have to input the number again because the exit-condition would be true.
+ 1
thanks
0
so if you are writing a program how do we know which loop to be used
0
As I said above you should use it if you are sure that the instructions will be executed at least once.
There is not a precise rule, the various cases come out after you do exercises.
My example is one common situation where you use it to accept a number if it satisfies some conditions, but most of the other things (not everything) can be done with all the types of loops.
0
You're welcome