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 2 of 2.
Mouli said:
1 decade ago
What does for(; ;) indicates?
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.
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
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.
Ashok said:
1 decade ago
What does this for (; ;) means?
Divya
This is use for infinity loop. it is use in server only.
Divya
This is use for infinity loop. it is use in server only.
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....
Harish said:
1 decade ago
(; ;) means that loop will run on the basis of conditional statement inside it.
Divya said:
1 decade ago
What does this for (; ;) means?
Sekhar reddy said:
1 decade ago
Briefly explain the code please.
Charu said:
1 decade ago
I think output should be 20 16 12 8 4 0 -4 -8
exclude -12,coz -12 is less than -10
exclude -12,coz -12 is less than -10
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers