Introduction
A program executes from top to bottom, except when we use control statements. Then, we can control the order of execution of the program, based on logic and the values.
In Java, control statements can be divided into the following three categories:
- Selection Statements
- Iteration Statements
- Jump Statements
Selection Statements
Selection statements allow you to control the flow of program execution, on the basis of the outcome of an expression or state of a variable, known during runtime.
Selection statements can be divided into the following categories:
- The if and if-else statements
- The if-else statements
- The if-else-if statements
- The switch statements
The if statements
The first contained statement (that can be a block) of an if statement, only executes when the specified condition is true. If the condition is false and there is no keyword, then the first contained statement will be skipped and execution continues with the rest of the program. The condition is an expression that returns a boolean value.
Example
- package com.deepak.main;
- import java.util.Scanner;
- public class IfDemo {
- public static void main(String[] args) {
- int age;
- Scanner inputDevice = new Scanner(System.in);
- System.out.print("Please enter Age: ");
- age = inputDevice.nextInt();
- if (age > 18)
- System.out.println("above 18 ");
- }
- }
Output
The if-else statements
In if-else statements, if the specified condition in the if the statement is false, then the statement after the else keyword (that can be a block) will execute.
Example
- package com.deepak.main;
- import java.util.Scanner;
- public class IfElseDemo {
- public static void main(String[] args) {
- int age;
- Scanner inputDevice = new Scanner(System.in);
- System.out.print("Please enter Age: ");
- age = inputDevice.nextInt();
- if (age >= 18)
- System.out.println("above 18 ");
- else
- System.out.println("below 18");
- }
- }
Output
The if-else-if statements
The statement following the else keyword can be another if or if-else statement.
That would look like this:
- if (condition)
- statements;
- else if (condition)
- statements;
- else if (condition)
- statement;
- else
- statements;
Whenever the condition is true, the associated statement will be executed and the remaining conditions will be bypassed. If none of the conditions are true, then the else block will execute.
Example
- package com.deepak.main;
- import java.util.Scanner;
- public class IfElseIfDemo {
- public static void main(String[] args) {
- int age;
- Scanner inputDevice = new Scanner(System.in);
- System.out.print("Please enter Age: ");
- age = inputDevice.nextInt();
- if (age >= 18 && age <= 35)
- System.out.println("between 18-35 ");
- else if (age > 35 && age <= 60)
- System.out.println("between 36-60");
- else
- System.out.println("not matched");
- }
- }
Output
The Switch Statements
The switch statement is a multi-way branch statement. The switch statement of Java is another selection statement that defines multiple paths of execution, of a program. It provides a better alternative than a large series of if-else-if statements.
Example
- package com.deepak.main;
- import java.util.Scanner;
- public class SwitchDemo {
- public static void main(String[] args) {
- int age;
- Scanner inputDevice = new Scanner(System.in);
- System.out.print("Please enter Age: ");
- age = inputDevice.nextInt();
- switch (age) {
- case 18:
- System.out.println("age 18");
- break;
- case 19:
- System.out.println("age 19");
- break;
- default:
- System.out.println("not matched");
- break;
- }
- }
- }
Output
An expression must be of a type of byte, short, int, or char. Each of the values specified in the case statement must be of a type compatible with the expression. Duplicate case values are not allowed. The break statement is used inside the switch to terminate a statement sequence. The break statement is optional in the switch statement.
Iteration Statements
Repeating the same code fragment several times, until a specified condition is satisfied, is called iteration. Iteration statements execute the same set of instructions, until a termination condition is met.
Java provides the following loop for iteration statements:
- The while loop
- The for loop
- The do-while loop
- The for-each loop
The while loop
It continually executes a statement (that is usually a block) while a condition is true. The condition must return a boolean value.
Example
- package com.deepak.main;
- public class WhileDemo {
- public static void main(String[] args) {
- int i = 0;
- while (i < 5) {
- System.out.println("Value :: " + i);
- i++;
- }
- }
- }
Output
Vithal WadjePosted Jan 1, 2015, 2:19 AM
good start