Can any 1 explain me how this is..? Give the ans too. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can any 1 explain me how this is..? Give the ans too.

int i=3; for(i=5; i<i*i; i--) { System.out.println(I);}

17th Apr 2017, 4:45 PM
SaNtO SaNdY☣
SaNtO SaNdY☣ - avatar
8 Answers
+ 9
From where did you get this code? The first assignement is unnecessary, because on second line you reassign a new value ( 5 instead 3 ) to 'i'... And the loop defined on second line is a complex declaration loop also unnecessary, because it's equivalent to: 'for (i=5; i!=1; i--)', as testing 'i<i*i' is always true while i is different of one ^^ Anyway, the loop print an undeclared/unassigned variable 'l' five times ( first i==5, second 4... finally 2, stopped when i==1 ).
17th Apr 2017, 4:54 PM
visph
visph - avatar
+ 5
@Visph it's a Java challenge quizz, hence the unnecessary assignment 😉
17th Apr 2017, 7:34 PM
Nikolay Nachev
Nikolay Nachev - avatar
+ 4
@Rrestoring faith: You're right... I've written too quick @@ ( previous post edited ;) )
17th Apr 2017, 5:09 PM
visph
visph - avatar
+ 4
@Siddahart Upadhyay answer isn't right, as there is 2 variables in the example, 'i' and 'l', not only one "l" ^^
18th Apr 2017, 3:46 AM
visph
visph - avatar
+ 1
Thank you all ...! now i got it .
18th Apr 2017, 3:43 AM
SaNtO SaNdY☣
SaNtO SaNdY☣ - avatar
+ 1
then the program should show an error due to uninitialized variable I so no output for this question
18th Apr 2017, 7:50 AM
Siddharth Upadhyay
Siddharth Upadhyay - avatar
0
@Visph the loop stops when i is 1, not 0.
17th Apr 2017, 5:04 PM
Rrestoring faith
Rrestoring faith - avatar
0
Output for this code will be 5432 because I=5 checks the condition and it's satisfies and print it and the process continues till 1 and stop because it happen like this 5<25 True 4<16 true 3<9 true 2<4true 1<1 false
17th Apr 2017, 5:30 PM
Siddharth Upadhyay
Siddharth Upadhyay - avatar