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 4 of 15.
Ramnayan said:
1 decade ago
Here for loop executes 06 times when control enters to loop untill i<5 continue keyword is executeddue to which it skips all the code below the continue regain increment the 'i' when i just reach the 5 else is executed and break keyword through the control just outside the loop in this way not printf statement is executed.
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
Adil saifi said:
1 decade ago
True block evaluated continuously tat is (x<5)0,1,2,3,4 until 4<5 when 5<5 condition checked the condition bcm false then the control pass to else block which will break and exit. but if u want to print the given statement just write it upon the break statement it will print 1 time indiaBIX on ur output screen.
Sandip k vaghasiya said:
1 decade ago
Here look at the program x is < 5, the if condition is satisfied and continue is encountered so the loop continue execution without executing the following statements. and when the value of x becomes 5, break is encountered and the control is transferred outside the loop. So 'IndiaBix' will not be printed even once.
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?
Geethapriya V said:
1 decade ago
if(x<5) this condition is true then continue keyword go to next iteration of for loop so loop is execute till (-1 to 4)6th iteration . In 7th iteration x=5 so if the condition(x<5) is false then the break keyword is executed (i.e) break keyword is used to jump out of the for loop . The result is 0 times.
Akshay Kadiyam said:
1 year ago
Since -1<=10 for loop get initialised.
Also, -1 <5 if the condition is true.
The printf("IndiaBIX"); statement gets skipped because of the continue statement that immediately follows it.
Continue statement directly goes to the next iteration skipping the code below it so printf does get executed
Also, -1 <5 if the condition is true.
The printf("IndiaBIX"); statement gets skipped because of the continue statement that immediately follows it.
Continue statement directly goes to the next iteration skipping the code below it so printf does get executed
(9)
Abhishek Kumar said:
1 decade ago
The if block will execute upto x=4, when it is x=5, then the if statement become false and it will go to else block, where break statement is given, the break will terminate the program and takes the control out of the program.
Therefore, there will be no output for this program. Option (C) is correct.
Therefore, there will be no output for this program. Option (C) is correct.
Ashu said:
1 decade ago
Till x is < 5, the if condition is satisfied and continue is encountered so the loop continue execution without executing the following statements. and when the value of x becomes 5, break is encountered and the control is transferred outside the loop. So 'IndiaBix' will not be printed even once.
Akhil said:
10 years ago
I. Break statement "STOP's" executing the loop.
II. Continue will execute the next instruction.
1st iteration: x = -1.
Process: if (x<5) [condition true] so "continue" executes and control goes to "else" where we break the loop and iterate again.
So the statement actually never gets printed.
II. Continue will execute the next instruction.
1st iteration: x = -1.
Process: if (x<5) [condition true] so "continue" executes and control goes to "else" where we break the loop and iterate again.
So the statement actually never gets printed.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers