+ 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.

29th Jan 2019, 2:38 PM
Corey
Corey - avatar
6 Antworten
+ 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.
29th Jan 2019, 4:22 PM
HonFu
HonFu - avatar
+ 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!
30th Jan 2019, 3:59 AM
Sahana Chowdary
Sahana Chowdary - avatar
+ 4
HonFu MargaritaK Muhammad Abu Baker Sahana Chowdary Thanks everyone 😃 I have another question, when to use this kind of expression ? Any practical examples ?
31st Jan 2019, 12:28 AM
Corey
Corey - avatar
+ 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 }
31st Jan 2019, 2:26 AM
Muhammad Abu Baker
Muhammad Abu Baker - avatar
+ 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
30th Jan 2019, 1:28 AM
Muhammad Abu Baker
Muhammad Abu Baker - avatar
+ 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.
29th Jan 2019, 9:38 PM
MargaritaK