How to define control flow statements in JShell in Java 9?



In this article, we will learn about the control flow statements in JShell in Java. JShell is a new interactive command-line tool introduced in Java 9. This tool can also be called REPL (Read-Eval-Print-Loop) because it takes input, evaluates it, and returns output to the user via the command line. 

JShell Control Flow Statements

Control flow statements are used to control the execution path of the code and are a fundamental part of the Java programming language. We can execute multiple-line control flow statements using JShell in the same way as Java. It recognizes multiple-line statements are prompts with the symbol "?>" to indicate to enter the next line statement.

Different Types of Control Flow Statements in JShell

The following are the different types of control flow statements in JShell:

The if-slse Statement

A JShell if statement is a conditional statement used to execute a block of code when it's true. If the condition is false, an optional else statement can be used to execute another block of code.

Syntax

The following is the syntax for the if-else statement in JShell: 

jshell> if (condition) {
   ...>     // code block
   ...> } else {
   ...>     // code block
   ...> }

Example

Below is an example of the if-else statement in JShell:

jshell> int distance = 50
distance ==> 50

jshell> if(distance < 30) {
...>       System.out.println("It's near");
...>    } else {
...>       System.out.println("It's far");
...>    }
It's far

The while Loop

The JShell while loop statement repeatedly executes a code block as long as a given condition is true and checks the condition before each iteration.

Syntax

The following is the syntax for the while loop in JShell:

jshell> while (condition) {
   ...>     // code block
   ...>     // update condition
   ...> }

Example

Below is an example of the while loop in JShell:

jshell> int i = 10
i ==> 10

jshell> while(i < 25) {
...>       System.out.println(i + " ");
...>       i++;
...>    }
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

The for Loop

The JShell for loop is an entry control loop, meaning it checks the given condition before executing the loop body.

Syntax

The following is the syntax for the for loop in JShell:

jshell> for (initialization; condition; update) {
   ...>     // code block
   ...> }

Example

Below is an example of the for loop in JShell:

jshell> String names[] = {"Adithya", "Jai", "Raja", "Chaitanya", "Ravi", "Surya"}
names ==> String[6] { "Adithya", "Jai", "Raja", "Chaitanya", "Ravi", "Surya" }

jshell> for(String name : names) {
...>       System.out.println(name);
...>    }
Adithya
Jai
Raja
Chaitanya
Ravi
Surya

The switch Statement

The JShell switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

Syntax

The following is the syntax for the switch statement in JShell:

jshell> switch (variable) {
   ...>     case value1: // code; break;
   ...>     case value2: // code; break;
   ...>     default: // code;
   ...> }

Example

Below is an example of the switch statement in JShell:

jshell> String grade = "B";
grade ==> "B"

jshell> switch (grade) {
   ...>     case "A": System.out.println("Welcome"); break;
   ...>     case "B": System.out.println("To"); break;
   ...>     case "C": System.out.println("Tutorials"); break;
   ...>     default: System.out.println("Point");
   ...> }
To
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-06-11T13:54:07+05:30

337 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements