Friday 7 June 2013

Loop Question 6

What will be output of following c code?

#include<stdio.h>
int main(){
    int i=1;
    for(i=0;i=-1;i=1) {
         printf("%d ",i);
         if(i!=1) break;
    }
    return 0;
}



EXPLANATION


Output: -1
Explanation:
Initial value of variable i is 1.
First iteration:
For loop initial value: i = 0
For loop condition: i = -1 . Since -1 is non- zero number. So loop condition true. Hence printf function will print value of variable i i.e. -1
Since variable i is not equal to 1. So, if condition is true. Due to break keyword program control will come out of the for loop.

2 comments: