+ 1

int result = 0; for (int i = 0; i < 5; i++) { if (i == 3) { result += 10; } else { result += i; } }

Explain this question. how the ouptut is coming

11th Sep 2016, 4:16 AM
Syed Maqsood
Syed Maqsood - avatar
2 Respostas
+ 1
It will loop 5 times. When the value of i is 3, add 10 to the result, otherwise add the value of i. int result = 0; for ( int i = 0; i < 5; i++ ) { if ( i == 3 ) { result += 10; Console.WriteLine( "i={0}, i is equal to 3 so add 10. Result={1}", i, result ); } else { result += i; Console.WriteLine( "i={0}, i isn't equal to 3 so add i. Result={1}", i, result ); } } Console.WriteLine( "RESULT: {0}", result ); i=0, i isn't equal to 3 so add i. Result=0 i=1, i isn't equal to 3 so add i. Result=1 i=2, i isn't equal to 3 so add i. Result=3 i=3, i is equal to 3 so add 10. Result=13 i=4, i isn't equal to 3 so add i. Result=17 RESULT: 17
11th Sep 2016, 4:19 PM
Liam
Liam - avatar
+ 1
Hint: (i=0) 0 = 0 + i (i=1) 1 = 1 + i (i=2) 2 = 2 + i (i=3) 3 = 3 + 10 (i=4) 13 = 13 + i = result
11th Jan 2018, 1:03 PM
Kate
Kate - avatar