IfElse

If/Else
An if/else statement in programming is simply a question: 'If this is true, do this, otherwise, do this. It often comes in two parts but can be extended and nested. There is also a one line if statement supported in most programming languages. It has the following format: bool ? result1 : result2; It is often used to assign a value to something and can be chained.

In Java
code format="java" public class Main{ public static void main(String[] args){ String out; int x = 5; if(x < 3){ //this returns false so it goes to the else out = "less than 3"; }else{ //nested if statement if(x > 5){ //this also returns false so it goes to the else out = "greater than 5"; }else /*chained else if*/ if(x == 5){ //this returns true so it executes out = "its 5"; }else{ //this is skipped out = "nope"; }       }        System.out.println(out);

//one line if       int y = (x == 5) ? 4:5; //this returns true so y will be set to 4 //chained one line if       y = (x == 1) ? 2:           (x == 2) ? 3:           (x == 3) ? 4:           (x == 4) ? 5:           (x == 5) ? 6:10;       //y will be set to 6 } } code

In C++
code format="cpp"
 * 1) include

int main{ int age = 19; if(age < 18){ //this returns false, go to else printf("can't vote, can't drink"); }else /*chain else if*/ if(age < 21){ //this returns true execute printf("can vote, can't drink"); }else{ //this is skipped printf("can vote, can drink"); } } code