- Control Structures in Java
- Types of Control Structures
- Sequential Control Structure:
- Conditional Control Structure:
- Repetition Control Structure:
- Selection Control Structure:
- Jump Control Structure:
- Control structures in Java
- Three kinds of control structures in Java
- 1. Control statements in java / Conditional Branches in java
- if statement
- nested if statement
- if-else statement
- if-else if statement/ ladder if statements
- Switch statement
- 2. Loops in Java / Looping statements in java
- for loop
- while loop
- do-while loop
- 3. Branching Statements in Java
- continue
- Leave a Comment Cancel reply
Control Structures in Java
Java provides special blocks of statements that allow great control over the execution of statements in a program. These are referred to as the control structures in Java.
Types of Control Structures
Java supports 5 types of control structures to control the flow of a program.
Sequential Control Structure:
- If the programmer uses no control structure within a Java program, the control moves forward to the next statements sequentially. This scenario is known as Sequential Control Structure.
- By default, all the statements execute sequentially one by one.
Conditional Control Structure:
- We can also call these statements as decision making control structure.
- They let us make a decision based upon the result of a condition.
- These statements will execute a block of code conditionally depending on whether the condition is true or false.
- If the condition is true, then the block of code will execute otherwise it will be skipped.
- In some cases, there are 2 statement blocks one for the true value and other for the false value.
- Moreover, the control will not go to the next statement until and unless the condition is satisfied.
- For example,
- if
- if..else
- if..else ladder (multiple if)
- nested if
Repetition Control Structure:
- The looping statements help to execute a set of instructions repeatedly as long as the condition is true.
- It repeats some portion of the program either a specified number of times or until a particular condition is satisfied.
- This process of repeating some portion of the program is called looping or branching.
- Sometimes we will provide the number of repetitions whereas in some cases we may not be aware of the number of repetitions.
- For example:
- for loop
- while loop
- do..while
- for each
Selection Control Structure:
- This control structure allows us to make a decision from a number of choices.
- Mostly, menu-driven programs use switch cases, where we have to choose a relevant option out of multiple options.
- Each option has a different function to perform.
- For example:
- the switch case
Jump Control Structure:
- There are 2 jump control statements i.e. break and continue.
- These statements directly transfer control of the program without depending upon any conditions.
- They cause a sudden jump or control transfer from the current statement to another statement somewhere else in the code.
- Thus they can interrupt the normal flow of the program according to the need.
In the next article, I will discuss in details all the types of decision making statements and conditional control structures available in Java. The article will cover all the types of if..else statements and simple programs based on if..else statements.
Control structures in Java
A program is a list of instructions or blocks of instructions. Java provides Control structures that can change the path of execution and control the execution of instructions. In this post, we will discuss the Control structures in programming language.
Three kinds of control structures in Java
1. Control statements in java / Conditional Branches in java
Java control statements use to choose the path for execution. There are some types of control statements:
if statement
It is a simple decision-making statement. It uses to decide whether the statement or block of statements should be executed or not. A Block of statements executes only if the given condition returns true otherwise the block of the statement skips. The if statement always accepts the boolean value and performs the action accordingly. Here we will see the simplest example of an if statement. We will discuss it in detail in a separate post. Visit here for more details.
public class IfStatementExample < public static void main(String arg[]) < int a = 5; int b = 4; // Evaluating the expression that will return true or false if (a >b) < System.out.println("a is greater than b"); >> >
Output: a is greater than b
nested if statement
The nested if statements mean an if statement inside another if statement. The inner block of the if statement executes only if the outer block condition is true. This statement is useful when you want to test a dependent condition. Here we will see the example of nested if with a common example and see how it works. Visit here for more details.
public class NestedIfStatementExample < public static void main(String arg[]) < int age = 20; boolean hasVoterCard = true; // Evaluating the expression that will return true or false if (age >18) < // If outer condition is true then this condition will be check if (hasVoterCard) < System.out.println("You are Eligible"); >> > >
Output: You are Eligible
if-else statement
In the if statement, the block of statements executes when the given condition returns true otherwise block of the statement skips. Suppose when we want to execute the same block of code if the condition is false. Here we come on the if-else statement. In an if-else statement, there are two blocks one is an if block and another is an else block. If a certain condition is true, then if block executes otherwise else block executes. Let’s see the example and understand it. Visit here for more details.
public class IfElseStatementExample < public static void main(String arg[]) < int a = 10; int b = 50; // Evaluating the expression that will return true or false if (a >b) < System.out.println("a is greater than b"); >else < System.out.println("b is greater than a"); >> >
Output: b is greater than a
if-else if statement/ ladder if statements
If we want to execute the different codes based on different conditions then we can use if-else-if. It is known as if-else if ladder. This statement executes from the top down. During the execution of conditions if any condition is found true, then the statement associated with that if statement is executed, and the rest of the code will be skipped. If none of the conditions returns true, then the final else statement executes. Let’s see the example of the ladder if and you can visit here for more details.
public class IfElseIfStatementExample < public static void main(String arg[]) < int a = 10; // Evaluating the expression that will return true or false if (a == 1) < System.out.println("Value of a: "+a); >// Evaluating the expression that will return true or false else if(a == 5) < System.out.println("Value of a: "+a); >// Evaluating the expression that will return true or false else if(a == 10) < System.out.println("Value of a: "+a); >else < System.out.println("else block"); >> >
Output: Value of a: 10
Switch statement
The switch statement in java is like the if-else-if ladder statement. To reduce the code complexity of the if-else-if ladder switch statement comes.
In a switch, the statement executes one from multiple statements based on condition. In the switch statements, we have a number of choices and we can perform a different task for each choice. You can read it in detail, for more details.public class SwitchStatementExample < public static void main(String arg[]) < int a = 10; // Evaluating the expression that will return true or false switch(a) < case 1: System.out.println("Value of a: 1"); break; case 5: System.out.println("Value of a: 5"); break; case 10: System.out.println("Value of a: 10"); break; default: System.out.println("else block"); break; >> >
Output: Value of a: 10
2. Loops in Java / Looping statements in java
The loop in java uses to iterate the instruction multiple times. When we want to execute the statement a number of times then we use loops in java.
for loop
A for loop executes the block of code several times. It means the number of iterations is fixed. JAVA provides a concise way of writing the loop structure. When we want to execute the block of code serval times, let’s see the example and visit here for more details.
public class ForLoopExample < public static void main(String arg[]) < // Creating a for loop for(int i = 1; i > >
Output: Iteration number: 1
Iteration number: 2
Iteration number: 3while loop
A while loop allows code to execute repeatedly until the given condition returns true. If the number of iterations doesn’t fix, it recommends using a while loop. It is also known as an entry control loop. For more details.
public class WhileLoopExample < public static void main(String arg[]) < // Creating a while loop int i = 1; while(i > >
Output: Iteration number: 1
Iteration number: 2
Iteration number: 3do-while loop
It is like a while loop with the only difference being that it checks for the condition after the execution of the body of the loop. It is also known as Exit Control Loop. For more details.
public class DoWhileLoopExample < public static void main(String arg[]) < // Creating a while loop int i = 1; do < System.out.println("Iteration number: "+i); i++; >while(i >
Output: Iteration number: 1
Iteration number: 2
Iteration number: 33. Branching Statements in Java
These use to alter the flow of control in loops.
If a break statement encounters inside a loop, the loop immediately terminates, and the control of the program goes to the next statement following the loop. It is used along with the if statement in loops. If the condition is true, then the break statement terminates the loop immediately.
public class BreakStatementExample < public static void main(String arg[]) < for(int i = 0; i > >
Output: Iteration number:0
Iteration number:1
Iteration number:2continue
The continue statement uses in a loop control structure. If you want to skip some code and need to jump to the next iteration of the loop immediately. Then you can use a continue statement. It can be used in for loop or a while loop. Read it in detail
public class ContinueStatementExample < public static void main(String arg[]) < for(int i = 0; i > >
Output: Iteration number:0
Iteration number:2
Iteration number:3Leave a Comment Cancel reply
You must be logged in to post a comment.
- Basics Of Java
- What is JAVA language
- JAVA features/advantage
- How to run Java program
- Difference between JRE, JDK and JVM
- Java Virtual Machine(JVM) & JVM Architecture
- Variables in Java
- Local Variable Type Inference (Java var)
- Java control statements
- if statement in Java
- Switch statement Java
- class and object in Java
- Constructor Overloading in Java
- Constructor chaining in java
- Multiple inheritance using interface in java
- Method overloading in Java
- Method overriding in Java
- Abstract class in java
- Interface in java
- Multiple inheritance using interface in java
- Java import package
- final variable in Java
- final method in java
- final class in Java
- Static variable in java
- Static method in java
- Static block in java
- Static class in java
- Thread life cycle in java
- Thread scheduler in java
- Java Thread Sleep
- join() method in java
- yield() method in java
- wait() method in Java
- notify() method in Java
- Thread class in java
- Daemon thread in java
- Callable and Future in java
- What is immutable String in java
- What is a mutable string
- String concatenation
- String comparison in Java
- Substring in Java
- Convert string to int
- String Replace in Java
- Substring in Java
- String constructor in java
- string methods in java
- try and catch java block
- finally block in java
- throw and throws keyword in java
- Chained exception in java
- User defined exception in java
- Java try with resource
- ArrayList in java
- Java ArrayList methods
- How to add the element in ArrayList
- How to replace element in an ArrayList?
- How to remove element from arraylist java
- How to access ArrayList in java
- How to get index of object in arraylist java
- How to Iterate list in java
- How to achieve Java sort ArrayList
- How to get java sublist from ArrayList
- How to Convert list to array in java
- How to convert array to list java
- How to remove duplicates from ArrayList in java
- Difference between ArrayList and array
- Difference between ArrayList and LinkedList
- Java linked list methods
- How to do LinkedList insertion?
- How to perform deletion in linked list
- How to get the element from LinkedList
- Traverse a linked list java
- Searching in linked list
- How to convert linked list to array in java
- How to remove duplicates from linked list
- How to perform linked list sorting
- Difference between ArrayList and LinkedList
- How to add element in HashSet?
- How to remove elements from HashSet?
- TreeSet internal working
- TreeSet Methods in Java
- Internal working of HashMap
- HashMap method in java
- How to add an object in HashMap by HashMap put() method
- How to get values from hashmap in java example
- How to remove the element by Java HashMap remove() method
- How to replace the value by HashMap replace() method
- How to check the map contains key by hashmap containskey() method
- How to get java map keyset by hashmap keyset() method
- How to get java map entryset by java entryset() method
- How to iterate hashmap in java
- TreeMap(Comparator comp)
- Treemap methods
- Generic types in Java
- Advantages of generics in java
- toString() method in Java
- equals() method in java
- Java hashCode() method
- clone() method in java
- finalize() method in java
- getclass() method in java
- wait() method in Java
- notify() method in Java
- Garbage collector in java
- finalize() method in java
- equals() method in java
- Java hashCode() method
- Comparable java interface
- Comparator interface
- Difference between comparable and comparator
- Why Comparable and Comparator are useful?
- comparator() method in java
- functional interface in java 8
- Predicate in java 8
- Why predicate in java 8
- Why consumer in java 8
- How lambda expression works in java
- Java 8 lambda expression example
- Why Lambda expression use functional interface only
- Lambda expression with the return statement
- Java stream operations in java
- Intermediate operation in Java 8
- Terminal operations in java 8
- Short circuit operations in Java 8
- Lazy evaluation of stream
- Converting stream to collections and Arrays
- Java 9 module
- try with resource improvement
- Optional Class Improvements
- Private methods in interface
- Factory Methods for Immutable List, Set, Map and Map.Entry
- Jshell java 9
- Java CompletableFuture API Improvements
- Local Variable Type Inference (Java var)
- Java 11 String New Methods
Content copy is strictly prohibited. Copyright © by JavaGoal 2022. Designed & Developed By Finalrope Soft Solutions Pvt. Ltd.