Flow Of Control

Sometimes you want a desired output from a program depending on various factors. So, you have to use some statements and maintain a particular flow in your program. Java provides such statements which are called program control statements.

Program control statements broadly belong to one of these categories:

  • Selection statements
  • Iteration statements
  • Jump statements

Selection statements:

Selection statements allows the compiler to choose the set of instructions to be executed depending on your given conditions. Java provides you with two types of selection statements:

  • if
  • switch

1. if statements:

● if:

if statement tests a particular condition. If the condition evaluates to true, a course of action is followed. Otherwise, the course of action is ignored.

Syntax: 

if(condition)
{
    statements;
}

Example:

int a=10, b=5;
if(a>b)
{
    System.out.println(a);
}

● if-else:

You can also have else part in if statement. The else part is executed if the condition of if statement evaluates to false.

Syntax:

if(condition)
{
    statements;
}
else
{
    statements;
}

Example:

int a=10, b=5;
if(a>b)
{
    System.out.println(a);
}
else
{
    System.out.println(b);
}

Also, if statement can have another if statement in its body. This type is called nested ifs.

2. switch statement:

Java provides you with a multiple-branch selection statements known as switch. This selection statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed. 

Syntax:

switch(expression)
{
    case constant1: statements1;
                                break;
    case constant2: statements2;
                                break;
    case constant3: statements3;
                                break;
}

Example:

int a=in.nextInt();
switch(a)
{
    case 1: System.out.println(“1”);
                 break;
    case 2: System.out.println(“2”);
                 break;
    case 3: System.out.println(“3”);
                 break;
}

Rules for switch:

  • The data type of expression in switch must be byte, char, short or int. 
  • Use break statement after each case. If you do not, then Java will continue executing statements of following cases until a break statement is found.

Iteration statements:

These statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled. I’ve already explained it in detailed and easy language here: https://www.globalblogs101.com/loops/

Jump statements:

The jump statements transfer program control within a function. Java provides you with three jump statements:

  • return
  • break
  • continue

1. return:

return statement is used to return from a function. And it’s also used in functions to return a value to the function caller. You can use a function to print something or return something. 

If it is not returning anything to its caller then you have to use void keyword, for instance:

public void factorial()

 If it is returning something then specify the data type of it, for instance:

 public int factorial().

2. break:

The break statement is used too skip over a part of the code. A break statement terminates the smallest enclosing while, do-while, for or switch statement. Execution resumes at the statement immediately following the body of the terminated statement.

3. continue:

The continue statement is another jump statement like the break statement as both the statemnet skip over a part of the code. But the continue statement, instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between.

Conclusion:

Now you know how to manipulate the flow of control in your program through jump,iteration and selection statements.