for (int a=0; a<10; a++){
    if(a == 5){
        System.out.println("Hello mr 5!");
    }
}

The above code show both itteration and selection code blocks. In this case the if block is inside the for block which means that it will be repeated. As "a==5" will only be true when, errr, a=5, it means that "Hello mr 5" will be printed once.

Consider the next peice of code.

 

for (int a=0; a<10; a++){
}
if(a == 5){
    System.out.println("Hello mr 5!");
}

This time the if block is outside the for block so it WILL NOT be repeated. The counter a will be 10 when the loop ends so the test a==5 will evaluate to false so will not be displayed.

Being aware of the blocks is very important as it can radically effect the course of your program.

next summary>>

 
Basics
Menu
Search