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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -