4 Control Statement in Java New

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Control statements are the statements that change the flow of execution of a

program. Such statements are called control flow statements. It is one of the fundamental
features of Java, which provides a smooth flow of program.

Java provides three types of control flow statements.

1. Decision Making statements


o if statements
o if else statement
o Nested if else statement
o switch statement
2. Loop statements
o do while loop
o while loop
o for loop
o for-each loop
3. Jump statements
o break statement
o continue statement

Decision-Making statements:
Decision-making statements decide which statement to execute and when. Decision-
making statements evaluate the Boolean expression and control the program flow
depending upon the result of the condition provided. Types of decision making statement
as following.

1) Simple if statement:

It is the most basic statement among all control flow statements in Java. It evaluates a
Boolean expression and enables the program to enter a block of code if the expression
evaluates to true.

Syntax of if statement is given below.

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

Consider the following example in which we have used the if statement in the java code.

Student.java

Student.java

1. public class Student {


2. public static void main(String[] args) {
3. int x = 10;
4. int y = 12;
5. if(x+y > 20) {
6. System.out.println("x + y is greater than 20");
7. }
8. }
9. }

Output:

x + y is greater than 20

2) if-else statement

The if-else statement is an extension to the if-statement, which uses another block of
code, i.e., else block. The else block is executed if the condition of the if-block is evaluated
as false.

Syntax:

1. if(condition) {
2. statement 1; //executes when condition is true
3. }
4. else{
5. statement 2; //executes when condition is false
6. }
Write a program to enter value of x and check number is even or old using scanner class.

Student.java

Import java.util.scanner
public class Student {
1. public static void main( String[],args)
2. { scanner sc=new scanner(System.in);
3. Int x;
4. System.out.println(“ Enter value of x”);
5. X=sc.nextInt();
6. If( x%2==0)
7. { System.out.println(“Number is odd”); }
8. Else
9. { System.out.println(“number is even”); }
10. } }

Output:

x + y is greater than 20

3) if-else-if ladder:

The if-else-if statement contains the if-statement followed by multiple else-if statements.
In other words, we can say that it is the chain of if-else statements that create a decision
tree where the program may enter in the block of code where the condition is true. We
can also define an else statement at the end of the chain.

Syntax of if-else-if statement is given below.

1. if(condition 1) {
2. statement 1; //executes when condition 1 is true
3. }
4. else if(condition 2) {
5. statement 2; //executes when condition 2 is true
6. }
7. else {
8. statement 2; //executes when all the conditions are false
9. }

Consider the following example.

import java.util.Scanner;
class Test
{
// main() method
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a percentage");
float per = sc.nextFloat();
if(per>=70)
{
System.out.println("Distinction");
}else if(per>=60)
{
System.out.println("First Class");
}else if(per>=50)
{
System.out.println("Second Class");
}else if(per>=40)
{
System.out.println("Pass");
}
else
{
System.out.println("Failed");
}
}
}

4. Nested if-statement

In nested if-statements, the if statement can contain a if or if-else statement inside


another if or else-if statement.

Syntax of Nested if-statement is given below.

1. if(condition 1) {
2. statement 1; //executes when condition 1 is true
3. if(condition 2) {
4. statement 2; //executes when condition 2 is true
5. }
6. else{
7. statement 2; //executes when condition 2 is false
8. }
9. }

Consider the following example.

public class Test {

public static void main(String args[]) {


int x = 30;
int y = 10;

if( x < 30 ) {
System.out.print("X < 30");
} else {
if( y > 9 ) {
System.out.print("X > 30 and Y > 9");
}
}
}

Switch Statement:
Switch statements are similar to if-else-if statements. The switch statement contains
multiple blocks of code called cases and a single case is executed based on the variable
which is being switched. The switch statement is easier to use instead of if-else-if
statements. It also enhances the readability of the program.

Points to be noted about switch statement:

o The case variables can be int, short, byte, char, or enumeration. String type is also
supported since version 7 of Java
o Cases cannot be duplicate
o Default statement is executed when any of the case doesn't match the value of
expression. It is optional.
o Break statement terminates the switch block when the condition is satisfied.
It is optional, if not used, next case is executed.
o While using switch statements, we must notice that the case expression will be of
the same type as the variable. However, it will also be a constant value.

The syntax to use the switch statement is given below.

1. switch (expression){
2. case value1:
3. statement1;
4. break;
5. .
6. .
7. .
8. case valueN:
9. statementN;
10. break;
11. default:
12. default statement;
13. }

Consider the following example to understand the flow of the switch statement.

package switchcase;
import java.util.Scanner;

public class Switchcase {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
int day;
System.out.println("Enter value of day");
day=sc.nextInt();
switch(day)
{ case 1 -> System.out.println("sunday");
Break;
case 2 -> System.out.println("Monday");
break;
case 3 -> System.out.println("tuesday");
Break;
case 4 -> System.out.println("wednesday");
Break;
case 5 -> System.out.println("thusday");
Break;
case 6 -> System.out.println("friday");
Break;
case 7 -> System.out.println("saturday");
Break;
default -> System.out.println("invalid day");
}
}
}

Loop Statements
Loops in Java is used to execute a particular part of the program repeatedly if a given condition
evaluates to be true. In Java, we have three types of loops that execute similarly. However,
there are differences in their syntax and condition checking time.

1. for loop
2. while loop
3. do-while loop

Let's understand the loop statements one by one.

1) for loop
for loop is similar to C and C++. It enables us to initialize the loop variable, check the
condition, and increment/decrement in a single line of code. We use the for loop only
when we exactly know the number of times, we want to execute the block of code.

1. for(initialization, condition, increment/decrement) {


2. //block of statements
3. }
The flow chart for the for-loop is given below.

Example1

1. public class series {


2. public static void main(String[] args) {
3. int i ,sum=0;
4. for( i = 1; i<=10; i++) {
5. sum = sum + i;
6. }
7. System.out.println("The sum of first 10 natural numbers is " + sum);
8. }
9. }

Output:

The sum of first 10 natural numbers is 55

for-each loop
Java provides an enhanced for loop to traverse the data structures like array or collection.
In the for-each loop, we don't need to update the loop variable. The syntax to use the for-
each loop in java is given below.

1. for(data_type var : array_name/collection_name){


2. //statements
3. }

Example for each loop

1. public class series {


2. public static void main(String[] args) {
3. // TODO Auto-generated method stub
4. String[] names = {"Java","C","C++","Python","JavaScript"};
5. System.out.println("Printing the content of the array names:\n");
6. for(String name:names) {
7. System.out.println(name);
8. }
9. }
10. }

Output:

Printing the content of the array names:

Java
C
C++
Python
JavaScript

2) while loop
The while loop is also used to iterate over the number of statements multiple times.
However, if we don't know the number of iterations in advance, it is recommended to use
a while loop. Unlike for loop, the initialization and increment/decrement doesn't take
place inside the loop statement in while loop.

It is also known as the entry-controlled loop since the condition is checked at the start of
the loop. If the condition is true, then the loop body will be executed; otherwise, the
statements after the loop will be executed.

The syntax of the while loop is given below.

1. while(condition){
2. //looping statements
3. }
The flow chart for the while loop is given in the following image.

Example for while

1. public class series {


2. public static void main(String[] args) {
3. int i = 0;
4. System.out.println("Printing the list of first 10 even numbers \n");
5. while(i<=10) {
6. System.out.println(i);
7. i = i + 2;
8. }
9. }
10. }
3) do-while loop
The do-while loop checks the condition at the end of the loop after executing the loop
statements. When the number of iteration is not known and we have to execute the loop
at least once, we can use do-while loop.

It is also known as the exit-controlled loop since the condition is not checked in advance.
The syntax of the do-while loop is given below.

1. do
2. {
3. //statements
4. } while (condition);

The flow chart of the do-while loop is given in the following image.

Example for do while loop

1. public class series {


2. public static void main(String[] args) {
3. int i = 0;
4. System.out.println("Printing the list of first 10 even numbers \n");
5. do {
6. System.out.println(i);
7. i = i + 2;
8. }while(i<=10);
9. }
10. }

Output:

Printing the list of first 10 even numbers


0
2
4
6
8
10

3) Jump Statements
Jump statements are used to transfer the control of the program to the specific
statements. In other words, jump statements transfer the execution control to the other
part of the program. There are two types of jump statements in Java, i.e., break and
continue.

Java break statement


The break statement is used to break the current flow of the program and transfer the
control to the next statement outside a loop or switch statement. However, it breaks only
the inner loop in the case of the nested loop.

The break statement cannot be used independently in the Java program, i.e., it can only
be written inside the loop or switch statement.

The break statement example with for loop

Consider the following example in which we have used the break statement with the for
loop.

BreakExample.java
1. public class BreakExample {
2.
3. public static void main(String[] args) {
4. // TODO Auto-generated method stub
5. for(int i = 0; i<= 10; i++) {
6. System.out.println(i);
7. if(i==6) {
8. break;
9. }
10. }
11. }
12. }

Output:

0
1
2
3
4
5
6

break statement example with labeled for loop

1. public class Series {


2.
3. public static void main(String[] args) {
4. a:
5. for(int i = 0; i<= 10; i++) {
6. b:
7. for(int j = 0; j<=15;j++) {
8. c:
9. for (int k = 0; k<=20; k++) {
10. System.out.println(k);
11. if(k==5) {
12. break a;
13. }
14. }
15. }
16.
17. }
18. }
19.
20.
21. }

Output:

0
1
2
3
4
5

Java continue statement


Unlike break statement, the continue statement doesn't break the loop, whereas, it skips
the specific part of the loop and jumps to the next iteration of the loop immediately.

Consider the following example to understand the functioning of the continue statement
in Java.

1. public class series {


2.
3. public static void main(String[] args) {
4.
5. for(int i = 0; i<= 2; i++) {
6.
7. for (int j = i; j<=5; j++) {
8.
9. if(j == 4) {
10. continue;
11. }
12. System.out.println(j);
13. }
14. }
15. }
16.
17. }

Output:

0
1

You might also like