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 10 of 15.
Jajanpreet Singh said:
1 decade ago
ITS SIMPLE:
As soon as x reaches value 5.
The block with "break" is executed which cause the compiler to abandon the loop and HENCE ANY OF ITS FURTHER ITERATIONS (EVEN THE STATEMENT AFTER BREAK IN SAME BLOCK).
As soon as x reaches value 5.
The block with "break" is executed which cause the compiler to abandon the loop and HENCE ANY OF ITS FURTHER ITERATIONS (EVEN THE STATEMENT AFTER BREAK IN SAME BLOCK).
Ramsan said:
1 decade ago
After the break statement, It will come out of the loop. Then it will not going to print IndiaBix.
Dharmesh said:
1 decade ago
For printing the statement either it should be in if or in else block.
ANJUM said:
1 decade ago
There is no {} for if or else, so the statement next to if is continue which starts the next iteration when if condition becomes false control moves to else then to break which takes the control out of loop and program terminates.
Mansi sharma said:
1 decade ago
x is declared as integer so it can not be initialised as x=-1 so program won't run.
Arnav Bijalwan said:
1 decade ago
Continue statement transfers control to next iteration. Hence when condition (x<5) gets true, control jumps to next iteration. When x becomes 5, condition in if statement becomes false and it executes else part where it encounters break statement and jumps out of loop.
Hence control never encounters the printf statement and thus, it is executed 0 times. Hence the option C is correct.
Hence control never encounters the printf statement and thus, it is executed 0 times. Hence the option C is correct.
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers