Java Loop not giving Desired results -
i new programming , learning java now. have written below code not gettin correct result :-
public class lifetime { public static void main(string[] args) { int x; for(x = 0; x < 3 ; x++); { int y = -1; //y initialized each time block entered system.out.println("y : " + y); // prints -1 y = 100; system.out.println("y : " + y); } } }
the result of comes :-
y : -1 y : 100
there nor repetitions till 3, why ?
same behavior observed in loop programms have written
can please ?
you have spurious semicolon between for
loop , think body. eliminate , should work expected.
for(x = 0; x < 3 ; x++); { ^ | eliminate
what's happening for
loop looping 3 times , each time executing empty statement formed semicolon. block statement want executed 3 times being executed once.
Comments
Post a Comment