Control Statements in Java: Everything You Need to Know

Updated on July 30, 2024

Article Outline

Control statements are fundamental to programming languages. They allow developers to dictate the flow of execution in a Java program. Control Statement in Java  enables you to make decisions, repeat tasks, and jump between different parts of source code.

 

In this article, we will learn about control statements in Java. After reading this article, You will know everything about the control statements in Java, including their types, uses and examples.

Introduction to Control Statements in Java

 

Control statements are a fundamental part of the Java programming language. They allow you to control the flow of execution in a program.  They enable the execution of a block of code multiple times, execute a block of code based on conditions, terminate or skip the execution of certain lines of source code, etc.

 

*Image
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure

Types of Control Flow Statements

Control  Flow Statements Type Control Flow Statements Description
Conditional Statements if-else This statement executes a block of code if a specified condition is true and another block if the condition is false.
switch-case This evaluates a variable or expression and executes code based on matching cases.
 Looping Statement for This code blocks a specified number of times, typically iterating over a range of values.
while  It executes a code block as long as a specified condition is true.
do-while It executes a code block once and then repeats the execution as long as a specified condition is true.
  Jump Statements break  These jump statements terminate the loop or switch statements and transfer control to the statement immediately following the loop or switch.
continue Skip the current iterations of a loop and continue with the next iteration.
return This statement function and returns a value to the caller.
goto The goto statement transfers control to a labelled statement within the same function. It is generally discouraged because it can make code difficult to read and prone to errors.

Conditional Statements in Java Programming

 

In Java, conditional statements in programming are used for certain blocks of code based on the specified conditions. This is very fundamental to decision-making in programs. Let’s see some common conditional statements.

 

  1. If Statement in Programming: The if statement executes a code block if a specified condition is true.

 

The following program demonstrates the if statement in Java language.

 

Program

class Main{ public static void main(String args[]){ System.out.println("Main") ;   int a = 343 ; if(a == 343){ System.out.println("A is equal to 343  ") ; } } }

Output

A is equal to 343

 

  1. If-else Statement in Programming:  The if-else statement executes one block of code if the specified condition is true. If the condition is not true, it will run another code block

 

The following program demonstrates the if-else statement in Java language.

 

 Program

class Main{ public static void main(String args[]){ int a = 30 ; if(a == 39) { System.out.println( " a is  equal to 39"); }else{ System.out.println(" a is not equal to 39"); } } }

Output

a is not equal to 39

 

  1. If-else-if Statement in Programming: The if-else-if statement executes one block of code if a specified condition is true, another block if another condition is true, and a default block of code if none of the conditions is true.

 

The following program demonstrates the if-else-if statement in Java language.

 

Program

class Main{ public static void main(String args[]){ int a = 34 ; if(a == 34){ System.out.println("a is equal to 34") ; }else if(a == 34){ System.out.println("a is equal to 34") ; }else{ System.out.println(" a is not equal to 5 or 10") ; } } }

Output

a is equal to 34
  1. Ternary Operator or Conditional Operator in Programming: In the Java programming language, a ternary operator assigns a value to a variable based on a condition set by the user.

 

The following program demonstrates the Ternary Operator in Java language.

 

Program

class Main{ public static void main(String args[]){ int a = 30 ; String c  = ( a ==5 ? "a is equal to 5" : " a is not equal to 5 ") ; System.out.println(c) ; } }

Output

a is not equal to 5

 

  1. Switch Statement in Programming:  Java language also supports the switch statement. The switch statement executes one block of code from multiple options based on the value of an expression.

 

The following program demonstrates the Switch Statement in Java.

 

Program

class Main { public static void main(String[] args){ int a = 33; switch (a) { case 5: System.out.println("a is equal to 5"); break; case 10: System.out.println("a is equal to 10"); break; default: System.out.println("a is not equal to 5 or 10"); } } }

Output

a is not equal to 5 or 10

Looping Statements in Java Programming

Loops are statements used in programming to repeatedly execute a block of code. They are essential for performing tasks such as iterating over elements in a list, reading data from a file, or executing a set of instructions a specific number of times. Let’s describe some common types of looping statements.

 

  1. For Loop in Programming:  The for loop is used to iterate over a sequence of characters or a list of items in the array. It executes a block of code for each item in the sequence.

 

The following program demonstrates the for loop in Java programming language.

 

Program

public class Main { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println("Number is = "+i); } } }

Output

Number is = 0 Number is = 1 Number is = 2 Number is = 3 Number is = 4 Number is = 5 Number is = 6 Number is = 7 Number is = 8 Number is = 9
  1. While Loop in Programming:  The while loop is similar in Java. This loop is used to repeatedly execute a block for code as long as a specified condition is true.

 

The following program demonstrates the while loop example in Java language.

 

Program

class Main { public static void main(String[] args){ int count = 0; while (count < 10) { System.out.println("Number = "+count); count++; } } }

Output

Number = 0 Number = 1 Number = 2 Number = 3 Number = 4 Number = 5 Number = 6 Number = 7 Number = 8 Number = 9
  1. Do-While Loop in Programming: The Java language also supports the do-while loop. It is used to execute a block of code at least once. IT executes the block repeatedly as long as a specified condition is true.

 

The  following program demonstrates a do-while loop example.

 

Program

class Main { public static void main(String[] args){ int count = 0; do { System.out.println("Number = "+count); count++; } while (count < 5); } }

Output

Number = 0 Number = 1 Number = 2 Number = 3 Number = 4
  1. Nested Loops in Programming:   Java also supports Nested Loops in a programming language. It means, a for loop can be nested inside another for loop to create two-dimensional iterations.

 

The following program demonstrates Nested Loops in Programming language.

 

Program     

class Main { public static void main (String[] args) { for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { System.out.println("i=" + i + " j=" + j); } } } }

Output

i=0 j=0 i=0 j=1 i=1 j=0 i=1 j=1

Jump Statements in Java Programming

Jump statements in programming change the flow of control within a program. It allows the programmer to transfer the program control to different parts of the code based on certain conditions or requirements. Let’s see common types of jump statements.

 

  1. Break Statement in Programming:  The break statement is used to exit from the loops when specific conditions occur in simple words. It terminates the loop’s execution and transfers control to the statement immediately following the loop.

 

The following program demonstrates the use of the case for the break statement.

 

Program

class Main { public static void main(String[] args){ for (int i = 0; i < 10; i++) { if (i == 5) break; System.out.print(i + " "); } } }

Output

0 1 2 3 4
  1. Continue Statement in Programming: The continue statement is used to skip the current iteration of a loop and proceed to the next iteration.

          

The following program demonstrates the continue statement in the Java language.

 

Program

class Main { public static void main(String[] args){ for (int i = 0; i < 10; i++) { if (i % 2 == 1) continue; System.out.print(i + " "); } } }

Output

0 2 4 6 8
  1. Return Statement in Programming: This section will show the return statement. It is used to exit a function and return a value to the caller.

 

The following program demonstrates the return statement use case in Java language.

 

Program

class Main { static boolean isEven(int N) { return N % 2 == 0; } public static void main(String[] args) { int N = 34; if (isEven(N)) { System.out.println("N is even"); } else { System.out.println("N is odd"); } } }

Output

N is even

Conclusion

In this article, we learned about the control statements in Java. These are fundamental to writing efficient and effective source code. It means they provide the ability to dictate the execution flow, enabling the creation of dynamic and responsive programs. From simple decision-making with ‘if-else’ statements to more complex structures like loops and switch case constructs, Understanding these elements is crucial for any Java programmer. Mastering the control statements improves your problem-solving ability and overall programming skills.

FAQs
The ‘break’ statement is used to prematurely exit a loop or switch statement. It is often used within loops to terminate the loop based on a specific condition.
Control statements are crucial for creating dynamic and responsive programs. They allow developers to implement decision-making, repetition, and branching, which are fundamental for solving complex problems and writing efficient source code.
The labeled statements provide a label to a code block or a loop. They are useful for controlling the flow with ‘break’ and ‘continue’ statements, especially in nested loops. A name followed by a colon defines a label.
A ‘for’ loop repeats a code block several times. It consists of three parts: initialization, condition, and increment/decrement. The loop runs as long as the condition is true.
The ‘return’ statement exits a method and optionally returns a value to the method caller. It is essential for providing method results and terminating method execution.

Updated on July 30, 2024

Link
left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved