Pages

Sunday, June 10, 2012

If conditions


In this lesson we are going to learn how to use the If coniditions. The If condition is used to decide where the execution of the code must go. Inside the If condition the is a logical condition which decide if the code will be executed inside the If condition or if it goes out. Then, the If condition can have an Else-If to catch the next execution, or it can have a final Else to execute the default code.

Example:
int a = 3, b = 4;
if (a == b){ //the following code will not be executed
System.out.println("a is equal to b");
}
else if (a > b){ // the following code will be executed
System.out.println("a is greater than b");
}
else
System.out.println("a is less than b");
/* the code before will not be executed. By the way, this code is considered inside the else, although not having braces. In those cases, only the next sentence will be executed */

No comments:

Post a Comment