0% found this document useful (0 votes)
3 views

Decision Structures1

Uploaded by

dokumanuels
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Decision Structures1

Uploaded by

dokumanuels
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

CSCI 1301

COMPUTER SCIENCE I
DECISION STRUCTURES
DECISION STRUCTURES

Yes/No or
Are you No True/False
at least Question
21?

Yes

You deserve a Conditionally


drink! Executed
Statement
DECISION STRUCTURES

Boolean false
Expression

true

Statement

 Boolean expression is an expression that is either true or false.


IF STATEMENTS
if(BooleanExpression)
statement;

if(age is greater than or equal to 21)


drinkingAge is true
if(score is greater than or equal to 90)
grade is A
if(the remainder of the number divided by 2 is 0)
even is true
RELATIONAL OPERATORS
Relational Operators
Meaning
(in Order of Precedence)
> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

== Equal to

!= Not equal to
QUIZ TIME!
 Determine whether the following expressions evaluate to true or
false:
4 + 2 != 6
8 < 15
2.5 > 5.8
2 * 16 < 97
JAVA IF STATEMENTS
if(age >= 21)
drinkingAge = true;
True/False
Question

if(quizScore == 10)
perfectScore = true;

Conditionally Executed
Statement
if(score > = 90)
grade = 'A';

 If the BooleanExpression evaluates to true, the statement that follows


is executed, otherwise the statement following the if-statement is
executed
WATCH OUT!
if(age >= 21);
No semicolon here!
drinkingAge = true;

Statement executes
regardless

if(quizScore = 10)
perfectScore = true; This is an assignment operator!

if(quizScore == 10)
perfectScore = true; THIS is the equality operator!
HOW DOES IT WORK?

import javax.swing.JOptionPane;
If the user enters 27…

public class Demos {


public static void main(String[] args) {

String ageString = JOptionPane.showInputDialog("How old are you?");


This expression int age = Integer.parseInt(ageString);

will evaluate to if(age >= 21)


true… JOptionPane.showMessageDialog(null, "Let's go to the bar!");

JOptionPane.showMessageDialog(null, "Goodbye!");
}
}
This line will be executed.

Followed by this line.


HOW DOES IT WORK?

import javax.swing.JOptionPane;
If the user enters 19…

public class Demos {


public static void main(String[] args) {

String ageString = JOptionPane.showInputDialog("How old are you?");


This expression int age = Integer.parseInt(ageString);

will evaluate to if(age >= 21)


false… JOptionPane.showMessageDialog(null, "Let's go to the bar!");

JOptionPane.showMessageDialog(null, "Goodbye!");
}
}
This line will be SKIPPED.

Only this line will be executed.


GIVE IT A GO!
 Write an if statement that will display a notice to the user if their
balance goes below a minimum balance.

 Write an if statement that will determine if a number is even or odd.

 Note: Assume any variables you wish to use have already been
declared.
MULTIPLE CONDITIONALLY EXECUTED STATEMENTS

 If more than one statement is to be executed conditionally, the


statements must be placed within a set of curly braces.

if(age >= 21)


{
drinkingAge = true; Block of
System.out.println("You deserve a drink!"); statements
}

if(score > = 90)


{
grade = 'A';
System.out.println("Congrats on your high
achievement!");
}
WATCH OUT!
 Each statement that is to be conditionally executed needs to go
inside the curly braces.
FLAGS
 A flag is a boolean variable that signals when some condition exists in
the program.
 When the flag variable is set to false, it indicates the condition does not
yet exist.
 When the flag variable is set to true, it means the condition does exist.

if(drinkingAge)
System.out.println("You deserve a drink!")

 Remember that a boolean expression is one whose value is either true


or false.
 And what are the only valid values of a boolean variable?
COMPARING CHARACTERS
 Characters are stored in memory as Unicode

if(ch == 'A')
System.out.println("The letter is A.");

if(' ' < 'a')


System.out.println("Space is less than A");
LOGICAL OPERATORS
Operator Meaning Description

! NOT Reverses the truth of a boolean


expression.
&& AND Both expressions must be true for
the overall expression to be true.
|| OR Only one expression needs to be
true for the overall expression to
be true.
TRUTH TABLES
Value of A Value of B Value of A && B Value of A || B Value of !(A)

true true true true false

true false false true false

false true false true true

false false false false true

 Short circuit evaluation


 The expressions on each side are evaluated only as far as
necessary to determine whether the entire expression is true or
false.
QUIZ TIME!
 Assume the following variables have been initialized:
int x = 5;
int y = 10;
int z = 15;
 Determine whether the following expressions evaluate to true or
false:
(x < 5 && y > x)
(x < 5 || y > x)
(x > 3 || y < 10 && z == 15)
QUIZ TIME!
(8 * 2 == 4 * 4) && !(3 + 3 == 6)
 Assuming the variable someNumber exists and already has a value:
(someNumber < 20 && someNumber > 40)
IF-ELSE
 Expansion of the if statement
 If the expression evaluates to true, a statement or block of statements
is executed.
 If the expression is false, a separate statement or block of statements
is executed.

if(BooleanExpression)
statement or block;
else
statement or block;
HOW DOES IT WORK?

import javax.swing.JOptionPane;
If the user enters 27…

public class Demos {


public static void main(String[] args) {

String ageString = JOptionPane.showInputDialog("How old are you?");


This expression int age = Integer.parseInt(ageString);

will evaluate to if(age >= 21)


true… JOptionPane.showMessageDialog(null, "Let's go to the bar!");
else
JOptionPane.showMessageDialog(null, “We should get some koolaid!");

JOptionPane.showMessageDialog(null, "Goodbye!");
}
}

These lines will be executed.

BOTH of these lines will be skipped.


HOW DOES IT WORK?

import javax.swing.JOptionPane;
If the user enters 19…

public class Demos {


public static void main(String[] args) {

String ageString = JOptionPane.showInputDialog("How old are you?");


This expression int age = Integer.parseInt(ageString);

will evaluate to if(age >= 21)


false… JOptionPane.showMessageDialog(null, "Let's go to the bar!");
else
JOptionPane.showMessageDialog(null, “We should get some koolaid!");

JOptionPane.showMessageDialog(null, "Goodbye!");
}
}
This line will be executed.
This line will be SKIPPED.
And so will this line…
GIVE IT A GO!
 Rewrite these statements to include an else statement:

 Write an if statement that will display a notice to the user if their


balance goes below a minimum balance.

 Write an if statement that will determine if a number is even or odd.

 Note: Assume any variables you wish to use have already been
declared.
NESTED IF STATEMENTS
if(BooleanExpression)  To test more than one condition, an if
{ statement can be nested inside
statement or block; another if statement.
}
else
{
if(BooleanExpression)
{
statement or block;
}
else
{
if(BooleanExpression)
{
statement or block;
}
}
}
IF-ELSE-IF
 The if-else-if statement tests a series of conditions.
 Simpler than nested if-statements

if(BooleanExpression)
{
statement or block;
}
else if(BooleanExpression)
{
statement or block;
}
else
{
statement or block;
}
COMPARING STRINGS
 Strings are reference variables
String name1 = "Bob";
String name2 = "Sam";

if(name1 == name2)
 The if statement compares the addresses the objects point to rather
than the values.
 To compare correctly:
 Use methods of the String class
MORE STRING METHODS
Method Name Description Example

equals Returns true if the two name.equals(someOtherName);


strings are equal, or
false if they are not
(Case Sensitive!)
equalsIgnoreCase Returns true if the two name.equalsIgnoreCase(someOtherName);
strings are equal, or
false if they are not (Not
Case Sensitive!)
PUTTING IT ALL TOGETHER
 Let’s update our GPA Calculator:
 Ask the user their academic standing, which we’ll store in a String
variable
 Use the following table to determine when and how to update the
academic standing:

Standing Triggered When…

Good GPA is not below 2.0

Warning GPA drops below 2.0

Probation Already at Warning and GPA drops below 2.0

Suspension Already at Probation and GPA drops below 2.0

 Display the user’s new academic standing.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy