int i = 4; int j = 6; for(i=0; i<5;i++){ if(i>2){ System.out.println("i = "+ i); for(j=j;j<10;j++){ System.out.println("j = "+ j); }} System.out.println("Enter a number for sum: "); int cash= input.nextInt(); do{ int sum= cash +1; System.out.println("the sum is: " + sum); }while( cash<5); } } } Output Enter a number for sum: THIS IS MY OUTPUT AND I AM NOT SURE THAT WH
THIS IS MY OUTPUT AND I AM NOT SURE THAT WHY IS THE FIRST PART i=0 ... j=6 .... not printed Can any of you guys help me understand this and is there any good resource from where i would be able to get all this concepts.
It should skip printing the value of i when i=0, 1 and 2 since you have the if statement that requires i to be greater than 2.
First of all: What do want to achieve with this code snippet?
Maybe this will help: //This does not make any sense at all, cause you //re-init the 'i' with 0 in line 3 (for-loop) int i = 4; //This will make your second for-loop start at j=6 int j = 6; //This will run for i=0 till i=4 for(i=0; i<5;i++) { //This will only be executed for i=3, i=4 if(i>2) { //Therefore this will print out: // i=3 // i=4 System.out.println("i = "+ i); //This loop will run for j=6 till j=9 for(j=j;j<10;j++) { //Therefor this will print out: // j=6 // ... // j=9 System.out.println("j = "+ j); } } //This part will run 5 times (i=0 -> i=4) System.out.println("Enter a number for sum: "); int cash= input.nextInt(); //This loop only runs for inputs 0, 1, 2, 3, 4 //Output depends on the number you enter for cash do { int sum= cash +1; System.out.println("the sum is: " + sum); } while(cash<5); }
Join our real-time social learning platform and learn together with your friends!