The statements that are used to control the flow of a program are called control statements. Control Statements are very important and basic in any programming language. Through control statements, we implement real-world scenarios in programs or software. There are different types of control statements in Java for different conditions.

Types of Control statements in JAVA

There are three types of control statements in java as given below.

  • Decision-Making statements
    • if statements
    • switch statement
  • Loop statements
    • do while loop
    • while loop
    • for loop
    • for-each loop
  • Jump statements/Branch Statements
    • break statement
    • continue statement

Decision-Making statements

 A piece of code based on some condition is called a Decision-making statementIF Statements execute a piece of code if the condition is true. Switch statements shift the control of the flow to a specific point in the code. There are four types of decision-making statements in Java: 

  1. Simple if statement
  2. if-else statement
  3. if-else-if ladder
  4. Nested if-statement
  1. if Statement

The If statement in java as well as in all programming languages is the simplest control statement. If statements decide whether a particular block of code will be executed or not in the program. If the condition is true, then the block of code is executed otherwise not.

General Syntax of the if statement is given below.

if(condition) {    
statement 1; //executes when the condition is true   
}    

1st of all if statement checks the given condition if the condition is true, then the code or block of codes inside the if block is executed. If the condition is false control shifts to the next statement in the program. Block of codes is written in curly brackets If we do not use curly brackets after the if statement then only the immediate statement is executed.

if(condition)
   statement1;
   statement2;

in the above program, only statement 1 will be executed only if the condition is true. Statement 2 will execute irrespective of whether the condition is true or false.

Example:

Advertisements
Advertisements
Advertisements