In Java, code is typically executed in a linear fashion, following the order in which statements appear. However, control flow statements are integral to Java programming, allowing developers to manage and alter the normal execution sequence. These statements enable the creation of more dynamic and flexible programs by introducing conditions, loops, and branching mechanisms. Despite the default top-to-bottom execution, control flow statements empower developers to influence the flow of their Java code, enhancing its structure and functionality.
Conditional statements These statements play a role in making decisions, determining a course of action based on a particular choice or outcome.
n is created by the conditional statement based on an expression that is passed. Either YES or NO.
Iterative statements, This statement involves continuous iteration until a specified condition is fulfilled. In simpler terms, when an expression is present, the statement repeats itself until the condition is satisfied.
Table of Content
Approach 1:
In this methodology, we employ an "if" statement to verify a particular condition, and the corresponding code block is executed when the specified condition is met.
Syntax:
if ( condition_is_given_here ) {
// If the condition is met,
//the code will get executed.
}
Examaple.
const num = 5;
if (num > 0) {
console.log("The number is positive.");
}
Output:
The number is positive.
Approach 2:
The if-else statement executes a specific set of actions based on a given condition. If the condition is met, a designated code block is executed; otherwise, an alternative code block is executed to fulfill the requirements of that particular condition.
Syntax
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
example using actual code
const num = 5;
if (num > 0) {
console.log("The number is positive.");
} else {
console.log("The number is not positive.");
}
Approach 3:
The switch case statement in JavaScript is employed to make decisions, offering an alternative to if-else statements. In certain scenarios, utilizing the switch case statement is considered more convenient than if-else statements.
Syntax:-
switch (expression) {
case value1:
// Code to be executed if expression matches value1
break;
case value2:
// Code to be executed if expression matches value2
break;
// Additional cases as needed
default:
// Code to be executed if none of the cases match the expression
}
switch: Keyword indicating the start of the switch statement.
expression: The value or expression to be evaluated.
case: Keyword followed by a specific value that is compared against the expression.
break: Keyword to exit the switch statement after a case is matched. Without it, the code will continue to the next case.
default: Optional case that is executed if none of the specified cases match the expression.
Example:-
const day = "Monday";
switch (day) {
case "Monday":
console.log("It's the start of the week.");
break;
case "Friday":
console.log("It's almost the weekend!");
break;
default:
console.log("It's a regular day.");
}
output "It's the start of the week." since day is "Monday".
Approach 4:
Loops allow you to repeatedly execute a block of code as long as a specified condition is true
for Loop:
Syntax:-
for (initialization; condition; increment/decrement) {
// code to be executed
}
Example:-
for (let i = 0; i < 5; i++) {
console.log(i);
}
While Loop:
Syntax:-
while (condition) {
// code to be executed
}
Example:-
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
Do-While Loop:
Syntax:-
do {
// code to be executed
} while (condition
Example:-
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
In each case:
The initialization is where you set up any initial values or variables.
The condition is the test that is evaluated before each iteration. If it is true, the loop continues; otherwise, it exits.
The increment/decrement is executed after each iteration of the loop (applicable to for loop).
The code to be executed is the block of statements inside the curly braces {} that will be repeated as long as the condition is true.