+ 1
do while loop
int a = 2; do { a+=3; } while(a < 4); Console.Write(a); someone explain how its answer is 5 answer should be < 4 right
3 Answers
+ 1
No because you the program does the following steps :
1) increments a by 3 ( a == 5 )
2) verifies if a is less than 4 so that it can repeat the block scope
3) sees that 5 > 4 so it doesn't repeat the block scope
4) prints a (5)
The result would always be †4 if you would increment a by 1 each time so it doesn't get out of the bounds or < 4 if you would also use a while loop instead of a do ... while loop
0
Stefan explained it best,
Also note,
do will execute atleast one time even if the while(condition) does not match.
Example: https://repl.it/JUdt/0
after executing for the 1st time, it won't run again if the conditions don't match.
0
the concept of do while loop is first do means execute code and then check while statement.
so thats why it increment 2 with 3 and print it then go to while condition.