A java statement is the smallest unit that is a complete instruction itself. Statements in java generally contain expressions and ended with a semi-colon. The most commonly used statements in Java program are
- Empty statements
- Expression statements
- Labelled statements
- Control statements
- Synchronized statements
- Guarding statements
- Empty statements :- These no nothing and are used during programming development as a place holder.
- Expression statements : Most statements are expression statements. Java has several types of expression statements like assignment, per-increment / Decrements ext..
- Labelled statements: Any statement may being with a label. Such label must not be a keywords, already declared local variables or previous used labels in this module.
- Control statements: Control statements are three types selection, iteration, and jump statements.
- Synchronized statements: These are used handling issues with multi threading.
- Guarding statements : These are used for safe handling of code that may cause exceptions. These statements use the keywords try, catch, and finally.
The following control statements are available in Java
- If-else statement
- do-while statement
- For loop
- for-each loop
- switch statement
- break statement
- continue statement
- return statement
I will discuss all these statements in three parts. Here i will discuss if-else and switch statements
1. If-else statement
This statement is used to perform a task depending on whether a given condition is true or false. Here, task represents a single statement or a group of statements. The syntax is as follow
if(condition)
statements1;
[ else ststements2]
The logic behind the execution is very simple,
The compiler first check the condition associated with if, if it is true then statement1 is executed otherwise statement2 is executed. Here the statements may be single line or more line of code. If more code then we use { } to enclose it.
The following Java program check if the given number is even or odd number
import java.util.*; class EvenOdd { public static void main(String[] args) { int num; Scanner scr = new Scanner(System.in); System.out.println(" Enter any number"); num=scr.nextInt(); if(num%2==0) { System.out.println(" entered number is even"); } else { System.out.println("entered number is odd"); } } }
Once can different variations of if-else statements depending upon requirement like nested if-else or else-if ladder ext.
Output:
The Switch statement
When there are several options and we want to choose only one option from the available ones, we can use switch statement. Depending on the selected option, a particular task can be performed. A task represents one or more statements.
The syntax of switch statement is
switch(variable)
{
case value1:
Satements1;
break;
case value2:
Satements2;
break;
case value3:
Satements3;
break;
case value4:
Satements4;
break;
.
.
.
case value N:
Statement N;
break;
default:
default statements
}
The logic behind the execution of switch case statement is simple.
First, the value / expression in associated switch is evaluated
Second, The evaluated value compared with every case value. If the value is matched then that case is executed. This is because the break statement breaks execution.
If none of the case value is matched the default case is executed.
The switch case statement is a multi way selection statement.
The following program shows the working of switch case statement.
import java.util.*; class SwitchDemo { public static void main(String[] args) { int day; Scanner scr=new Scanner(System.in); System.out.println("Enter day number"); day=scr.nextInt(); switch(day) { case 1: System.out.println(" Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println(" Monday"); break; default: System.out.println(" Enter value between (1-7) only"); } } }
The following is the output
0 comments :
Post a Comment
Note: only a member of this blog may post a comment.