0% found this document useful (0 votes)
4 views

Java

The document provides an overview of the Java switch statement and various loop constructs including for, while, and do-while loops. It explains the syntax, usage, and examples of each loop type, highlighting key points such as the conditions for using each loop and the concept of nested loops. Additionally, it covers labeled loops and infinite loops, demonstrating their implementation in Java programming.

Uploaded by

heranjaf18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java

The document provides an overview of the Java switch statement and various loop constructs including for, while, and do-while loops. It explains the syntax, usage, and examples of each loop type, highlighting key points such as the conditions for using each loop and the concept of nested loops. Additionally, it covers labeled loops and infinite loops, demonstrating their implementation in Java programming.

Uploaded by

heranjaf18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

JAVA SWITCH

JAVA SWITCH STATEMENT


The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder
statement.

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.

for(int; condition; incr / decr){ while(condition){ do{


Syntax
// code to be executed //code to be executed //code to be executed
} } }while(condition);
SALAR FADHIL 87
LOOPS IN JAVA
JAVA FOR LOOP
A simple For-Loop is the same as C/C++. We can initialize the variable, check condition and
increment / decrement value. It consists of three parts:
1. Initialization of control variables: A for loop generally uses a variable to control how many times the
body of loop will be executed and when the body of loop terminates.
2. Test-condition: The value of control variable is evaluated using the test condition. The test condition is a
Boolean expression, such as i < 10 that determines when the loop will exit.
3. Iteration: When the loop body is executed, the control of execution is transferred back to for statement
after executing the last statement in the loop.

Syntax: for( int i = 1 ; i <= 10 ; i++ ){


for(initialization; condition; increment/decrement){
//statement or code to be executed System.out.println(i);
}
}
SALAR FADHIL 88
LOOPS IN JAVA
JAVA FOR LOOP
Java Program to demonstrate the
example of for loop
public class ForExample {
public static void main(String[] args) {

//Code of Java for loop

for( int I = 1; I <= 5; i++ ){

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.

public class NestedFor { public class Pyramid {


public static void main(String[] args) { public static void main(String[] args)
{
//loop of i Output: for( int i=1; i<=5; i++ ){
for( int i=1; i<=3; i++ ){
11 for( int j=1; j<=i; j++ ){
//loop of j 12
for( int j=1; j<=3; j++ ){ 13 System.out.print("* "); Output:
21
22 *
System.out.println(i+" "+j); }
23 **
} //end of i System.out.println();//new line
31 ***
} //end of j }
32 ****
} }
33 *****
} }
SALAR FADHIL 91
LOOPS IN JAVA
JAVA LABELED FOR LOOP
A Java program to demonstrate the use of labeled for loop
We can have a name of each Java
for loop. public class LabeledFor {
To do so, we use label before the public static void main(String[] args) {
for loop. It is useful if we have //Using Label for outer and for loop
nested for loop so that we can aa:
break/continue specific for loop. for( int i=1; i<=3; i++ ){
Usually, break and continue bb: Output:
keywords breaks/continues the for( int j=1; j<=3; j++ ){
innermost for loop only. 11
if( i==2 && j==2 ){
12
break aa;
13
}
21
System.out.println(i+" "+j);
}
}
}
SALAR FADHIL
}
92
LOOPS IN JAVA
JAVA INFINITIVE FOR LOOP
If you use two semicolons ;; in the for loop, it will be infinitive for loop.

public class InfinitiveFor {


public static void main(String[] args) {

//Using no condition in for loop


for(;;){

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.

If the number of iteration is not


fixed, it is recommended to use
while loop.

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.

public class WhileExample2 {


Syntax: public static void main(String[] args) {
while(true){
while(true){
//code to be executed
System.out.println("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.

The Java do-while loop is executed at least once because


condition is checked after loop body.

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); System.out.println("infinitive do while loop");

}while(true);
}
}

SALAR FADHIL 100

You might also like