Control Statements in Java – Expert’s Top Picks
Last updated on 28th Oct 2022, Artciles, Blog
- In this article you will get
- 1.Introduction
- 2.Decision Making Statements
- 3.Conclusion
Introduction
Control Statements in Java is one of the fundamentals required for a Java Programming. It allows smooth flow of a program.Each programmer is familiar with a term statement, which can simply be explained as an instruction given to the computer to perform particular operations. A control statement in java is a statement that finds whether the other statements will be executed or not. It controls flow of a program. An ‘if’ statement in a java finds the sequence of execution between a set of two statements. Control Statements can be split into a 3 categories, namely.
- Selection statements
- Iteration statements
- Jump statements
Decision-Making Statements
Statements that find which statement to be execute and when are known as a decision-making statements. The flow of the execution of the program is controlled by control flow statement.
There are four decision-making statements available in a Java.
Simple if statement
The if statement finds whether a code should be executed based on a specified condition.
Syntax:
- if (condition) {
- }
- Output:
- If statement!
- Hello World!
If..else statement:
In this statement, if condition specified is a true, the if block is executed. Otherwise, else block is will be executed.
Example:
- public class Main
- {
- public static void main(String args[])
- {
- int a = 15;
- if (a > 20)
- System.out.println(“a is greater than 10”);
- else
- System.out.println(“a is less than 10”);
- System.out.println(“Hello World!”);
- }
- }
- }
- Output:
- a is less than 10
- Hello World!
Nested if statement:
An if present inside an if block is known as the nested if block. It is similar to the if..else statement, except they are ecplained inside another if..else statement.
Syntax:
- if (condition1) {
- Statement 1; //executed if first condition is true
- if (condition2) {
- Statement 2; //executed if second condition is true
- }
- else {
- Statement 3; //executed if second condition is false
- }
- }
Example:
- public class Main
- {
- public static void main(String args[])
- {
- int s = 18;
- if (s > 10)
- {
- if (s%2==0)
- System.out.println(“s is even number and greater than 10!”);
- else
- System.out.println(“s is odd number and greater than 10!”);
- }
- else
- {
- System.out.println(“s is less than 10”);
- }
- System.out.println(“Hello World!”);
- }
- }
- Output:
- s is even number and greater than 10!
- Hello World!
Switch statement:
A switch statement in java is used to execute single statement from the multiple conditions. The switch statement can be used with the short, byte, int, long, enum types, etc.
Certain points must be noted while using a switch statement:
- One or N number of a case values can be specified for switch expression.
- Case values that are a duplicate are not be permissible. A compile-time error is generated by a compiler if unique values are not used.
- The case value are must be a literal or constant. Variables are not be permissible.
- Usage of break statement is made to end the statement sequence. It is an optional to use this statement. If this statement is not be specified, to the next case is be executed.
Looping Statements:
Statements that are execute a block of code repeatedly until a specified condition is met are known as the looping statements. Java offers the user with three types of a loops:
While:
Known as a most common loop, while loop evaluates a specific condition. If condition is a true, code is be executed. This process is continued until specified condition turns out to be a false.The condition to be specified in while loop must be a Boolean expression. An error will be generated if type used is int or a string.
Syntax:
- while (condition)
- {
- statementOne;
- }
Example:
- public class whileTest
- {
- public static void main(String args[])
- {
- int i = 5;
- while (i <= 15)
- {
- System.out.println(i);
- i = i+2;
- }
- }
- }
- Output:
- 5
- 7
- 9
- 11
- 13
- 15
Do..while:
The do-while loop is similar to while loop, the only difference being that a condition in the do-while loop is evaluated after an execution of the loop body. This guarantees that loop is executed at be least once.
Syntax:
- do{
- //code to be executed
- }while(condition);
For:
The for loop in java is used to an iterate and evaluate the code multiple times. When number of iterations is known by a user, it is recommended to use for loop.
Syntax:
- for (initialization; condition; increment/decrement)
- {
- statement;
- }
For-Each:
The traversal of elements in the array can be done by the for-each loop. The elements present in array are returned one by one. It must be noted that a user does not have to increment value in for-each loop.
Branching Statements:
Branching statements in a java are used to jump from the statement to another statement, thereby the transferring a flow of execution.
Break:
The break statement in a java is used to terminate a loop and break a current flow of the program.
Continue:
To jump to a next iteration of the loop, and make use of continue statement. This statement continues a current flow of the program and skips a part of code at specified condition.
Conclusion
Decision-making statements are used to control flow based on a certain conditions.There are different decision making statements in a Java such if, if-else, if-else-if ladder and switch statements.Loop statements are used to run the block of code repeatedly a number of times.There are various loop statement constructs like for, while, do-while and for-each statements. Branching statements are used to transfer a control of the program to a various block of code or method.There are various types of branching statements like break, continue and return statments that transfer a control of execution to a specific point in a code.
Are you looking training with Right Jobs?
Contact Us- Java Servlets Tutorial
- OOPs Concepts in Java | Learn from Basics with Examples
- How to Use Java Classes In Talend ? : Step-By-Step Process with REAL-TIME Examples
- What is Abstraction in Java | Implementations of Abstraction in Java | A Definitive Guide with Best Practices
- Kotlin vs Java | Know Their Differences and Which Should You Learn?
Related Articles
Popular Courses
- Hadoop Developer Training
11025 Learners
- Apache Spark With Scala Training
12022 Learners
- Apache Storm Training
11141 Learners
- What is Dimension Reduction? | Know the techniques
- Difference between Data Lake vs Data Warehouse: A Complete Guide For Beginners with Best Practices
- What is Dimension Reduction? | Know the techniques
- What does the Yield keyword do and How to use Yield in python ? [ OverView ]
- Agile Sprint Planning | Everything You Need to Know