Control Statements in Java Programming
Java Selection Statement: Java’s program control statements can be organized into the following categories:
- Selection Statements
- Iteration Statements
- Jump Statements
Selection statements allow our program to choose between different paths of execution. Iteration statements enable a section of code to be repeated. Jump statements directly transfer program control from one location to another. Java’s selection statements include if and switch. Iteration statements include for, while and do-while and jump statements include break, continue and return.
The if Statement: This type of statements are used when we have to select statement or a block of statements if certain condition is satisfied and take some other action if the condition is not satisfied. There are different form of if statements as mentioned below
Simple if statement: When we have to perform the action only if certain condition is matched and do nothing if the condition is not satisfied. This situation can be best described by simple if statement. The complete form of simple if statement is :
if (boolean expression) {
true block;
}
If the condition is true then statements in the block are executed and otherwise nothing will be done. Consider the following code segment.
int x = 10;
if(x<10)
System.out.println (“x is less than 10”);
The if else statement: When there is a need of executing a statement or block if a condition matches and another statement or block is to be executed otherwise, then we use if-else statement. It has the general syntax as given below:
if(condition){
// execute if true statements here
}
else
{
//execute if not true statement here
}
In this situation, if the condition is satisfied, the first block is executed; otherwise the second block is executed.
The if-else ladder statement: If we have the situation where there are different actions that are executed depending upon different condition with same type of instance then if –else ladder is useful. It has general syntax as given below:
if(condition1)
statement 1
else if(condition2)
statement 2
else if(condition3)
statement 3
………………..
else if(condition N)
statement N
else
default statement
The conditional expressions are evaluated from top downwards. As soon as true condition is found, the statement associated with it is executed and the rest of the ladder is bypassed. If none of the condition is true, the final else statement is executed. The final else often acts as a default condition. If there is no final else and all other conditions are false, no action will take place.
The switch Statement: The second Java selection statement is the switch. The switch provides for a multiway branch. Thus, it enables a program to select among several alternatives. Although a series of nested if statements can perform multiway tests, for many situations the switch is more efficient approach. It works like this. The value of an expression is successively tested against a list of constants. When a match is found, the statement sequence associated with that match is executed. The general form of the switch statement is:
switch(expression ){
case value1:
statement 1:
break;
case value 2:
statement 2:
break;
……..
default:
default statement;
}
Also Read: Java Programming II | Old Paper Solution | BIM 5th Semester
The following program illustrates the switch
public class SwitchDemo { public static void main(String[] args) { int i; for(i=0;i<10;i++){ switch(i){ case 0: System.out.println("i is zero"); break; case 1: System.out.println("i is one"); break; case 2: System.out.println("i is two"); break; case 3: System.out.println("i is three"); break; case 4: System.out.println("i is four"); break; case 5: System.out.println("i is five"); break; default: System.out.println("i is six or more"); } } } }
The output of above program is:
Run:
i is zero
i is one
i is two
i is three
i is four
i is five
i is six or more
i is six or more
i is six or more
i is six or more
Iteration Statement: When there is a need of executing a task a specific number of times until a termination condition is met, we use iteration statements. These statements are also called looping statements or repetitive statements. Java provides three ways of writing iterative statements. They are while statement, do while statement, and for statement.
The while statement: The while statement executes the statements as long as the given condition remains true. The general syntax of while loop is:
while (condition){
//statements;
}
The statements within the brace are executed as long as the condition is true. When the condition becomes false, the while loop stops executing these statements and exists out of the loop. Consider the following program.
public class WhileLoop { public static void main(String[] args) { int i=0; while(i<=10){ System.out.print(i+" "); i++; } } }
The output of the above program is:
0 1 2 3 4 5 6 7 8 9 10
The do while Statement: The do while loop executes its body at least once. So, to guarantee, at least one time execution of statements do while is used. The basic syntax of do while loop is:
do{
//statements
} while(condition);
Here the statements inside the brace are executed at least once, because its condition is at the bottom of the loop.
public class DoWhileLoop { public static void main(String[] args) { int i=1; do{ System.out.print("Hello "); i++; } while(i<=10); } }
The output of the above program is:
Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello
The for Loop: When we have the fixed number of the iteration known then we use for loop. The basic syntax of for loop is:
for (initialization; termination; increment/decrement){
//statements;
}
First before loop starts, the initialization statement is executed that initializes the variable or variables in the loop. Second the termination condition is evaluated, if it is true, the body of the for loop will be executed. Finally, the increment/decrement statement will be executed, and termination condition will be evaluated, this continues until the termination condition is false. Consider the following condition:
public class ForLoop { public static void main(String[] args) { int i; for(i=10;i>0;i--){ System.out.print(i+" "); } } }
The output of the above program is:
10 9 8 7 6 5 4 3 2 1
Nested Statements: We can define one control flow statements (if, if else) or looping (while, do-while, for) within another statement this is referred to as nested statement. When one loop is nested inside another loop, the inner loop is first terminated and again restarted when the first loop starts for the next incremented value. The outer loop is terminated last. Let us consider following program segments:
for(int i=1;i<=10;i++) { for(int j=1;j<=10;j++) { System.out.print(i*j+" "); } System.out.println(); }
This nested loop prints multiplication table
Jump Statements: In Java there are some jumping statements they are used to transfer of control from one part of the program to another part of the program. Java jump statements are: break, continue, and return.
The break Statement
The use of break statement causes the immediate termination of the loops and switch from the point of break statement to the statement following the loop. In case of nested loop, if the break statement is inside the inner loop then it breaks inner loop only. Consider the following program.
public class BreakStatement { public static void main(String[] args) { for(int i=0;i<=10;i++){ if(i==5) break; System.out.println(i+" "); } } }
The output of the above program:
0 1 2 3 4
The continue Statement: Continue statement causes the execution of the current iteration of the loop to cease, and then continue at the next iteration of the loop. Consider the following program that demonstrates the uses of continue statement which does not display 5 and 7 but displays 6,,8,9.
public class ContinueStatement { public static void main(String[] args) { for(int i=1;i<=10;i++){ if(i==5||i==7) continue; System.out.print(i+" "); } } }
The output of the above program is:
1 2 3 4 6 8 9 10
The return Statement: It is used to transfer the program control to the caller of the method. There are two forms of the return statement. First with general form return expression, returns the value whereas the second with the form return; returns no value but only the control to the caller. The following program demonstrates use of the return statement in the method that returns the sum of two numbers.
import java.util.Scanner; public class AddTest { public static int add(int a,int b){ return a+b; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter first number"); int a = scan.nextInt(); System.out.println("Enter second number "); int b = scan.nextInt(); int s = add(a,b); System.out.println("The sum of two numbers is "+s); } }
The output of the above program is:
Run:
Enter first number
10
Enter second number
20
The sum of two numbers is 30
Control statements are the statements that change the flow of execution of statements. For example, If, If-else, Switch-Case, while-do statements.
Also Read: Java Programming II | Old Paper Solution | BIM 5th Semester
control statements control statements control statements control statements control statements control statements control statements control statements control statements control statements control statements