Week9 Module8 JavaControlStructures Conditional
Week9 Module8 JavaControlStructures Conditional
Module 8
CONTROL STRUCTURES
Learning Objectives:
At the end of this lesson, the students shall be able to:
1. Learn how to use decision control structures (if, else, switch) which allows
selection of specific sections of code to be executed.
2. Create a Java program using different types of conditional Structures.
if statement
The if-statement specifies that a statement (or block of code) will be
executed if and only if a certain boolean statement is true.
1|Page Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
or
if (boolean_expression )
{
statement1;
statement2;
...
}
or
2|Page Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
Example 1:
//if statement
import java.util.Scanner;
public class IfSample1a {
public static void main(String args[]) {
Scanner scan = new
Scanner(System.in);
int grade;
Example 2:
//if statement
import java.util.Scanner;
3|Page Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
if-else statement
The if-else statement is used when we want to execute a certain
statement if a condition is true, and a different statement if the condition is
false.
The if-else statement has the form,
if (boolean_expression)
statement;
else
statement;
if (boolean_expression)
{
statement1;
statement2;
...
}
else
{
statement1;
statement2;
...
}
4|Page Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
Example 1:
//if-else statement
import java.util.Scanner;
public class IfElseSample1a {
public static void main(String args[]) {
Scanner scan = new
Scanner(System.in);
int grade;
Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
5|Page
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
Example 2:
//if-else statement
import java.util.Scanner;
Example 3:
import java.util.Scanner;
public class IfElseSample2a {
public static void main(String args[]) {
String pass="jru";
if (pass=="jru")
System.out.println("accepted");
else
System.out.println("invalid");
}}
6|Page Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
Example 4:
import java.util.Scanner;
public class IfElseSample2b {
public static void main(String args[]) {
String pass;
Scanner scan = new Scanner(System.in);
if (pass=="jru")
System.out.println("accepted");
else
System.out.println("invalid");
} }
Example 5:
import java.util.Scanner;
public class IfElseSample2c {
public static void main(String args[]) {
String pass;
Scanner scan = new Scanner(System.in);
if (pass.equals("jru"))
System.out.println("accepted");
else
System.out.println("invalid");
}}
7|Page Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
equals() Method
Description
The method determines whether the Number object that invokes the
method is equal to the object that is passed as an argument.
Syntax
public boolean equals(Object o)
Parameters
Here is the detail of parameters −
Any object.
Return Value
The method returns True if the argument is not null and is an
object of the same type and with the same numeric value. There
are some extra requirements for Double and Float objects that are
described in the Java API documentation.
Example 6:
System.out.println(x.equals(y));
System.out.println(x.equals(z));
System.out.println(x.equals(a));
}}
String pass;
Scanner scan = new Scanner(System.in);
if (pass.equalsIgnoreCase("jru"))
System.out.println("accepted");
else
System.out.println("invalid");
}}
equalsIgnoreCase() Method
Description
This method compares this String to another String, ignoring case
considerations. Two strings are considered equal ignoring case, if they are
of the same length, and corresponding characters in the two strings are
equal ignoring case.
Syntax
public boolean equalsIgnoreCase(String anotherString)
Parameters
Here is the detail of parameters –
Return Value
This method returns true if the argument is not null and the
Strings are equal, ignoring case; false otherwise.
9|Page Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
Example 8:
Output
Returned Value = true
Returned Value = true
Returned Value = true
if-else-if statement
The statement in the else-clause of an if-else block can be another if-
else structure. This cascading of structures allows us to make more complex
selections.
10 | P a g e Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
Take note that you can have many else-if blocks after an if-statement.
The else- block is optional and can be omitted. In the example shown above,
if oolean_expression1 is true, then the program executes statement1 and
skips the other statements. If boolean_expression2 is true, then the
program executes statement 2 and skips to the statements following
statement3.
11 | P a g e Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
12 | P a g e Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
Example 1:
import javax.swing.JOptionPane;
class IfElseIfSample1a {
public static void main(String[] args) {
int testScore = 76;
char grade;
else {
grade = 'F';
}
JOptionPane.showMessageDialog(null, "Grade = " + grade);
} }
13 | P a g e Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
Example 2:
import javax.swing.JOptionPane;
class IfElseIfSample1b {
int testScore;
char grade;
14 | P a g e Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
Example 3:
import java.io.*;
public class IfElseIfSample2 {
public static void main(String args[]) throws IOException{
15 | P a g e Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT
College of Computer Studies and Engineering
INFORMATION TECHNOLOGY DEPARTMENT
ITC c105 – Computer Programming using Java
Example 3:
//************************************************************************
// MixedTypeInput
// This application demonstrates testing before reading to be
// sure to use the correct input method for the data.
//************************************************************************
import java.io.*;
import java.util.Scanner;
double number;
Scanner in = new Scanner(System.in);
if (in.hasNextInt())
{
number = (double)in.nextInt();
System.out.println("You entered " + number);
}
else if (in.hasNextFloat())
{
number = (double)in.nextFloat();
System.out.println("You entered " + number);
}
else if (in.hasNextDouble())
{
number = in.nextDouble();
System.out.println("You entered " + number);
}
else
System.out.println("Token not an integer or a real value.");
}}
Enter your gross
income: 55000
You entered 55000.0
Compiled by: Prof. Mary Ann B. Taduyo, MAEd, MIT and Prof. RoelL C. Traballo, MSIT