Habenero Java

Click here for the previous tutorial.

The If statement is an essential part to programming. This allows you to make a basic test. It works fairly similarly to the inline if afore mentioned. In the case of a regular if statement, you will declare the test to be performed, and then inside of a code block what will be executed if true.

if(booleanTest){

}

In this case, if booleanTest were true, it would execute whatever is in between the braces. If it were false, it would ignore the block and skip to the closing brace.
The test can be used with more than just boolean type variables. For example, let's suppose we are making a game, in which the user controls a character inside of ouyr virtual world. He may have an int type variable controlling the health in a range from 0 to 100. We could formulate a test, and check everytime the user gets hit if he/she is dead, by checking the value of the health.

if(playerHealth <= 0){
    System.out.println("You died!");
}

In this case, it returns a boolean type value, without us having to make a new variable for it. The logical operator <= takes care of all of the calculation for us.

What about in the case that the player is not dead? We may then want to inform them how much damage they took. We could test for this with another if statement, using (playerHealth > 0), but there is an even easier way.

if(playerHealth <= 0){
    System.out.println("You died!");
}else{
    System.out.println("You took " + enemyAttack + " damage!");
}

Using the else tag, we can state exactly what should happen if the if statement proves false. Additionally, we can create what are called "if else statements". With these, we can check if the first statement is true, if not then check something else, if not that for something else, add infinitum, until eventually (and optionally) ending with an else statement as the default.

if(playerHealth <= 0){
    System.out.println("You died!");
}else if(playerWeaponEquipped != true){
    System.out.println("You took " + enemyAttack + " damage!");
    System.out.println("You lost your " + weapon);
    weapon = 0;
}else{
    System.out.println("You took " + enemyAttack + " damage!");
}

With this, we can check if the player died, then if the player was disarmed in the fight.

With the use of if else statements, a programmer can check for multiple conditions simultaineously. However, in many cases there is a more efficient method of doing this. Let us suppose we have made a dungeon that requires a secret word to pass, only instead of there being one word, there are six, that all lead to different places. We can ask the user to tell us the password (The actual method for user input will be discussed next tutorial), then use a switch to check for all six words at the same time, and set up a defualt incase none are said. With an if esle statement, this would take a significant amount of typing,but with a switch it is very simple.

switch(password){
case "chaos": door = "Downwillow Hollows";
    break;
case "anteater": door = "Chivilar Steppes";
    break;
case "cyber hunt": door = "Keplar 32-b";
    break;
case "crowns": door = "King's Piece";
    break;
case "grass": door = "Sliding Ravine";
    break;
case "legacy": door = "Templar Courtyard";
    break;
case "leave": door = "Dungeon Hallway";
    break;
default: door = "Dungeon Gate";
    break;

With this we can check if it is equal to any of the seven specific passwords (Note we used seven, as one was used in order to leave the gates into the dungeon hallway). You can read it as "switch(variable)" as "checking (variable)" and "case (value)" as "In the case (variable) is equal to (value)". The break statement simply tells the compiler that you have found the case you are looking for, and you are done with the switch; when it hits the break, it will jump out of the switch and continue with the rest of the program. The default statement is like the ending else statement of the if else group we had made earlier: if all else fails, execute whatever is here. In this case, if none of the case statements had been matched, the player would remain in the dungeon gate room, per the default statement that we declared.

Finally, we can assume that, given enough players, someone will become frustrated with your game when they are trying to type "chaos", but they keep typing "Chaos". In Java, these are not recognized as the same, and their computer is not going to be forgiving. We can be a bit curteous to our fans, with a function embedded in Java, toLowerCase(). Simply attach it to the end of the String containing their guess with a dot.

switch(password.toLowerCase()){

This will remove any capitol letters within the String and replace them with lowercase letters. Meaning if a player were to type "ChAos" it would be equally as valid as "chaos".

Click here to coninue to the next tutorial.