Question about do-while quiz | SoloLearn: Learn to code for FREE!


Question about do-while quiz

Can any explain this code ? Why it works and how ? do{ printf("SoloLearn"); }while(5, 3, 0); Any help will be appreciated.

1/29/2019 2:38:24 PM


6 Answers

New Answer


The condition here makes (dubious) use of the comma operator: An expression like that will evaluate everything in the parentheses (so you can sneak something like x++ in), but the final value for the whole expression will be the last mentioned item, which is 0 and therefore false.


Using a comma tells compiler to consider only the last value nd thus ignoring all other values……so here only 0 is considered nd since it's a do while loop, SoloLearn gets printed once!


HonFu MargaritaK Muhammad Abu Baker Sahana Chowdary Thanks everyone 😃 I have another question, when to use this kind of expression ? Any practical examples ?


You can use it if you need a expression to do a side effect before the condition check if ( y=f(x) , y<6 ){ //Do something }


HonFu do you mean that it will use 5 for the condition in the first iteration, 3 in the second, 0 in the last


A do while loop is somehow similar to a while loop. The difference basically is that the statements will be executed once before the condition in the brackets is tested. Therefore, if the condition in the brackets is correct the statements will continue to be executed until the condition in the brackets is no longer valid.