ITC C106 Lecture - Decision Control Structures
ITC C106 Lecture - Decision Control Structures
Programming II
Module 1
Java Decision Structures
Prepared by: A. C. Valderama – CSE- IT
Dept.
Module Objectives
• Review and recognize the concept and code
programs using decision control structures (if,
if else, if else if, switch) which allows selection
of specific sections of code to be executed.
• Recall the use of relational and logical
operators and flags.
• Induce coding with order of operator
precedence, numeric ranges, String objects
comparison and the use of the DecimalFormat
class
Prepared by: A. C. Valderama – CSE- IT
Dept.
Content Outline
if (hasTiktok)
{ Do you
have a Yes
displayUsername();
postVideo(); Tiktok
monitorComments(); acct.?
Display username.
}
Post a 30 sec. video.
Note the use of curly
braces to block several Monitor comments.
statements together.
1 import javax.swing.JOptionPane;
2
3 public class IfNumbers {
4 public static void main(String[] args) {
5 String input;
6 int number;
7
8 input = JOptionPane.showInputDialog(“Enter a number:”);
9 number = Integer.parseInt(input);
10
11 if(number > 10)
12 JOptionPane.showMessageDialog(null, “The number is greater than 10.”);
13 if(number < 10)
14 JOptionPane.showMessageDialog(null, “The number is less than 10.”);
15 if(number == 10)
16 JOptionPane.showMessageDialog(null, “The number is 10”);
17
18 System.exit(0);
19 } Prepared by: A. C. Valderama – CSE- IT
20 } Dept.
Table of contents
if Statements and Boolean Expressions
1 import javax.swing.JOptionPane;
2
3 public class IfNumbers {
4 public static void main(String[] args) {
5 String input;
6 int number;
7
8 input = JOptionPane.showInputDialog(“Enter a number:”);
9 number = Integer.parseInt(input);
10
11 if(number > 10)
12 JOptionPane.showMessageDialog(null, “The number is greater than
10.”);
13 if(number < 10)
14 JOptionPane.showMessageDialog(null, “The number is less than 10.”);
15 if(number == 10)
16 JOptionPane.showMessageDialog(null, “The number is 10”);
17
18 System.exit(0); Prepared by: A. C. Valderama – CSE- IT
19 } Dept.
Table of contents
if Statements and Boolean Expressions
1 import javax.swing.JOptionPane;
2
3 public class IfNumbers {
4 public static void main(String[] args) {
5 String input;
6 int number;
7
8 input = JOptionPane.showInputDialog(“Enter a number:”);
9 number = Integer.parseInt(input);
10
11 if(number > 10)
12 JOptionPane.showMessageDialog(null, “The number is greater than 10.”);
13 if(number < 10)
14 JOptionPane.showMessageDialog(null, “The number is less than 10.”);
15 if(number == 10)
16 JOptionPane.showMessageDialog(null, “The number is 10”);
17
18 System.exit(0);
19 } Prepared by: A. C. Valderama – CSE- IT
20 } Dept.
Table of contents
Programming Style and
if Statements
is functionally equivalent to
if (expression)
{
statement1;
statement2;
} Curly brace ends the statement.
if (expression)
statement1; Only this statement is conditionally executed.
statement2;
statement3;
char c = ′A′;
if(c < ′Z′)
System.out.println("A is less than Z");
if (expression)
statementOrBlockIfTrue;
else
statementOrBlockIfFalse;
No Yes
Do you have a
Tiktok acct.?
Create one. Display username.
1 import javax.swing.JOptionPane;
2 public class Division {
3 public static void main(String[] args) {
4 double number1, number 2, quotient;
5 String input;
6 input = JOptionPane.showInputDialog(“Enter a number: “);
7 number1 = Double.parseDouble(input);
8
9 input = JOptionPane.showInputDialog(“Enter another number: “);
10 number 2 = Double.parseDouble(input);
11
12 if(number2 == 0) {
13 JOptionPane.showMessageDialog(null, “Division by 0 “ + “ is not possible!”);
14 }
15 else {
16 quotient = number1 / number2;
17 JOptionPane.showMessageDialog(null, “The quotient of “ + number1 + “ divided by “ + number2
+ “ is: “ + quotient);
18 }
19 } Prepared by: A. C. Valderama – CSE- IT
20 } Dept.
Table of contents
if-else Statement
1 import javax.swing.JOptionPane;
2 public class Division {
3 public static void main(String[] args) {
4 double number1, number 2, quotient;
5 String input;
6 input = JOptionPane.showInputDialog(“Enter a number: “);
7 number1 = Double.parseDouble(input);
8
9 input = JOptionPane.showInputDialog(“Enter another number: “);
10 number 2 = Double.parseDouble(input);
11
12 if(number2 == 0) {
13 JOptionPane.showMessageDialog(null, “Division by 0 “ + “ is not possible!”);
14 }
15 else {
16 quotient = number1 / number2;
17 JOptionPane.showMessageDialog(null, “The quotient of “ + number1 + “ divided by “ + number2
+ “ is: “ + quotient);
18 }
19 } Prepared by: A. C. Valderama – CSE- IT
20 } Dept.
Table of contents
Nested if Statements
No Yes
Do you have a
Tiktok acct.?
Create one.
No Yes
Do you have
followers?
import javax.swing.JOptionPane;
public class NestedIf {
public static void main(String[] args) {
String input;
double number;
import javax.swing.JOptionPane;
public class NestedIf {
public static void main(String[] args) {
String input;
double number;
import javax.swing.JOptionPane;
public class NestedIf {
public static void main(String[] args) {
String input;
double number;
else
{
statement;
statement; These statements are executed if none of
etc. the expressions above are true.
}
Prepared by: A. C. Valderama – CSE- IT
Dept.
Table of contents
if-else-if Statements
else
{
if(number >= 50 && number >= 80)
{
JOptionPane.showMessageDialog(null, "You passed with distinction!");
}
else
{
JOptionPane.showMessageDialog(null, "You passed!");
}
}
System.exit(0);
} Prepared by: A. C. Valderama – CSE- IT
} Dept.
Table of contents
The || Operator
• The logical OR operator (||) takes two operands that
must both be boolean expressions.
• The resulting combined expression is false if (and only
if) both operands are false.
else
{
System.out.println(“You do not qualify for the loan”);
}
Expression 1 !Expression1
true false
false true
if (name1.compareTo(name2) == 0)
String name1 = M A R K
String name2 = M A R Y
The same The same The same K less
than Y
Thus, name1 is less than name2
GREATER
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
LESS
switch (SwitchExpression)
{
…
}
case CaseExpression:
// place one or more statements here
break;
System.out.print(“Enter 1, 2 or 3: “);
number = keyboard.nextInt();
switch (number)
{
case 1:
System.out.println(“You entered 1.”);
break;
case 2:
System.out.println(“You entered 2.”);
break;
case 3:
System.out.println(“You entered 3.”);
break;
default:
System.out.println(“That’s not 1, 2 or 3!”);
}
} Prepared by: A. C. Valderama – CSE- IT
Dept.
Table of contents
The case Statement
public static void main(String[] args)
{
int number;
Program output:
Scanner keyboard = new Scanner(System.in); Enter 1, 2 or 3: 1 [Enter]
You entered 1.
System.out.print(“Enter 1, 2 or 3: “); You entered 2.
number = keyboard.nextInt(); You entered 3.
That’s not 1, 2 or 3!
switch (number)
{
case 1:
System.out.println(“You entered 1.”);
case 2:
System.out.println(“You entered 2.”);
case 3:
System.out.println(“You entered 3.”);
default:
System.out.println(“That’s not 1, 2 or 3!”);
}
}