+ 5
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.
6 Respostas
+ 9
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.
+ 5
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!
+ 4
HonFu MargaritaK Muhammad Abu Baker Sahana Chowdary Thanks everyone 😃
I have another question, when to use this kind of expression ? Any practical examples ?
+ 4
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
}
+ 3
HonFu do you mean that it will use 5 for the condition in the first iteration, 3 in the second, 0 in the last
+ 1
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.