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)
Sajila said:
1 decade ago
i=20;
First print value of i(i.e: 20)
Check (i.e:20>=-10) true
Then value of i is 16 and print it(i.e., : i=20-4=16)
Then check as 16>=-10 true
Print 12(i=16-4)
Check 12>=-10 true
Print 8(i=12-4)
Check 8>=-10 true
Print 4(i=8-4)
Check 4>=-10 true
Print 0(i=4-4)
Check 0>=-10 true
Print -4(i=0-4)
Check -4>=-10 true
Print -8(i=-4-4)
Check -8>=-10 true
Print -12(i=-8-4)
Check -12>=-10
It is false
Then print the output as :20 16 12 8 4 0 -4 -8 -12
First print value of i(i.e: 20)
Check (i.e:20>=-10) true
Then value of i is 16 and print it(i.e., : i=20-4=16)
Then check as 16>=-10 true
Print 12(i=16-4)
Check 12>=-10 true
Print 8(i=12-4)
Check 8>=-10 true
Print 4(i=8-4)
Check 4>=-10 true
Print 0(i=4-4)
Check 0>=-10 true
Print -4(i=0-4)
Check -4>=-10 true
Print -8(i=-4-4)
Check -8>=-10 true
Print -12(i=-8-4)
Check -12>=-10
It is false
Then print the output as :20 16 12 8 4 0 -4 -8 -12
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)
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.
Vimala said:
1 decade ago
ashok (;;) means there is no condition, it's of no use in that ,it will execute as long the if condition satisifes, then it will come out because of break statement.
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.
Gagan said:
1 decade ago
Starting from 20,
i = i-4
i = 20-4=16,
i=16-4=12,
i=12-4=8,
i= 8-4=4,
i= 4-4=0,
i= 0-4=-4,
i= -4-4=-8,
i=-8-4=-12
So ,,,20,16,12,8,4,0,-4,-8,-12....
i = i-4
i = 20-4=16,
i=16-4=12,
i=12-4=8,
i= 8-4=4,
i= 4-4=0,
i= 0-4=-4,
i= -4-4=-8,
i=-8-4=-12
So ,,,20,16,12,8,4,0,-4,-8,-12....
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].
Preeti said:
1 decade ago
-12 will also print because condition has been checked after print statement. That's why O/P will be 20 16 12 8 4 0 -4 -8 -12.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers