0
What will be the output of this code ? Please explain it ?
#include <stdio.h> int main() { do{ printf("Solo Learn"); } while(3,5,0); return 0; }
3 odpowiedzi
+ 3
Output is a line of text "Solo Learn".
The loop will run once only, because `do-while` loop always execute the statement(s) within its body, once before evaluating the loop condition.
You may have already noticed that a warning will be given because there were multiple expressions used as loop condition. Because the expressions are separated by comma operator; the last expression (0) is the only one counted for as a loop condition (3 and 5 are abandoned).
As you probably know already, in C or C++ language, logically, a zero is considered falsey (non zeroes are truthy), and thus, the loop ceases repetition because the loop condition is falsey. So the loop only runs once. Try to replace the last expression with non zero, and you will have yourself an infinite loop, printing "Solo Learn" over and over : )
(Edit)
About comma operator:
https://en.m.wikipedia.org/wiki/Comma_operator
https://stackoverflow.com/questions/54142/how-does-the-comma-operator-work
+ 3
Output :- Solo Learn
Note that --
Comma operator is a binary operator.
It returns the value of rightmost operand.
So, here condition of do-while loop is evaluated to 0.
0 is false value.
Therefore loop is executed once, then it reaches to termination.
This was my question. I was not familiar with comma operator.
+ 1
It will print Solo Learn for once and show you the errors because of the condition statement you put inside the while loop.anyway it run for first time .