If Statements

You use the if statement to test simple conditions with only a few possible outcomes. They syntax for the if statement is

Execute a single line of code with an if statement

if(boolean expression)
    //This line of code will run if the condition is true

Execute multiple lines of code

if(boolean expression){
    // All the code from the beginning of the { to the end of the }
    // will be executed, when the boolean expression
    //evaluates to true.
}

if

int temperatureInDegrees = 100;

if(temperatureInDegrees == 100){
    //Its now at boiling point
}

if-else

int temperatureInDegrees = 100;

if(temperatureInDegrees <= 0){
    //Its very cold, its below freezing point
}else{
    // Its not below freezing point
}

if-else if-else

boolean enteredDoorCode = true;
boolean passedRetinaScan = true;

if(enteredDoorCode){
    // Allow entry
} else if(passedRetinaScan){
    // Allow entry
} else {
    //Deny access
}

results matching ""

    No results matching ""