1. int i =1; while( i<= 10); { printf(“ \n%d”,i); i++; }
Output:- No output because a
infinite loop.
In the above program a “;” is encounter after the while so it is understand as following by the compiler
while(i<=10) {
; }
clearly in the body of loop there is no increment and only null statement so condition still remains true that
10. int a =4, b =0, c; while(a>=0) { a--; b++; if( a = =b) continue; else printf( “\n %d %d”,a,b); } Output:- 3 1 1 3 0 4 -1 5
In the above program first a = 4,b = 0 and when condition checked it becomes true and control execute the body of loop and a is decrease by 1 and b i increase by 1. so a = 3 and b = 1 now again condition is checked a
cause an indefinite loop and because of null statement no output is generated.
2. char ch;
while(ch =0; ch<=255;ch++)
printf(“\n %d-%c”,ch,ch);
Output:- Error.
In the above program “while” loop is used in the place of “for” that cause generate an error because of different syntax. 3. int a =4; while( a = =1) { a = a-1; printf(“%d”,a); --a; } Output:- No output
Because condition is false for the first time and control immediately comes out from the body of loop without execute anything.
4. int a =4,b,c;
b = --a; c = a--;
printf(“\n%d %d %d”,a,b,c);
Output:- 2 3 3
In the above program a = 4 now we understand the pre and post increment/decrement.
= = b that is false and control jumps to the else block and print the value of a and b that is 3 and 1 respectively now when control reach to the end of the loop again jumps to the condition now again condition remains true and again body of loop executed resultant a = 2 and b = 2 and when condition a = = b is checked it becomes true that continue the loop using “continue” statement and control jumps to the loop condition that is true because a = 2 that is greater then 0.again body of loop executed and a =1,b = 3 and gets printed because the condition in if is false and from the end of the loop control jump to the condition that is true and a =0 and b =4 that is not equal so printed out and repeatedly when condition is checked it is true and execute a = -1 and b = 5 that is not equal and gets printed through else block and this time when condition is checked becomes false because a = -1 that is not greater or equal to 0 and jumps out of the loop.
11. int a = 4, b = 0,c; while( a>=0) { if(a = =b) break; else printf(“\n%d %d”,a,b); a--; b++; }
b = --a (1) a = a-1[ ( a =3);] (2) b=a [(b =3)]; i.e. In prefix decrement first decrease and then assign.
c=a-- (1) c = a [c = 3]; (2) a = a- 1[ a = 2]; i.e. In postfix decrement first assign and then decrease.
so finally a = 2, b = 3, c = 3 and gets printed through printf().
5. int a =4,b =3,c;
c = a-- -b;
printf(“\n%d%d%d”,a,b,c);
Output:- 3 3 1
Here at first a = 4; and in the exp. c = a-- -b; postfix decrement op. is used with a means first a is use in exp and then decrease i.e. (1) c = a-b;[c = 4-3] [c = 1] (2) a = a-1;[a = 4- 1] [3] and b still remain same so finally a = 3, b = 3, c = 1 and gets printed through printf().
6. while(‘a’<’b’)
printf(“\n malyalam is a palindrome”);
Output:- “malyalam is a
palindrome” printed indefinitely. Because condition ‘a’<’b’ never becomes false and every time it execute the body of loop that print the above massage “malyalam is a palindrome”.
7. int i;
Output:- 4 0
3 1
Here a = 4 and b = 0 and when condition in loop is checked it becomes true and then control jumps to the else block because a!=b and gets printed the value of a and b that is 4 and 0 respectively. and then a is decreased by 1 and b is increased by 1 so a = 3 and b = 1.Again when repeatedly when loop condition is checked it becomes true and control executes the body of loop and control print out the value of a and b that is 3 and 1 from the else block because condition becomes false and now a = 2 and b = 2 and loop condition will be again true and when comparison a = = b gives true control execute the if block and jumps out of the loop because of “break” statement.
12. int i;
for(i = 1;i<=5;printf(“\n%d”,i)); i++;
Output:- 1 will printed indefinitely
no. of times.
Here “;” is used after the loop means loop contain NULL statement and i++ is not in the body of the loop because no braces is used. In first i is initialize with 1 and when condition is checked it becomes true and gets printed i that is 1 now no increment take place and whenever condition is checked it always becomes true and indefinitely print 1.
while(i =15) {
printf(“\n%d”,i); i++;
}
Output:- print 15 indefinitely.
Because in the statement while(i = 15), i is not compared with 15. Here i is assigned with 15 that is a non zero value so each time when condition is checked i becomes 15 and condition becomes true and execute the body of loop that first print the value of i means 15 and then increment by 1 and i becomes 16 but again when condition check is encountered then again i becomes 15 and again execute the body of loop as same as previous that cause 15 printed indefinitely. 8. float a = 1.1; while( a = =1.1) { printf(“\n%d”,x); x=x-0.1; } Output:- No output
Because in the condition a float variable is compared with double value and control jumps out of the loop.
Drill Note: whenever we use
floating point constant it is consider a double type value.
for(; ;) { if(i>5) break; else j+=i; printf(“%d\t”,j); i+=j; } Output:- 2 5
In this program in first i = 1 and j = 1; And whenever there is no condition in the for loop by default it is assume true and execute the body and when i>5 is executed control jumps to the else block because condition is false and then j+=i [j = j+i] that gives j =2 and than j is printed through printf(). Now next statement i+=j gives i =3 and from the ending braces of loop control jumps to the loop and check for the condition that is true because missing and again i>5 is checked that becomes false and control execute the else block that cause j =5 (3 + 2) and gets printed now i will be 8 and now when the next time i>5 condition is checked it will be true and control jumps out of the loop because of the “break” statement.