Java Class -6
Java Class -6
1 if statement
An if
statement c
onsists of a
boolean
expression
followed by
one or more
statements.
2 if...else
statement
An if
statement c
an be
followed by
an
optional els
e
statement,
which
executes
when the
boolean
expression
is false.
3 nested if
statement
You can use
one if or els
e
ifstatement
inside
another if or
else
if statement(
s).
4 switch
statement
A switch sta
tement
allows a
variable to
be tested for
equality
against a list
of values.
If statement in Java
An if statement consists of a Boolean
expression followed by one or more
statements.
Syntax
Following is the syntax of an if
statement −
if(Boolean_expression) {
// Statements will execute
if the Boolean expression is
true
}
If the Boolean expression evaluates to
true then the block of code inside the if
statement will be executed. If not, the
first set of code after the end of the if
statement (after the closing curly brace)
will be executed.
Flow Diagram
Example
if( x < 20 ) {
System.out.print("This is if
statement");
}
}
}
This will produce the following result −
Output
This is if statement.
if( x < 20 ) {
System.out.print("This is if
statement");
}else {
System.out.print("This is else
statement");
}
}
}
This will produce the following result −
Output
This is else statement
The if...else if...else Statement
Syntax
Following is the syntax of an if...else
statement −
if(Boolean_expression 1) {
// Executes when the Boolean expression
1 is true
}else if(Boolean_expression 2) {
// Executes when the Boolean expression
2 is true
}else if(Boolean_expression 3) {
// Executes when the Boolean expression
3 is true
}else {
// Executes when the none of the above
condition is true.
}
Example
if( x == 10 ) {
System.out.print("Value of X is
10");
}else if( x == 20 ) {
System.out.print("Value of X is
20");
}else if( x == 30 ) {
System.out.print("Value of X is
30");
}else {
System.out.print("This is else
statement");
}
}
}
This will produce the following result −
Output
Value of X is 30
if( x == 30 ) {
if( y == 10 ) {
System.out.print("X = 30 and Y
= 10");
}
}
}
}
This will produce the following result −
Output
X = 30 and Y = 10
1 while loop
Repeats a
statement
or group of
statement
s while a
given
condition
is true. It
tests the
condition
before
executing
the loop
body.
2 for loop
Execute a
sequence
of
statement
s multiple
times and
abbreviate
s the code
that
manages
the loop
variable.
3 do...while
loop
Like a
while
statement,
except
that it tests
the
condition
at the end
of the loop
body.
For loop in Java with example
System.out.println("The value
of i is: "+i);
}
}
}
The output of this program is:
The value of i is: 10
The value of i is: 9
The value of i is: 8
The value of i is: 7
The value of i is: 6
The value of i is: 5
The value of i is: 4
The value of i is: 3
The value of i is: 2
In the above program:
int i=10 is initialization expression
i>1 is condition(Boolean expression)
i– Decrement operation
Infinite for loop
The importance of Boolean expression
and increment/decrement operation co-
ordination:
class ForLoopExample2 {
public static void
main(String args[]){
for(int i=1; i>=1;
i++){
System.out.println("The value
of i is: "+i);
}
}
}
This is an infinite loop as the condition
would never return false. The
initialization step is setting up the value
of variable i to 1, since we are
incrementing the value of i, it would
always be greater than 1 (the Boolean
expression: i>1) so it would never return
false. This would eventually lead to the
infinite loop condition. Thus it is
important to see the co-ordination
between Boolean expression and
increment/decrement operation to
determine whether the loop would
terminate at some point of time or not.
Here is another example of infinite for
loop:
// infinite loop
for ( ; ; ) {
// statement(s)
}
For loop example to iterate an
array:
Here we are iterating and displaying
array elements using the for loop.
class ForLoopExample3 {
public static void
main(String args[]){
int
arr[]={2,11,45,9};
//i starts with 0 as
array index starts with 0 too
for(int i=0;
i<arr.length; i++){
System.out.println(arr[i]);
}
}
}
Output:
2
11
45
9
10. Java Program to find sum of natural
numbers using for loop
11. Java Program to find factorial of a
number using loops
12. Java Program to print Fibonacci
Series using for loop
while( x < 20 ) {
System.out.print("value of x :
" + x );
x++;
System.out.print("\
n");
}
}
}
This will produce the following result −
Output
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
do {
System.out.print("value of x :
" + x );
x++;
System.out.print("\
n");
}while( x < 20 );
}
}
This will produce the following result −
Output
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
1 break
statement
Terminates
the loop or
switch
statement
and
transfers
execution to
the
statement
immediately
following the
loop or
switch.
2 continue
statement
Causes the
loop to skip
the
remainder
of its body
and
immediately
retest its
condition
prior to
reiterating.
System.out.println("Value of
variable is: "+num);
if (num==2)
{
break;
}
num++;
}
System.out.println("Out
of while-loop");
}
}
Output:
Value of variable is: 0
Value of variable is: 1
Value of variable is: 2
Out of while-loop
Example – Use of break in a for
loop
The same thing you can see here. As
soon as the var value hits 99, the for
loop gets terminated.
public class BreakExample2 {
public static void
main(String args[]){
int var;
for (var =100; var>=10; var
--)
{
System.out.println("var:
"+var);
if (var==99)
{
break;
}
}
System.out.println("Out of
for-loop");
}
}
Output:
var: 100
var: 99
Out of for-loop
Example – Use of break statement
in switch-case
public class BreakExample3 {
switch (num)
{
case 1:
System.out.println("Case 1
");
break;
case 2:
System.out.println("Case 2
");
break;
case 3:
System.out.println("Case 3
");
break;
default:
System.out.println("Default
");
}
}
}
Output:
Case 2
System.out.print(j+" ");
}
}
}
Output:
0 1 2 3 5 6
Example: Use of continue in While
loop
Same thing you can see here. We are
iterating this loop from 10 to 0
for countervalue and when the counter
value is 7 the loop skipped the print
statement and started next iteration of
the while loop.
public class ContinueExample2
{
System.out.print(counter+"
");
counter--;
}
}
}
Output:
10 9 8 6 5 4 3 2 1 0
Example of continue in do-While
loop
public class ContinueExample3
{
public static void
main(String args[]){
int j=0;
do
{
if (j==7)
{
j++;
continue;
}
System.out.print(j+ "
");
j++;
}while(j<10);
}
}
Output:
0 1 2 3 4 5 6 8 9