Control Statements in any programming language are used to branch and divert the control of execution based on a modification to the state of the program. In Java, the control statements can be grouped into three categories:
These statements are used to control the flow of execution of the program based on certain conditions that are only known at run time.
The control of the program depends upon the condition result that is a Boolean value, either true or false. The types of if statement are if statement,if-else statement,else-if statement and Nested if-statement.
if(< condition>) {
//block of code
}
The switch statement helps us to check the variable for the range of values defined for multiple case statements.
switch < variable> {
Case < value1>:
// statements
Case < value n>:
// statements
Default:
// statements
}
Iteration Statements or loop statements are used to execute the set of instructions in a repeated order.
It enables us to initialize the loop variable, check the condition, and increment/decrement in a line of code.
for(< initialization>, < condition>, < increment/decrement>) {
//statements
}
Java provides a loop to traverse the data structures like array or collection.
for(type var : collection){
//statements
}
The while loop is used to iterate over the number of statements multiple times.
While(< condition>){
//statements
}
The do-while loop executes the loop statement and then checks the condition at the end of the loop.
do
{
//statements
} while (< Condition>);
Jump statements are used for transferring the control of the program to the specific statements given.
The Break statement is used to breaking the current flow of the program and transferring the control to the next statement outside the current flow. The Break statement is used for breaking the loop and in the switch statement.
Continue statement does not break the loop, but it skips the specific part of the loop and jumps immediately to the next iteration of the loop.
Knowledge2life!
Knowledge2life!
|