June 2018
Beginner
722 pages
18h 47m
English
The if...else construct allows executing a certain statement or block if the expression evaluates to true; otherwise, another statement or block is executed:
if(Boolean expression){ //do something} else { //do something else}
Here are two examples:
int x = 1, y = 1; if(x == y){ System.out.println("x == y"); //prints: x == y x = y - 1;} else { System.out.println("x != y"); }if(x == y){ System.out.println("x == y");} else { System.out.println("x != y"); //prints: x != y}
You can see how easy it is to read this code when the braces {} are used consistently. And, as in the previous case of a simple if statement, each block can have another nested block with another if statement, and so on – as many blocks and as deeply nested as ...
Read now
Unlock full access