Massive | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Massive

what values ​​will the algorithm give and say why? int result = 0; for(int i = 0; i < 5; i++); if(i == 3); result += 10; else result +=i; System.out.println(result)

14th Dec 2017, 12:27 PM
MARKO
MARKO - avatar
5 Answers
+ 10
Code will give error first of all, since the for loop has more than one statement it should have curly braces enclosing all its statements secondly the if statement will give an error because "i" exists only in the for loop thirdly placing a semicolon after the if statement terminates it so "result += 10" executes irrespective of whether the if condition is true or false
14th Dec 2017, 12:38 PM
David Akhihiero
David Akhihiero - avatar
+ 6
int result = 0;//assigned 0 to result for(int i = 0; i < 5; i++); if(i == 3) result += 10; else result +=i; System.out.println(result) for i=0 else part evaluate and result+=0=0 for i=1 else part evaluated and result+=1=1 for i=2 else part evaluate and result+=2=3 for i=3 if part evaluate and result+=10=13 for i=4 else part evaluate and result+=4=17 so output become 17 but it is only when the semicolon is not present after if statement
14th Dec 2017, 1:06 PM
GAWEN STEASY
GAWEN STEASY - avatar
0
yes, its lessons Java, and answer this it algorithm 17, Whyyyy?
14th Dec 2017, 12:41 PM
MARKO
MARKO - avatar
0
thanx
14th Dec 2017, 1:09 PM
MARKO
MARKO - avatar
0
thx
14th Dec 2017, 1:12 PM
MARKO
MARKO - avatar