Java
Java
Points to Remember
There can be one or N number of case values for a switch expression.
The case value must be of switch expression type only. The case value must be literal or
constant. It doesn't allow variables.
The case values must be unique. In case of duplicate value, it renders compile-time
error.
The Java switch expression must be of byte, short, int, long (with its Wrapper
type), enums and string.
Each case statement can have a break statement which is optional. When control
reaches to the break statement, it jumps the control after the switch expression. If a
break statement is not found, it executes the next case.
The case value can have a default label which is optional.
84
SALAR FADHIL
JAVA SWITCH
JAVA SWITCH STATEMENT
Syntax:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
case value3:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched
} SALAR FADHIL 85
JAVA SWITCH //Java Program to demonstrate the use of Java Switch
JAVA SWITCH STATEMENT //statement with String
public class SwitchStringExample {
public static void main(String[] args) {
public class SwitchExample {
public static void main(String[] args) { String levelString = "Expert";
//Declaring a variable for switch expression int level = 0;
int number=20;
//Switch expression switch(levelString){
switch(number){
//Case statements case "Beginner": level = 1;
case 10: System.out.println(“Ten"); break;
break; case "Intermediate": level = 2;
case 20: System.out.println(“Twenty"); break;
break; case "Expert": level = 3;
case 30: System.out.println(“Thirty"); break;
break; default: level = 0;
//Default case statement break;
default:System.out.println("Not in 10, 20 or 30"); }
} System.out.println("Your Level is: "+ level);
} }
} }
SALAR FADHIL 86
LOOPS IN JAVA
In programming languages, loops are used to execute a block of statements or a set of instructions / functions
repeatedly when some conditions become true. Each execution of the loop is called iteration in java.
Java language provides three types of loop statements for performing loop for loop
operations. They are: while loop
do-while loop
If the number of iteration is If the number of iteration is not If the number of iteration is
fixed, it is recommended to fixed, it is recommended to use not fixed and you must have
When to use use for loop. while loop. to execute the loop at least
once, it is recommended to
use the do-while loop.
System.out.println(“Hello Java”);
}
} Hello Java
} Hello Java
Hello Java
Hello Java
Hello Java
SALAR FADHIL 89
LOOPS IN JAVA
JAVA FOR LOOP
Syntax:
Java Program to demonstrate the example of for loop
for(initialization; condition; incr/decr){ which prints table of 1
//statement or code to be executed
}
Output: public class ForExample {
public static void main(String[] args) {
1 //Code of Java for loop
2
3 for( int i =1; i <= 10; i++ ){
4
5 System.out.println(i);
6 }
7 }
8 }
SALAR FADHIL 9 90
10
LOOPS IN JAVA
JAVA NESTED FOR LOOP
If we have a for loop inside the another loop, it is known as nested for loop. The inner
loop executes completely whenever outer loop executes.
System.out.println("infinitive loop");
}
}
}
93
SALAR FADHIL
LOOPS IN JAVA
JAVA FOR-EACH LOOP
Java for-each loop can also be used to retrieve elements of a collection. A collection represents a group of
elements like Integer values, strings, like an array or objects. It is easier to use than simple for loop
because we don't need to increment value and use subscript notation.
It works on the basis of elements and not the index. It returns element one by one in the defined variable.
Syntax:
public class ForEachExample {
for(data-type variable : array-name){
public static void main(String[] args) {
//code to be executed
}
//Declaring an array
int arr[]={ 12, 23, 44, 56, 78 };
Output: //Printing array using for-each loop
for( int i: arr ){
12
23 System.out.println(i);
44 }
56 }
78 }
SALAR FADHIL 94
LOOPS IN JAVA
JAVA WHILE LOOP
The Java while loop is used to
iterate a part of the program several
times.
SALAR FADHIL 95
LOOPS IN JAVA
JAVA WHILE LOOP
public class WhileExample {
public static void main(String[] args) {
Syntax:
int i = 1; Output:
while(condition){
while( i <= 10 ){ 1
2
//code to be executed 3
System.out.println(i);
4
} 5
i++; 6
7
} 8
} 9
} 10
SALAR FADHIL 96
LOOPS IN JAVA
JAVA INFINITIVE WHILE LOOP
If you pass true in the while loop, it will be infinitive while loop.
SALAR FADHIL 97
LOOPS IN JAVA
JAVA DO-WHILE LOOP
The Java do-while loop is used to iterate a part of the
program several times.
If the number of iteration is not fixed and you must have
to execute the loop at least once, it is recommended to
use do-while loop.
SALAR FADHIL 98
LOOPS IN JAVA
JAVA DO-WHILE LOOP
public class DoWhileExample {
public static void main(String[] args) {
Syntax:
int i = 1; Output:
do{
1
do{
//code to be executed 2
3
System.out.println(i); 4
}while(condition);
5
i++; 6
7
}while(I <= 10); 8
} 9
} 10
SALAR FADHIL
99
LOOPS IN JAVA
JAVA INFINITIVE DO-WHILE LOOP
If you pass true in the do-while loop, it will be infinitive do-while loop.
Syntax:
do{ public class DoWhileExample2 {
public static void main(String[] args) {
//code to be executed do{
}while(true);
}
}