Control Flow Statements:
The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and
branching, enabling your program to conditionally execute particular blocks of code.
Java Control statements in a programming language are very useful as they allow a programmer to change the flow of program execution i.e. altering the normal program flow to jump directly on some statement(s) or to skip a
statement(s).
In Java control statements are divided into following 3 categories:
1. decision-making statements
1.1 If
1.2 If-else
1.3 switch
2. looping statements
2.1 for
2.2 While
2.3 do-while
2.4 Enhanced for loop
3. branching statements
3.1 break
3.2 continue
3.3 return
Selection/Decision making Statements: Using these statements, a piece of code would be executed only if a certain condition(s) is true.
1. if Statement: Statement(s) between the set of curly braces ‘{ }’ will be executed only if the condition(s), between the set of brackets ‘( )’ after ‘if’ keyword, is/are true.If the value is false, then the if block is skipped and execution continues with the rest of the program. You can either have a single statement or a block of code within an if statement. Note that the conditional expression must be a Boolean expression.
Syntax:
if (Condition) {
// statements;
}
2. if-else Statement: If the condition(s) between the brackets ‘( )’ after the ‘if’ keyword is/are true then the statement(s) between the immediately following set of curly braces ‘{ }’ will be executed else the statement(s) under, the set of curly braces after the ‘else’ keyword will be executed.You can either have a single statement or a block of code within if-else blocks. Note that the conditional expression must be a Boolean expression.
Syntax:
if (condition) {
// statements;
} else {
// statements;
}
3. switch Statement: When there is a long list of cases & conditions, then if/if-else is not good choice as the code would become complicated.
Syntax:
switch (expression)
{
case value1:
//statement;
break;
case value2:
//statement;
break;
default:
//statement;
}
The following rules apply to a switch statement:
- The variable used in a switch statement can only be a byte, short, int, or char. A switch works with the byte, short, char, and int primitive data types. It also works with the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.
- You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
- The value for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
- When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
- When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
- Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
- A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
1. while loop: The while statement continually executes a block of statements while a particular condition is true.
Syntax:
while (expression) {
statement(s)
}
The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
2. do-while: It will enter the loop without checking the condition first and checks the condition after the execution of the statements. That is it will execute the statement once and then it will evaluate the result according to the condition.
Syntax:
do {
statement(s)
} while (expression);
The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.
3. for Statement: A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
A for loop is useful when you know how many times a task is to be repeated.
Syntax:
for(initialization; Boolean_expression; increment)
{
//Statements
}
Following is the flow of control in a for loop:
- The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables.
- Next, the Boolean expression is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop.
- After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to increment any loop control variables.
- The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step,then Boolean expression). After the Boolean expression is false, the for loop terminates.
4. Enhanced for loop: As of java 5 the enhanced for loop was introduced.The Enhanced for statement is designed for iteration through Collections and arrays and can be used to make your loops more compact and easy to read.
Syntax:
for(declaration : expression)
{
//Statements
}
Declaration: The newly declared block variable, which is of a type compatible with the elements of the array/collection you are accessing. The variable will be available within the for block and its value would be the same as the current array/collection element.
Expression: This evaluate to the array you need to loop through. The expression can be an array/collection variable or method call that returns an array/collection.
Example:
public class Test {
public static void main(String args[]){
int [] numbers = {100, 200, 300, 400, 500};
for(int y : numbers ){
System.out.print( x + ",");
}
System.out.print("\n");
String [] names ={"Krish", "Munna", "Anusha", "Bobby"};
for( String name : names ) {
System.out.print( name + ",");
}
}
}
1. Break statements: It contains two forms like labeled and unlabeled.The break statement is used for breaking the execution of a loop (while, do-while and for) . It also terminates the switch statements.
Syantax:
break; // breaks the innermost loop or switch statement.
syntax: continue;
continue label;- The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.
- A labeled continue statement skips the current iteration of an outer loop marked with the given label.
- The return statement exits from the current method, and control flow returns to where the method was invoked.
- This statement is used to return a value to the caller method and terminates execution of method.
- This has two forms: one that returns a value and the other that can not return.
- The returned value type must match the return type of method.When a method is declared void, use the form of return that doesn't return a value(return;).
Syntax: return;
return values;
I will explain example for Control flow of statements in another section.
No comments:
Post a Comment