C Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - General Questions (Q.No. 1)
1.
How many times "IndiaBIX" is get printed?
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
continue;
else
break;
printf("IndiaBIX");
}
return 0;
}
Discussion:
142 comments Page 12 of 15.
Aarushi said:
1 decade ago
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
printf("hi");
else
break;
printf("IndiaBIX");
}
return 0;
}
The output is:
hiIndiaBix
hiIndiaBix
hiIndiaBix
hiIndiaBix
hiIndiaBix
hiIndiaBix
Why?
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
printf("hi");
else
break;
printf("IndiaBIX");
}
return 0;
}
The output is:
hiIndiaBix
hiIndiaBix
hiIndiaBix
hiIndiaBix
hiIndiaBix
hiIndiaBix
Why?
Priyanka Rawat said:
1 decade ago
Yes, the control does not go to print statement as before it break statement is given, and if we remove break keyword the word will get get printed 6 times... -1, 0, 1, 2, 3, 4.. as x<5 is given.
Sudharsan said:
1 decade ago
The continue statement does not allow to execute statement that present after the continue statement and it allows next iteration.
Murugesan said:
1 decade ago
@Rohit and Aminul.
Let I explain your program o/p,
b=5 this is the value given in the program, if 'b++' the value will not get increase, in case of '++b' the value will get increase by one.
int c = ((b++) + (++b) + (++b) + (++b));
in above statement first (b++) the value remains 5,
Second (++b) the value gets increase by one so now value is 6, third (++b) the value gets increase by one so now value is 7,fourth (++b) the value gets increase by one so now value is 8.
In C the last incremented value will apply to both side of variables. so fourth (++b) value 8, will apply to third (++b), now third (++b) value change to 8, second (++b) value remains 6, first (b++) value remains 5.
So 5+6+8+8 = 27.
Hope you understand.
Let I explain your program o/p,
b=5 this is the value given in the program, if 'b++' the value will not get increase, in case of '++b' the value will get increase by one.
int c = ((b++) + (++b) + (++b) + (++b));
in above statement first (b++) the value remains 5,
Second (++b) the value gets increase by one so now value is 6, third (++b) the value gets increase by one so now value is 7,fourth (++b) the value gets increase by one so now value is 8.
In C the last incremented value will apply to both side of variables. so fourth (++b) value 8, will apply to third (++b), now third (++b) value change to 8, second (++b) value remains 6, first (b++) value remains 5.
So 5+6+8+8 = 27.
Hope you understand.
Vipin dhawan said:
1 decade ago
In above statement first (b++) the value remains 5,
Second (++b) the value gets increase by one so now value is 6, third (++b) the value gets increase by one so now value is 7, fourth (++b) the value gets increase by one so now value is 8.
But the answer is come 26.
Second (++b) the value gets increase by one so now value is 6, third (++b) the value gets increase by one so now value is 7, fourth (++b) the value gets increase by one so now value is 8.
But the answer is come 26.
Hemant Sharma said:
1 decade ago
Let's think what's compiler reads this program.
Since when i<5 it skips the remaining part and when i>=5 it breaks the loop and exit the function. As no print command outside the loop so there is 0 times print, that's it.
Since when i<5 it skips the remaining part and when i>=5 it breaks the loop and exit the function. As no print command outside the loop so there is 0 times print, that's it.
Dhaval Chandnani said:
1 decade ago
When the the loop will run first time the value of x will be '-1'.
As 'if' condition checks whether x<5, this condition will be true as -1<5, and always the loop will be continued, until x is not equals to 5 (x!=5).
When x equals to 5 the 'if' condition get false and the 'else' condition will be executed, in else condition the break statement will be executed and ultimately it will move the control out of the loop resulting into unexecution of 'printf' statement throughout the loop.
As 'if' condition checks whether x<5, this condition will be true as -1<5, and always the loop will be continued, until x is not equals to 5 (x!=5).
When x equals to 5 the 'if' condition get false and the 'else' condition will be executed, in else condition the break statement will be executed and ultimately it will move the control out of the loop resulting into unexecution of 'printf' statement throughout the loop.
Wester9208 said:
1 decade ago
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5) // here this is true for x=-1
continue;//so this is excuted
else //next this is executed
break;//the moment it reaches here, the break //statement gets the control out of for loop
printf("IndiaBIX");//so this statement not printed
}
return 0;
}
------------------------------------------------------
So according to that it executes 0 times.
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5) // here this is true for x=-1
continue;//so this is excuted
else //next this is executed
break;//the moment it reaches here, the break //statement gets the control out of for loop
printf("IndiaBIX");//so this statement not printed
}
return 0;
}
------------------------------------------------------
So according to that it executes 0 times.
Swarup said:
1 decade ago
Since the continue transfers the flow of program at the starting of the loop and break statement transfers the flow of program outside the loop it will be printed for 0 times.
Bulbul said:
1 decade ago
We can not use continue with if statement.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers