C# Programming - Control Instructions - Discussion
Discussion Forum : Control Instructions - General Questions (Q.No. 2)
2.
Which of the following is the correct output for the C#.NET program given below?
int i = 20 ;
for( ; ; )
{
Console.Write(i + " ");
if (i >= -10)
i -= 4;
else
break;
}
Discussion:
20 comments Page 1 of 2.
Sai charan said:
7 years ago
First when it Enter into "forloop"
it prints "i" = "20"
it compares (i>-10) i.e (20>-10) is true
then it changes (i-=4) i.e 20-4=16 again stored in "i"
it prints "i" = "16"
it compares (i>-10) i.e (16>-10) is true
then it changes (i-=4) i.e 16-4=12 again stored in "i"
it prints "i" = "12"
it compares (i>-10) i.e (12>-10) is true
then it changes (i-=4) i.e 12-4=8 again stored in "i"
it prints "i" = "8"
it compares (i>-10) i.e (8>-10) is true
then it changes (i-=4) i.e 8-4=4 again stored in "i"
it prints "i" = "4"
it compares (i>-10) i.e (4>-10) is true
then it changes (i-=4) i.e 4-4=0 again stored in "i"
it prints "i" = "0"
it compares (i>-10) i.e (0>-10) is true
then it changes (i-=4) i.e 0-4=-4 again stored in "i"
it prints "i" = "-4"
it compares (i>-10) i.e (-4>-10) is true
then it changes (i-=4) i.e -4-4=-8 again stored in "i"
it prints "i" = "-8"
it compares (i>-10) i.e (-8>-10) is true
then it changes (i-=4) i.e -8-4=-12 again stored in "i"
it prints "i" = "-12"
it compares (i>-10) i.e (-12>-10) is False
The break statement is Executed then it jumps out of the loop.
it prints "i" = "20"
it compares (i>-10) i.e (20>-10) is true
then it changes (i-=4) i.e 20-4=16 again stored in "i"
it prints "i" = "16"
it compares (i>-10) i.e (16>-10) is true
then it changes (i-=4) i.e 16-4=12 again stored in "i"
it prints "i" = "12"
it compares (i>-10) i.e (12>-10) is true
then it changes (i-=4) i.e 12-4=8 again stored in "i"
it prints "i" = "8"
it compares (i>-10) i.e (8>-10) is true
then it changes (i-=4) i.e 8-4=4 again stored in "i"
it prints "i" = "4"
it compares (i>-10) i.e (4>-10) is true
then it changes (i-=4) i.e 4-4=0 again stored in "i"
it prints "i" = "0"
it compares (i>-10) i.e (0>-10) is true
then it changes (i-=4) i.e 0-4=-4 again stored in "i"
it prints "i" = "-4"
it compares (i>-10) i.e (-4>-10) is true
then it changes (i-=4) i.e -4-4=-8 again stored in "i"
it prints "i" = "-8"
it compares (i>-10) i.e (-8>-10) is true
then it changes (i-=4) i.e -8-4=-12 again stored in "i"
it prints "i" = "-12"
it compares (i>-10) i.e (-12>-10) is False
The break statement is Executed then it jumps out of the loop.
(2)
Vinod Yadav said:
4 years ago
Answer is 20,16,12,8,4,0.
When i=-4 then
Because of if(-4>=-10)
It will not go inside if block
Because of -4 is bigger than -10 so,
Final answer is 20,16,12,8,4,0.
When i=-4 then
Because of if(-4>=-10)
It will not go inside if block
Because of -4 is bigger than -10 so,
Final answer is 20,16,12,8,4,0.
(1)
SHREYAS said:
1 decade ago
All three conditions of for loop is optional.
-12 because after checking for -8 it will decrements it to -12 then print it then check for -12 which break the loop.
-12 because after checking for -8 it will decrements it to -12 then print it then check for -12 which break the loop.
Abhiraj said:
6 years ago
Here it is first printed and it is checked thats why we have -12 also and it comes out from loop.
Sunshine said:
8 years ago
@Latha, It is not 10, but -10 (minus 10). So the answer for sure is C.
AmuthaG said:
8 years ago
While Compilation it shows only from 20 to -12.
Inathi Gqola said:
9 years ago
To the people starting programming, please use curly braces ({}) in your conditional statements. They say you can't teach an old dog new tricks. You have been warned.
Sunil said:
1 decade ago
Can we write break in else part?
Siddhi said:
1 decade ago
Can we write break statement in else?
Latha said:
1 decade ago
Output will be 20, 16, 12, 8:
When var i is 8 then if(i>=10) condition is fail and go to break.
So, How will be the answer is [C].
When var i is 8 then if(i>=10) condition is fail and go to break.
So, How will be the answer is [C].
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers