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 3 of 15.
Purushottam kumar said:
1 decade ago
Here inside the program x is < 5, the "if" condition is true and continue is encountered so the loop continue inside 'for' execution without executing the following statements. and when the value of x becomes 5, 'break' is executed and the 'for' loop will be terminated , So 'IndiaBix' will not be printed even once because 'printf' will never executed.
Anita said:
1 decade ago
The "Continue" Statement Takes the control to the beginning of the loop,So When ever value of if(x<5) continue appreas,control goes to begining of loop and value of X is incremented.
"Break" Satatement takes the control out of the loop.So when it is occured,control is taken out of the Loop.
Thus "IndiaBix" Will never get printed.
"Break" Satatement takes the control out of the loop.So when it is occured,control is taken out of the Loop.
Thus "IndiaBix" Will never get printed.
Uday kumar said:
1 decade ago
@Murugesan @Vipin dhawan.
I has little bit query you guys answered for @Rohit question.
When it is b++ there will be no change in the value.
for ++b the value get increased by 1.
OK.
Let us take you are right for a while then,
for(i=-1;i<10;i++)
In this case what will be occur? I mean what will happens here for i++ whether it will increase or won't change.
I has little bit query you guys answered for @Rohit question.
When it is b++ there will be no change in the value.
for ++b the value get increased by 1.
OK.
Let us take you are right for a while then,
for(i=-1;i<10;i++)
In this case what will be occur? I mean what will happens here for i++ whether it will increase or won't change.
Sayali said:
1 decade ago
continue will transfer control back to loop statement for i = -1 to 4 since it satisfies condition if (i<5), for i=5 it will satisfy condition in the else part i.e.(!(i<5)) control will go to break statement and break statement will stop executing loop and will come out of loop and will not print indiabix
Raju Naidu said:
1 decade ago
Hai,
The keyword 'continue' is used to skip the some statements and continuing the current process. In our programm till the x=5 it continuing the process once it's reached to 5 if condition getting false so else block will be execute but in else block we had break statement, so it breaks the loop and it will never go to print the indiabix so answer is 0 times.
The keyword 'continue' is used to skip the some statements and continuing the current process. In our programm till the x=5 it continuing the process once it's reached to 5 if condition getting false so else block will be execute but in else block we had break statement, so it breaks the loop and it will never go to print the indiabix so answer is 0 times.
Pranab Nandi said:
1 decade ago
The condition x<5 is true until x=5, so the true block executed. The code after the continue statement(including printf("IndiaBIX");) will not executed and go to increment step(x++). Again the loop is continued.
When x=5, then else block executed, it has break statement, So loop is terminated.
Thus "IndiaBix" Will never get printed.
When x=5, then else block executed, it has break statement, So loop is terminated.
Thus "IndiaBix" Will never get printed.
Owais dar said:
1 decade ago
The value of x starts from -1 and is incremented everytime till x<5. becux the continue statement sends the control back to increment process. And once the value of x reachs 5 it executes else part, which is break statement, break statement terminates loop. This means the control will never encounter printf statement. Hence no 'IndiaBix' will be printed.
Dhivakar said:
1 decade ago
Untill x<5 the 'if' part gets executed,which contains the 'continue' statement and it simply transfers the control to the begining of the loop iteratively for 6 times....
and now x=5 the control passes to the else part which contains the 'break' statement before printf and hence the loop gets terminated before printing "indiabix"
and now x=5 the control passes to the else part which contains the 'break' statement before printf and hence the loop gets terminated before printing "indiabix"
Rohit 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.so that the ans c is rite
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers