+ 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

10th Jul 2017, 7:58 AM
vineet kumar
vineet kumar - avatar
3 odpowiedzi
+ 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
10th Jul 2017, 8:02 AM
Stefan Octavian
Stefan Octavian - avatar
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.
10th Jul 2017, 8:37 AM
Salekin
Salekin - avatar
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.
11th Jul 2017, 8:25 PM
Vijay Makwana
Vijay Makwana - avatar