At what condition do we use do while loop ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

At what condition do we use do while loop ?

25th Oct 2018, 7:26 PM
Tony Tomey Jose
Tony Tomey Jose - avatar
5 Answers
+ 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.
25th Oct 2018, 7:39 PM
Angelo
Angelo - avatar
+ 1
thanks
25th Oct 2018, 7:55 PM
Tony Tomey Jose
Tony Tomey Jose - avatar
0
so if you are writing a program how do we know which loop to be used
25th Oct 2018, 7:41 PM
Tony Tomey Jose
Tony Tomey Jose - avatar
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.
25th Oct 2018, 7:46 PM
Angelo
Angelo - avatar
0
You're welcome
25th Oct 2018, 7:56 PM
Angelo
Angelo - avatar