JAVA PROGRAMMING
Dr B.Veera Jyothi
IT Department ,CBIT
Outline
s
Control Statements
Selection
Iteration
Jump
2
Selection
Statements
Selection Statements are also called Decision Making Statements.
Selection
Selection Statements
Switch Statement
3
if Statements
if Statements
Simple if
if else
if- else- if Ladder
Nested if
4
Simple if
Syntax :
if (condition)
{
statement1;
}
Purpose: The statements will be evaluated if the value of the condition is true.
5
Simple if
Flow Chart: Start
True False
Condition
Statements
End
6
Example
7
if else
Syntax :
if (condition)
{
statement1;
}
else
{
statement2;
}
Purpose: The statement 1 is evaluated if the value of the condition is true otherwise
statement 2 is true.
8
if else
Flow Chart: Start
True False
Condition
Statement 1 Statement 2
End
9
Example
10
Java program to illustrate if-else statement
class IfElseDemo
{
public static void main(String args[])
{
int i = 10;
if (i < 15)
System.out.println("i is smaller than 15");
else
System.out.println("i is greater than 15");
}
}
Output:
i is smaller than 15
Program to demonstrate if-else statement
public class Sample {
public static void main(String args[])
{
int a = 80, b = 30;
if (b > a)
{
System.out.println("b is greater");
} else
{
System.out.println("a is greater");
}
}
}
Output:
A is greater
If-else-if Ladder
Syntax :
if(condition)
statements;
else if(condition)
statements;
else if(condition)
statements;
...
...
else
statements;
11
Example
import java.util.Scanner;
{
class Day s else if (day == 3)
{
public static void main(String System.out.println("\n Wednesday");
args[]) }
{ else if (day == 4)
Scanner s = new Scanner(System.in); System.out.println("Enet {
day between 0 to 6 Day = "); int day = s.nextInt(); System.out.println("\n Thursday");
if (day == 0) }
{ else if (day == 5)
System.out.println("\n Sunday"); {
} System.out.println("\n Friday");
else if (day == 1) }
{ else
System.out.println("\n Monday"); {
} System.out.println("\n Saturday");
else if (day == 2) }
{ }
System.out.println("\n Tuesday"); }
}
12
Java program to illustrate if-else-if ladder
class ifelseifDemo
{
public static void main(String args[])
{
int i = 20;
if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}
Nested if
• A nested if is an if statement that is the target of another if or else.
• Nested ifs are very common in programming.
Syntax :
if(condition)
{
if(condition)
statements....
else
statements....
}
else
{
if(condition)
statements....
else
statements.
...
}
13
Example
14
switch
Syntax :
switch (expression)
{
case value 1 :
statement 1 ; break;
case value 2 :
statement 2 ; break;
...
...
case value N :
statement N ; break;
default :
statements ; break;
}
Purpose: The statements N
will be evaluated if the value 15
of the logical expression is
switch
Start
Flow Chart:
Variable or Expression
Case A Statements
Case A
break;
True
False
Case B Case B Statements
True break;
False
… Case C Statements
True break;
False
default Default Statements
End
16
Example
17
Iteration Statements
Iterations/ Loops
Each loop has four types of
statements :
while
Initialization
Condition checking
Execution
Increment / Decrement do while
for
18
while
Syntax:
m=1
initialization while(m<=20)
while(final value) {
{ System.out.println(m);
statements; m=m+1;
increment/decrement; }
}
Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.
19
Example
print values from 1 to 10
class while1 Output :
{ 1
public static void main(String args[]) 2
{ 3
int i=1; 4
while(i<=10) 5
{ 6
System.out.println("\n" + i); i+ 7
+; 8
} 9
} 10
}
20
do while
Syntax:
initialization do m=1
{ do
statements; {
increment/decrement; Syste
} m.out
while(final value); .printl
n(m);
m=m
+1;
}
Purpose: To evaluate the statementswhile(
from initial value to final value with given
increment/decrement. m==2
0);
21
Example
class dowhile1
{
public static void main(String args[])
{
int i = 1;
int sum = 0; do
{
sum = sum + i; i++;
}while (i<=10);
System.out.println("\n\n\tThe
sum of 1 to 10 is .. " + sum);
}
}
Output :
The sum of 1 to 10 is .. 55 22
for
Syntax:
for(initialization;final value;increment/decrement) for(m=1;m<=20;m=m+1)
{ {
statements; System.out.println(m);
} }
Purpose:To evaluate the statements from initial value to final value with given
increment/decrement.
23
Example
class for1
{
public static void main(String args[])
{
int i;
for (i=0;i<5;i++)
{
System.out.println("\nExample of for loop ");
}
}
Output :
Example of for loop Example
of for loop Example of for
loop Example of for loop
Example of for loop
24
Jump
Statements
Jump
break
continue
return
25
The break
statement
This statement is used to jump out of a loop.
Break statement was previously used in switch – case statements.
On encountering a break statement within a loop, the execution continues with the statementnext
outside the loop.
The remaining statements which are after the break and within the loop are skipped.
Break statement can also be used with the label of a statement.
A statement can be labeled as follows.
statementName : SomeJavaStatement
When we use break statement along with label as,
break statementName;
26
Example
class break1 Output :
{ 1
public static void main(String args[]) 2
{ 3
int i = 1; 4
while (i<=10)
{
System.out.println("\n" + i); i++;
if (i==5)
{
break;
}
}
}
}
27
continue Statement
This statement is used only within looping statements.
When the continue statement is encountered, the next iteration starts.
The remaining statements in the loop are skipped. The execution starts from the
top of loop again.
28
Example
class continue1 Output :
{ 1
public static void main(String args[]) 3
{ 5
for (int i=1; i<1=0; i++) 7
{ 9
if (i%2 == 0)
continue;
System.out.println("\n" + i);
}
}
}
29
The return
The
Statement
last control statement is return.The return statement is used to
explicitly return from a method.
That is, it causes program control to transfer back to the caller of the
method.
The return statement immediately terminates the method in which it is
executed.
30
Example
class Return1
{
public static void main(String args[])
{
boolean t = true; System.out.println("Before the
return."); if(t)
return; // return to caller Output :
System.out.println("This won't execute."); Before the return.
}
}
31
Thank You