Java Chapter 4
Java Chapter 4
1. If statement
2. If-Else statement
3. If-Else-If statement
4. Nested If statement
5. Switch statement
6. Jump statement
If Statement
An if statement consists of boolean expression followed by
one or more statements. Based on the result of these
expression programs are executed i.e. if condition true then
if block executed. If statement does not any other block
except if block.
Syntax –
if(Condition) {
// statements;
}
If statement
If_Example.java
Syntax –
if(Condition) {
//Statements;
}
else {
// Statements;
}
If-else statement
IfElseExample.java
public class IfElseExample {
public static void main(String args[]) {
int x = 10;
if(x % 2 ==0) {
System.out.println(“x is even number.”);
}
else {
System.out.println(“x is odd number.”);
}
}
}
Syntax –
if(Condition) {
//Statements;
if(Condition) {
//Statements;
}
}
Nested if statement
NestedIfExample.java
public class NestedIf {
public static void main(String args[]) {
int marks = 75;
int age=21
if(age>= 21) {
if(marks >=75){
System.out.println(“Elligible.”);
}
}
}
}
Output - Elligible.
Switch Statement
Switch statement is work just like if-else-if ladder i.e. executes one statement from multiple conditions. There
are following important points about switch statement:
SwitchExample2.java
public class SwitchExample1 { switch(ch) {
public static void main(String args []) { case ‘a’: System.out.println(“Vowel”); break;
int num = 2; case ‘A’: System.out.println(“Vowel”); break;
switch(num) { case ‘e’: System.out.println(“Vowel”); break;
case 1: System.out.println(“Case-1”); break; case ‘E’: System.out.println(“Vowel”); break;
case 2: System.out.println(“Case-2”); break; case ‘i’: System.out.println(“Vowel”); break;
case 3: System.out.println(“Case-3”); break; case ‘I’: System.out.println(“Vowel”); break;
default: System.out.println(“Invalid”); case ‘o’: System.out.println(“Vowel”); break;
} case ‘O’: System.out.println(“Vowel”); break;
} case ‘u’: System.out.println(“Vowel”); break;
} case ‘U’: System.out.println(“Vowel”); break;
default: System.out.println(“Consonant”);
Output – Case-2 }}}
Output – Vowel
jump Statement
There are three types of Jump Statement in java:
1. Break Statement
2. Continue Statement
3. Return Statement
break Statement
The java break statement is used to break loop or switch statement. Java Break statement breaks the current
flow of the program i.e. in case of inner loop, it breaks only inner loop not outer and control will resumes
at the next statement.
Break statement
BreakLoopDemo.java
// Break to exit a loop
class BreakLoopDemo
{
public static void main(String args[])
{
for (int i = 0; i < 10; i++)
{
Output –
if (i == 5)
i: 0
break;
i: 1
i: 2
System.out.println("i: " + i);
i: 3
}
i: 4
System.out.println("Loop complete.");
Loop complete.
}
}
break Statement
// with label inside an inner loop to break outer loop
Java Break statement with Labeled public class BreakExample3 {
For loop: public static void main(String[] args) {
aa: // Name of label
Java does not have goto statement as for(int i=1;i<=3;i++){
java uses label. We can use break bb: // Name of label
statement with a label. This feature of for(int j=1;j<=3;j++){
break statement introduced since if(i==2&&j==2){
JDK1.5.
break aa;
}
System.out.println(i+" "+j); Output –
} 11
} 12
} 13
} 21
continue Statement
The continue statement is used in loop control structure when you need to jump to the next iteration of the
loop immediately. The Java continue statement is used to continue the loop. It continues the current flow
of the program and skips the remaining code at the specified condition. In case of an inner loop, it
continues the inner loop only.
continue statement
ContinueExample.java
// Continue Statement
In programming, loops are used to execute instruction again & again (i.e. repeatedly) when some condition
become true.
•for loop
•while loop
•do-while loop
For Loop
The for loop is used to execute a sequence of statements multiple times. If the number of execution is fixed, it
is recommended to use for loop. The syntax of for statement contains initialization, condition and
increment/decrement in one line with separated by semi-colon.
Syntax –
A for loop inside another for loop is called nested for loop. In nested for loop, inner loop executes completely
whenever outer loop executes.
For-each Loop - In java for-each loop is used to traverse an array or collection. It returns one by one element
in the defined variable.
Syntax -
labelname;
for(initialization; condition; increment/decrement) {
Statements;
}
For Loop
LabeledForLoopExample.java
Syntax –
while(condition) {
Statements;
}
while Loop
// While_Example.java
Syntax –
do{
Statements;
}while(condition);
Do-while Loop
// Do_While_Example.java