C# Programming - Control Instructions - Discussion

Discussion :: 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;
}

[A]. 20 16 12 84 0 -4 -8
[B]. 20 16 12 8 4 0
[C]. 20 16 12 8 4 0 -4 -8 -12
[D]. 16 12 8 4 0
[E]. 16 8 0 -8

Answer: Option C

Explanation:

No answer description available for this question.

Charu said: (Jul 9, 2011)  
I think output should be 20 16 12 8 4 0 -4 -8
exclude -12,coz -12 is less than -10

Sekhar Reddy said: (Jul 15, 2011)  
Briefly explain the code please.

Divya said: (Aug 9, 2011)  
What does this for (; ;) means?

Harish said: (Oct 5, 2011)  
(; ;) means that loop will run on the basis of conditional statement inside it.

Gagan said: (Jan 20, 2012)  
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....

Ashok said: (Apr 23, 2012)  
What does this for (; ;) means?
Divya
This is use for infinity loop. it is use in server only.

Vimala said: (Aug 16, 2012)  
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.

Sajila said: (Dec 18, 2012)  
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

Preeti said: (Aug 31, 2013)  
-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.

Mouli said: (Aug 31, 2013)  
What does for(; ;) indicates?

Shreyas said: (Oct 21, 2013)  
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.

Latha said: (Apr 27, 2014)  
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].

Sunil said: (Sep 22, 2014)  
Can we write break in else part?

Siddhi said: (Sep 22, 2014)  
Can we write break statement in else?

Inathi Gqola said: (Jul 30, 2015)  
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.

Amuthag said: (Sep 20, 2016)  
While Compilation it shows only from 20 to -12.

Sunshine said: (Dec 4, 2016)  
@Latha, It is not 10, but -10 (minus 10). So the answer for sure is C.

Sai Charan said: (Jul 26, 2017)  
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.

Abhiraj said: (Jul 3, 2018)  
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:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.