Topics
Types of Java Statements
Conditional Statements
Using if() Statement
Using switch () Statement
Java Statements
JAVA STATEMENTS
Java statement denotes specific action that you want the
program to perform.
Types of Java Statements
• Declarative statement
• Assignment statement
• Conditional statement
.
JAVA STATEMENTS
Declarative Statements
• These are statements used when preparing a particular user-
defined objects such as class, variable, method, array, string
and other user-defined objects.
Example
• public class Activity1 { } // creating/declaring a class
• public static void main() { } // creating a method
• int age; // creating a variable
• float grades[20]; // creating an array
• String stdName=“MJ”; // creating a String
JAVA STATEMENTS
Assignment Statement
• A type of statement used when assigning value to a
variable.
Example:
a. int age; // declaration statement
age = 15; // assignment statement
b. String stdName;
stdName = “MJ”;
c. float grades[5];
grades[0] = 75.5f;
d. char middleInitial;
middleInitial = ‘C’;
JAVA STATEMENTS
Program-Control Statements
• These are statements that govern or dictate the flow of
execution within a Java program.
Importance of Program-Control Statements
Can make the program more adaptable and flexible to
handle different situations.
Can shorten the length of a program and make it more
efficient.
Reduces code redundancy.
Two types of Program-control statements
• Conditional statements
• Loop-control statements
JAVA STATEMENTS
Block of Statements
• Java statement can be a single statement or a block of
statements.
• A block of statements are logically arranged statements
executed as a group or a block.
• Block of statements are enclosed inside curly braces.
Example of single statements
public class PrintHello
public static void main( String[] args )
System.out.println(“Hello”);
JAVA STATEMENTS
Example: Block of statements
public class PrintSum {
static float sum;
public static void main( String[] args ) {
int n1, n2;
n1 = 5; block of
block of statements
n2 = 10; statements
sum = n1 + n2;
System.out.println(sum);
}
}
JAVA STATEMENTS
Enclosing Block of Statements
Example:
public class PrintSum
{
public static void main( String[] args )
{
int n1, n2, sum;
n1 = 5;
n2 = 10;
sum = n1 + n2;
System.out.println(sum);
}
}
CONDITIONAL STATEMENTS
Type of program-control statement which causes the
program to perform certain action/s when a particular
condition is satisfied or not satisfied.
• Used when program when there is selective execution of
statements.
• Used when program is dealing with a table of alternative
actions.
• Will involve relational and logical operators.
• Java supports two conditional statements
• if() statement
• switch() statement
CONDITIONAL STATEMENTS
if() statement Example 1:
• Allows multi-way tests public class GetMax {
public static void main(String[] args ) {
and can be used for int n1, n2, high;
any type of n1 = 5; n2 = 3; high = 0;
if ( n1 > n2 )
comparison. high = n1;
else
Syntax #1: high = n2;
if ( condition ) System.out.println(
statement1; “Higher number is “ + high);
}
else }
statement2;
where:
condition is a comparison
between two entities.
CONDITIONAL STATEMENTS
Syntax #2: Example #2:
public class CalcTax {
if ( conditon1 )
public static void main( String[] args ) {
statement1; float gross, rate = 150F, hrs = 20F;
else if ( conditon2 ) float tax = 0f;
statement2; gross = rate * hrs;
else if ( conditon3 ) if ( gross > 5000 ) tax = .2f * gross;
else if ( gross > 4000 ) tax = .15f * gross;
statement3;
else if ( gross > 3000 ) tax = .1f * gross;
else if ( conditon4 ) else if ( gross > 2000 ) tax = .05f * gross;
statement4; else if ( gross > 1000 ) tax = .025f * gross;
……. else tax = 0f;
else if ( conditionN-1 ) System.out.println(“Rate Per Hour is “ + rate);
System.out.println(“Hours Worked is “ + hrs);
statementN-1;
System.out.println(“Grosswage is “ + gross);
else System.out.println(“Tax is “ + tax);
statementN; }
}
CONDITIONAL STATEMENTS
Syntax #3:
if ( condition )
Note:
statement;
• This format of if()
statement must be
Example #3-1: applied if there is only
n1 = 5; n2 = 3; high = n1; one aternative flow of
if ( n2 > n1 ) action.
high = n2;
Example #3-2:
gross = rate * hrs; tax = 0;
if ( gross > 1000 )
tax = .1f * gross;
CONTROL-STATEMENTS
switch() statement
• Also handles multiple alternatives but unlike if(), this
statement can only test for equality relationship.
Rules:
Syntax :
• Type af variable and
switch ( variable ) { each constant must
case constant1: statement1; match.
case constant2: statement2; • Type of variable must
case constant3: statement3; either be a character
variable or an integer
case constant4: statement4;
variable.
…………
case constantN-1: statementN-1;
default: statementN;
}
CONDITIONAL STATEMENTS
Example #4-1: Note:
public class PrintTitle { • Each case must
public static void main ( String[] args ) { end with a break
statement
char sc = ‘F’; • A break statement
String title = “”; is used to exit from
switch ( sc ) { a switch. Failure to
issue this
case ‘F’ : title = “Ms. “; break; statement will
case ‘M’ : title = “Mr. “; break; cause the
default : title = “”; remaining
statements to also
} execute
}
CONDITIONAL STATEMENTS
Example #4-2:
public class PrintNumInWord {
public static void main ( String[] args ) {
int num = 5;
String word = “”;
switch ( num ) {
case 0 : word = “Zero”; break;
case 1 : word = “One”; break;
case 2 : word = “Two”; break;
}
}