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 9 of 15.
Utsav said:
1 decade ago
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
printf("\nIndiaBIX");
else
break;
printf("\nIndiaBIX");
}
return 0;
}
Indiabix is printed 12 times. why ?
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
printf("\nIndiaBIX");
else
break;
printf("\nIndiaBIX");
}
return 0;
}
Indiabix is printed 12 times. why ?
Santosh Waghmode said:
1 decade ago
When control is passes to the else block then it found the break statement and the BREAK statement pass the control to the last statement so the message in the printf() function is not printed even one time.
Samaleswari Prasad Nayak said:
1 decade ago
When the condition of for loop is true, the control will enter to the for block. Again when the condition of if is true it will enter the if block. But as it will encounter the continue statement, the control will move to the next step i.e. it will continue its execution. After that when the control will enter to the else block it will encounter the break statement. So control will come out of the loop.
So 0 times is the answer.
So 0 times is the answer.
Suvidha said:
1 decade ago
Else loop is not executed because break statement is encountered first and break statement will terminate you from that else loop thats why nothing gets printed. When your true statement gets executed for 5 times and when true condition fails it comes to else part but in else the first statement is break so it throws you out of the loop.
Ritesh said:
1 decade ago
The code should be like as under
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
continue;
printf("IndiaBIX");
else
break;
}
return 0;
}
And output shd be like that
IndiaBIX
IndiaBIX
IndiaBIX
IndiaBIX
IndiaBIX
#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
continue;
printf("IndiaBIX");
else
break;
}
return 0;
}
And output shd be like that
IndiaBIX
IndiaBIX
IndiaBIX
IndiaBIX
IndiaBIX
Ashutosh chaturvedi said:
1 decade ago
For x= -1 to x=4 in for loop the control will be passed to if statement and if statement contain the continue statement and the purpose of continue statement is to redirect the control to the beginning of the loop so printf statement is not executed. And for x=5 to x=10 the control will be passed to else statement containing the statement break which will redirect the control out of the loop so, here as well printf will not get executed. So, overall "indiabix" will be printed 0 times.
Satti babu said:
1 decade ago
It will continue every time when x<5 that means it goes into again starting loop then when it is greater than 5 it will break that means exit from loop and gives out put 0 times.
Samikshya said:
1 decade ago
thanks to everyone
RAJASHEKER.T said:
1 decade ago
Whenever x=5, it goes to else part.
In that else part break statement is there.
So break statement, breaks the forloop and come out of the loop.
So there is no output.
In that else part break statement is there.
So break statement, breaks the forloop and come out of the loop.
So there is no output.
Kalaivani said:
1 decade ago
Else statement should not be followed by break;.
If break is used in else statement it returns nothing.
If break is used in else statement it returns nothing.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers