Control Flow Statement

15 0 0
                                    

If statement
- if( condition ){ }
- if the condition is true, the statements inside the braces or the body will be executed.

===============================

class Test1{
public static void main(String[] args){
int x = 5;
int y = 3;
if( y < x ){
System.out.println(y);
}
}
}

================================
Result:
3

The result was 3 since the condition was true. So, expression inside the body of if was executed.

If else statement
- if( condition ){
// statement 1
} else {
// statement 2
}
- it is the same with the if when true but if false, the else body will run which is statement 2 in this case.
- also it is also possible to nest an if else within an if else.

=============================≈===

class Test2{
public static void main(String[] args){
int age = 60;
if( age >= 60 ){
System.out.println("Senior citizen");
} else if( age > 21 && age < 60){
System.out.println("Not that old");
} else {
System.out.println("Still a child");
}
}
}

================================
Result:
Senior citizen

Switch Statement
- it is the same with nested if else statement.
- switch( variable){
case value1:{ }
....
case valueLast:{ }
default: { }
}
- the default is the last option like the else when nothing is true.
- the break keyword is needed on a case when you want to get out of the switch or else it will continue running until it finds the break keyword.

================================

class Test3{
public static void main(String[] args){
String color = "red";
switch( color ){
case "green":{
System.out.println("Go");
break;
}
case "yellow":{
System.out.println("Ready");
break;
}
default:{
System.out.println("Stop!");
break;
}
}
}
}

================================
Result:
Stop!

Since the value of color variable is not one of the cases, it automatically goes to the default, and run the default's block or body.

While loop
    - a loop is running in circles.
    - while( condition ){
         // statement
     }
    - the statement inside the while body or block will keep on looping until the condition becomes false.

=================================

class TestWhile{
    public static void main(String[] args){
        int counter = 0;
        while( counter <= 3 ){
            System.out.println(counter);
            counter++;
        }
    }
}

=================================
Result:
0
1
2
3

So, if use this the value true in the condition, the loop will be infinite.

while( true ){
    // statement
}

Do while loop
    - this is the same with the while loop except the condition is tested last.
    - do{
        // statement
      }while( condition );

for loop
    - syntax:
      for( initialize; condition; increment){
        // statement
      }
    - it is the same with while except, the increment and initialization is done together with the syntax.

================================

class TestFor{
    public static void main(String[] args){
       for( int x=0; x<=3; x++){
           System.out.println(x);
       }
    }
}

=================================
Result:
0
1
2
3

So, if you want an infinite loop, you just remove all the things inside the parenthesis except for semicolon.

for( ; ; ){
   //statement
}

Java ProgrammingWhere stories live. Discover now