Conditionals Statements
Conditionals Statements
Conditional statements are statements that check an expression then may or may not
execute statement or group of statements depending on the result of the condition.
In Java, the following are the types of conditional statements:
1. if statement
The general form of the if statement is:
if (expression)
statement;
Where:
if is a reserve word in Java
expression is a relational or logical expression that evaluates to a true or false value.
statement may be either be a single Java statement or a block of statements.
The general form of the if statement with block of statement is:
if (expression)
{
statement_sequence;
}
Note:
Example 1. Write a program that will display “Congratulations you PASSED!” if the student’s
grade is greater than or equal to 70
Source code:
import java.util.Scanner;
public class Sample1
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
float grade;
System.out.print(“Enter student’s grade:”);
grade=input.nextFloat();
if(grade>=70)
System.out.print(“Congratulations you PASSED!”);
}
}
Sample Output:
if (expression)
statement_1;
else
statement_2;
Where:
if and else is a reserve words in Java
expression is relational or logical expression that evaluates to a true or false value
statement_1 and statement_2 may either be a single Java statement or a block of
statements.
The general form of the if- else statement with block of statements is:
if (expression
{
statement_sequence1;
}
else
{
statement_sequence2;
}
In an if- else statement, if the expression is true, the statement or block of statements
after if statement will be executed; otherwise, the statement or block of statements in the else
statement will be executed.
Example 2. Write a program that will output “Congratulations you PASSED!” if the student’s
grade is greater than or equal to 70. Otherwise output, “Sorry you FAILED!”
Source code:
import java.util.Scanner;
public class Sample2
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
float grade;
System.out.print(“Enter student’s grade:”);
grade=input.nextFloat();
if(grade>=70)
System.out.print(“Congratulations you PASSED!”);
else
System.out.print(“Sorry you FAILED!”);
}
}
Sample Output:
Enter student’s grade:50
Sorry you FAILED!
Conditional Statements in Java Computer Programming I
3. if –else- if ladder
The general form of the if –else- if ladder statement is:
if (expression_1)
statement_1;
else if (expression_2)
statement_2;
else if (expression_3)
statement_3;
:
:
else
statement_else;
Where:
if and else are reserve words in Java
expression is a relational or logical expression that evaluates to a true or false value
Example 3. Write a program that will display the high school level of a student, based on its year-
entry. For example, the year entry 1 means the student is a freshman, 2 is a sophomore, and so
on. Here are the given criteria:
Source code:
import java.util.Scanner;
public class Sample3
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int year_entry;
System.out.print("Enter year-entry number: ");
year_entry=input.nextInt();
if(year_entry==1)
System.out.println("Freshman");
else if(year_entry==2)
System.out.println("Sophomore");
Conditional Statements in Java Computer Programming I
else if(year_entry==3)
System.out.println("Junior");
else if(year_entry==4)
System.out.println("Senior");
else
System.out.println("Out of School");
}
}
Sample output:
Enter year-entry number: 5
Out of School