[go: up one dir, main page]

0% found this document useful (0 votes)
78 views4 pages

Conditionals Statements

1. Conditional statements in Java include if, if-else, and if-else-if statements that execute different code blocks depending on boolean expressions. 2. An if statement executes the code block if the expression is true, while if-else executes the else block if the expression is false. 3. An if-else-if ladder checks multiple expressions in order, executing the block for the first true expression before moving on. The else block executes if all expressions are false.

Uploaded by

Francene Alvarez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views4 pages

Conditionals Statements

1. Conditional statements in Java include if, if-else, and if-else-if statements that execute different code blocks depending on boolean expressions. 2. An if statement executes the code block if the expression is true, while if-else executes the else block if the expression is false. 3. An if-else-if ladder checks multiple expressions in order, executing the block for the first true expression before moving on. The else block executes if all expressions are false.

Uploaded by

Francene Alvarez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Conditional Statements in Java Computer Programming I

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;
}

In an if statement, if the expression evaluates to true, the statement or block of statements


that forms the target of the if statement will be executed. Otherwise, the program will ignore
the statement or the block of statements.

Note:

Never place a semicolon after the expression in an if statement.

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:

Enter student’s grade:95


Congratulations you PASSED!
Conditional Statements in Java Computer Programming I

2. if- else statement


The general form of the if- else statement is:

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

statement_1 and statement_2 may either be a single Java statement or a block of


statements.
In an if –else- if ladder, the expression are evaluated from top downward. As soon as a
true condition is found, the statement associated with it is executed, and the rest of the ladder
will not be executed. If none of the condition is true, the final else is executed.
The final else acts as a default condition. If all other conditions are false, the last
statement_else is performed. If the final else is not present, then no action takes place.
Note: The final else is optional, you may include this part in the program or you may not include
if not needed.

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:

Year-entry number High School level


1 Freshman
2 Sophomore
3 Junior
4 Senior
Other entry no. Out of School

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

4. The Nested- if statement


One of the most confusing aspects of the if statement in any programming language is
nested if. A nested if is an if statement that is the object of either an if or else. This is sometimes
referred to as “an if within an if.”
Example 4. Write a program that will display the highest value among the three numbers.
Source code:
import java.util.Scanner;
public class Sample4
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
float num1,num2,num3;
System.out.print(“Enter the value of the first number:”);
num1=input.nextFloat();
System.out.print(“Enter the value of the second number:”);
num2=input.nextFloat();
System.out.print(“Enter the value of the third number:”);
num3=input.nextFloat();
if(num1==num2 && num1==num3)
System.out.print(“The three numbers are equal”);
else
{
if(num1>num2 && num1>num3)
System.out.print(“The highest value is ” + num1);
else if(num2>num3)
System.out.print(“The highest value is ” + num2);
else
System.out.print(“The highest value is ” + num3);
}
}
}
Sample output:
Enter the value of the first number:3
Enter the value of the second number:6
Enter the value of the third number:5
The highest value is 6

You might also like