Control flow statements

Control flow statements, would change or break the flow of execution by implementing decision making, looping, and branching your program to execute particular blocks of code based on the conditions.
Control flow statements in Java allow you to run or skip blocks of code when special conditions are met. There are 3 types of control flow statements supported by the Java programming language.

Decision-making: if-then, if-then-else, if else if, nested if else and switch
Looping: for, while, do-while
Branching: break, continue, return

In this post we will mainly discuss about Decision-making statements.

Java if statements:

1. If-then statement

The “if” statement in Java works exactly like in most programming languages. With the help of “if” you can choose to execute a specific block of code when a predefined condition is met.

Syntax:

if (expression) {
   // statements
}

Here expression is a boolean expression (returns either true or false).

If the expression is evaluated to true, statement(s) inside the body of if (statements inside parenthesis) are executed.

If the expression is evaluated to false, statement(s) inside the body of if are skipped from execution.

Example:

public class IfThenDemo {
public static void main(String[] args) {
int age = 2;
System.out.println("James age is " + age + " years old");
if (age < 3) {
System.out.println("James is an Infant");
}
}
}

Output:

James age is 2 years old
James is an Infant

2. The if-then-else Statement:

The if-then-else statement provides a alternate path of execution when an if clause evaluates to false.

Syntax:

The syntax of if-then-else statement is:

if (expression) {
   // codes
}
else {
  // some other code
}

Example:

public class IfThenElseDemo {
public static void main(String[] args) {
int A= 4;
                int B= 7;
if (A > B) {
System.out.println("A is Greater than B");
}
                else{
                        System.out.println("B is Greater Number");
                     }

}
}

Output:

B is Greater Number


3. If else If Statement

In Java, it's possible to execute one block of code among many. For that, you can use if..else...if ladder.

Syntax:

if (expression1)
{
   // codes
}
else if(expression2)
{
   // codes
}
else if (expression3)
{
   // codes
}
.
.
else
{
   // codes
}

Example:

public class FlowControlExample {
public static void main(String[] args) {
int age = 17;
System.out.println("James is " + age + " years old");
if (age < 4) {
System.out.println("James is a baby");
} else if (age >= 4 && age < 13) {
System.out.println("James is a child");
} else if (age >= 13 && age < 20) {
System.out.println("James is a teenager");
} else if (age >= 20 && age < 60) {
System.out.println("James is an adult");
} else {
System.out.println("Jamesr is an old men");
}
}
}

Output:
James is 17 years old
James is a teenager


4. Nested if..else Statement

It's possible to have if..else statements inside a if..else statement in Java. It's called nested if...else statement.

5. Switch Statement:

In Java, the if..else..if ladder executes a block of code among many blocks. The switch statement can a substitute for long if..else..if ladders which generally makes your code more readable.

Syntax:

switch (variable/expression) {
case value1:
   // statements
   break;
case value2:
   // statements
   break;
   .. .. ...
   .. .. ...
default:
   // statements
}


class SwitchDemo {
    public static void main(String[] args) {
        int age = 2;
        String yourAge;
        switch (age) {
            case 1:  System.out.println("You are one year old");
                     break;
            case 2:  System.out.println("You are two year old");
                     break;
            case 3:  System.out.println("You are three year old");
                     break;
            default: System.out.println("You are more than three year old");
                     break;
        }
    }
}

Output:

You are two year old

To get more details please watch below youtube video and Subscribe the channel.


No comments:

Post a Comment