Programming





Control Statements in Java

Control Statements in Java


by Administrator | 29-Mar-2020
Java

Tags:  Core Java  Control Statement  Loops

Share: 


Control statements are used to control the flow of the program based on certain conditions. There can be situations like the user needs to run the same code multiple times or a few lines of code to be run only when a number is odd etc. These are various types of control statements in Java:

  • Conditional control statement
    1. if statement
    2. switch statement
  • Looping/iteration control statement
    1. for loop
    2. while loop
    3. do-while loop
  • Unconditional control statement
    1. Break statement
    2. Continue statement

Conditional control statement: Conditional control statement is used to execute some statement(s) based on some condition. Examples of conditional statement are:

  • if statement is of various types:
    1. Simple if statement: The block inside the if block will be executed only if the condition is true.

Syntax:

if (expression) {

Statement;

}

 2. if-else statement : If the Boolean expression results in true then if block will execute. If the Boolean expression is false then the code inside else block will execute.

Syntax:

if (expression) {

Statement;  

}else{

Statement;  

}

3. Nested if statement: If there are more than one condition for a statement to be executed then nested if statement can be used.

Syntax:

if (expression) {

        if (expression) {

Statement;  

}

}else{

Statement;

}

4. if else ladder: This statement can be used when same value has to be compared with more than one condition.

Syntax:

if (expression) {

Statement  

} else if (expression) {

Statement;

}else if(expression){

Statement;

}else {

 Statement;

}

  • switch statement: Switch statements are used when there are a number of conditions to be tested. if else ladder code can also be achieved using switch statement with lot more readability and less tedious code.

For e.g. we need to write code depending on days of a week. Writing that code using if else will be very tedious, confusing and less readable.

Syntax:

switch (expression) {

case 1: Statement

case 2: Statement

default: Statement

}

Rules of using switch statements

  • Switch statements can only evaluate to byte, short, int, char or enum data types.
  • There is no limit on number of case statements.
  • The constant expression of a case must be a literal variable, final variable or a constant.
  • If a case condition matches a case constant, no other case statement will be tested, and all statements following that case will be executed until a break statement or end of the switch block is reached. This rule is applicable for default as well.
  • The default block is optional. If there is any code which needs to be executed if none of the cases are matches then that code goes in default.
  • The datatype of the parameter for switch statement should be same or compatible as case statement.
  • There can be only one default statement. It can be used at any place within the switch block but it is recommendable to use the default statement after all the case statements for readability.
  • To terminate the execution of the switch statement, break statement can be used.

Looping/iteration control statement

This statement is used to execute a block of code multiple times based on some condition.

  • For statement

It is the most famous and widely used statement. It is used to loop a code for a number of times. All that code can be accommodated in one line.

Syntax:

for (initialization; condition; iteration){

 statement;

}

You can initialize multiple variables in the initialization and iteration part of for statement.

  • While statement

While statement can be used when the number of times in not known to the user.

Syntax:

while (expression) {

statement;

}

  • do-while statement

do while is same as while statement but it will run atleast once.

Syntax:

do{

statement;

} while (expression);

Unconditional control statement

  • Break statement

break is a keyword in java. It is used to terminate the execution of the current switch block or looping statement. Break statement can only be used within a switch or loop statement.

  • Continue statement

continue is also a keyword. It is used inside any looping statement. Continue statement can be used to skip some statement from the block of the looping statement.

In the case of for statement, the control will be transferred to the iteration statement. In the case of while or do-while the control will be transferred to the conditional statement.


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.