While(1) is a infinite loop so if you need to come out of it then you need to issue the break statement explicitly.
main() { int i=0; while(1) { printf("%d\n", i++); if (i==2) break; } }
While(0) means non entry and code under while will never get executed.
main() { int i=0; while(0) { printf("%d\n", i++); // This will never get executed if (i==2) break; } }
While 0 { // will never be executed as condition always FALSE }
While 1 { // will never given up as condition always TRUE // this will be used in Multitasking/Threading concepts. }
while(0)
Actually While of something (i.e) while( X ) X is the condition we are giving to run the loop for X times, so as we know that in C the value 0 have meaning called FALSE(0x00000000). so whenever a loop receives a FALSE input it will just leave the loop and proceeds with the next immediate statement.
while(1) looking into While(X), if X = 1, here the value X = 1 will be taken as TRUE (i.e) 0xffffffff, so not only while(1) while (ANY NON ZERO VALUE) can run as infinite loop other than X = 0. simply ,the condition for the loop is always TRUE. so usually we have to give a condition for the loop to break.
while(1) Is infinite loop & While(0) Do not enter in while loop it directed exit the loop
While(0) means dont enter into it, but why you want to have while(0) in c code. Any suggestion.
Asked in the interview today.
Array consist of -1 and 1, Find count of all sub-arrays where sum = 0. Input: [-1,1,-1,1]
Output: 4 [-1,1] [1,-1],[-1,1],[-1,1,-1,1]