1) Give the output for the following program?
#include <stdio.h>
int i;
int main()
{
for (i=1;i<= 15; i++)
{ printf("%d\n", i);
if (i=10)
break;
}
return 0;
}
Answers
is initialized to 5 and as the loop condition is false before the first iteration itself so the loop doesn't execute. The statement System.out.println(i * 4); is outside the loop so it gets executed once, printing 20 to the console.
★Hope it's helps you...
Explanation:
Step 1: for(;;) this statement will genereate infinite loop.
Step 2: printf("%d\n", i++); this statement will print the value of variable i and increement i by 1(one).
Step 3: if(i>10) here, if the variable i value is greater than 10, then the for loop breaks.
There can exists a switch statement, which has no case.
switch(i) becomes switch(1), then the case 1: block is get executed. Hence it prints "Case1".
printf("This is c program."); is ignored by the compiler.
Hence there is no error and prints "Case1".
The while() loop must have conditional expression or it shows "Expression syntax" error.
Example: while(i > 10){ ... }
Because, case 3 + 2: and case 5: have the same constant value 5.