SlideShare a Scribd company logo
Chapter 5
Conditionals and
Loops: Part 2
• Java Software Solutions
• Foundations of Program
Design
• 9th Edition
John Lewis
William Loftus
Key
Concept
A loop allows a program to
execute a statement
multiple times.
Outline
Boolean Expressions
The if Statement
Comparing Data
The while Statement
Iterators
The ArrayList Class
Key
Concept
Conditionals and loops
allow us to control the flow
of execution through a
method.
Flow of
Control
• Unless specified otherwise, the
order of statement execution
through a method is linear: one
after another
• Some programming statements
allow us to make decisions and
perform repetitions
• These decisions are based on
boolean expressions (also called
conditions) that evaluate to true or
false
• The order of statement execution
is called the flow of control
Key
Concept
An if statement allows a
program to choose
whether to execute a
particular statement.
Conditional
Statements
• A conditional statement lets us choose which statement will
be executed next
• They are sometimes called selection statements
• Conditional statements give us the power to make basic
decisions
• The Java conditional statements are the:
– if and if-else statement
– switch statement
• We'll explore the switch statement in Chapter 6
Boolean
Expressions
• A condition often uses one of Java's equality
operators or relational operators, which all
return boolean results:
== equal to
!= not equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal
to
• Note the difference between the equality
operator (==) and the assignment operator (=)
Boolean
Expressions
• An if statement with its boolean
condition:
if (sum > MAX)
delta = sum –
MAX;
• First, the condition is evaluated:
the value of sum is either greater
than the value of MAX, or it is not
• If the condition is true, the
assignment statement is
executed; if it isn't, it is skipped
• See Age.java
//********************************************************************
// Age.java Author: Lewis/Loftus
//
// Demonstrates the use of an if statement.
//********************************************************************
import java.util.Scanner;
public class Age
{
//-----------------------------------------------------------------
// Reads the user's age and prints comments accordingly.
//-----------------------------------------------------------------
public static void main(String[] args)
{
final int MINOR = 21;
Scanner scan = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scan.nextInt();
continue
continue
System.out.println("You entered: " + age);
if (age < MINOR)
System.out.println("Youth is a wonderful thing. Enjoy.");
System.out.println("Age is a state of mind.");
}
}
continue
System.out.println("You entered: " + age);
if (age < MINOR)
System.out.println("Youth is a wonderful thing. Enjoy.");
System.out.println("Age is a state of mind.");
}
}
Sample Run
Enter your age: 47
You entered: 47
Age is a state of mind.
Another Sample Run
Enter your age: 12
You entered: 12
Youth is a wonderful thing. Enjoy.
Age is a state of mind.
Logical
Operators
• Boolean expressions can also
use the following logical
operators:
! Logical
NOT
&& Logical
AND
|| Logical
OR
• They all take boolean operands
and produce boolean results
• Logical NOT is a unary operator
(it operates on one operand)
• Logical AND and logical OR are
binary operators (each operates
on two operands)
Logical NOT
• The logical NOT operation is also called logical
negation or logical complement
• If some boolean condition a is true, then !a is false;
if a is false, then !a is true
• Logical expressions can be shown using a truth
table:
a !a
true false
false true
Logical
AND and
Logical
OR
• The logical AND expression
a && b
is true if both a and b are true,
and false otherwise
• The logical OR expression
a || b
is true if a or b or both are
true, and false otherwise
Key
Concept
Logical operators are often
used to construct
sophisticated conditions.
Logical AND and Logical OR
• A truth table shows all possible true-false
combinations of the terms
• Since && and || each have two operands, there
are four possible combinations of a and b
a b a && b a || b
true true true true
true false false true
false true false true
false false false false
Logical
Operators
• Expressions that use logical
operators can form complex
conditions
if (total < MAX+5 &&
!found)
System.out.println("Proc
essing…");
• All logical operators have lower
precedence than the relational
operators
• The ! operator has higher
precedence than && and ||
Boolean Expressions
• Specific expressions can be evaluated using truth
tables
total < MAX found !found total < MAX && !found
false false true false
false true false false
true false true true
true true false false
Short-
Circuited
Operators
• The processing of && and ||
is “short-circuited”
• If the left operand is sufficient
to determine the result, the
right operand is not evaluated
if (count != 0 &&
total/count > MAX)
System.out.println("Te
sting.");
• This type of processing should
be used carefully

More Related Content

PPTX
Java Chapter 05 - Conditions & Loops: part 6
PPTX
Java Chapter 05 - Conditions & Loops: part 1
PPTX
Java Chapter 05 - Conditions & Loops: part 5
PPTX
Java Chapter 05 - Conditions & Loops: part 3
PPTX
Flow control in Python
PPTX
Introduction to Python Programming
PPTX
Managing state in modern React web applications
PPTX
Java Chapter 05 - Conditions & Loops: part 6
Java Chapter 05 - Conditions & Loops: part 1
Java Chapter 05 - Conditions & Loops: part 5
Java Chapter 05 - Conditions & Loops: part 3
Flow control in Python
Introduction to Python Programming
Managing state in modern React web applications

What's hot (20)

PDF
Rethinking the debugger
PPTX
Akka framework
PDF
Java se 8 language enhancements & features
KEY
Introduction to Actor Model and Akka
ODP
Life after Calc core change
PPTX
Introduction to actor model with examples on Akka.NET
PDF
Pharo: A Reflective System
PDF
Introduction to the Actor Model
PPTX
The Actor Model - Towards Better Concurrency
PPTX
React Hooks
PDF
Introduction to Akka
PPT
Do,Do while loop .net Visual Basic
PPTX
Real time operating systems (rtos) concepts 7
PPTX
Vb.net (loop structure)
PPTX
.NET Standard - NuGet Analysis
PDF
RoelTyper
PDF
Test Driven Development
PDF
Eclipse and Java 8 - Eclipse Day India 2013
PDF
Introduction to Functional Reactive Programming
PDF
Ansible Galaxy @ Berlin Meetup 0415
Rethinking the debugger
Akka framework
Java se 8 language enhancements & features
Introduction to Actor Model and Akka
Life after Calc core change
Introduction to actor model with examples on Akka.NET
Pharo: A Reflective System
Introduction to the Actor Model
The Actor Model - Towards Better Concurrency
React Hooks
Introduction to Akka
Do,Do while loop .net Visual Basic
Real time operating systems (rtos) concepts 7
Vb.net (loop structure)
.NET Standard - NuGet Analysis
RoelTyper
Test Driven Development
Eclipse and Java 8 - Eclipse Day India 2013
Introduction to Functional Reactive Programming
Ansible Galaxy @ Berlin Meetup 0415
Ad

Similar to Java Chapter 05 - Conditions & Loops: part 2 (20)

PPT
Ap Power Point Chpt3
PPT
slides03.ppt
PPTX
Lewis_Cocking_AP_Decision_Making_For_Coding
PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
PPT
Ap Power Point Chpt3 B
PPTX
ICSE Class X Conditional Statements in java
PPTX
Understand Decision structures in c++ (cplusplus)
PDF
Week04
PPT
Web development basics (Part-4)
PDF
Week03
PPTX
C language (Part 2)
PPTX
Java chapter 3
PPTX
Adobe Flash Actionscript language basics chapter-2
PPTX
Java Control Statement Control Statement.pptx
PPTX
06-Control-Statementskkkkkkkkkkkkkk.pptx
PPT
java_lect_04 Decision Structures in Java.ppt
PPT
classes js adsfdsfdfdfdd dfdfadfdfda.ppt
PDF
C++ problem solving operators ( conditional operators,logical operators, swit...
PPT
Variables Arguments and control flow_UiPath.ppt
PDF
Java input Scanner
Ap Power Point Chpt3
slides03.ppt
Lewis_Cocking_AP_Decision_Making_For_Coding
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
Ap Power Point Chpt3 B
ICSE Class X Conditional Statements in java
Understand Decision structures in c++ (cplusplus)
Week04
Web development basics (Part-4)
Week03
C language (Part 2)
Java chapter 3
Adobe Flash Actionscript language basics chapter-2
Java Control Statement Control Statement.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx
java_lect_04 Decision Structures in Java.ppt
classes js adsfdsfdfdfdd dfdfadfdfda.ppt
C++ problem solving operators ( conditional operators,logical operators, swit...
Variables Arguments and control flow_UiPath.ppt
Java input Scanner
Ad

More from DanWooster1 (20)

PPTX
Upstate CSCI 540 Agile Development
PPTX
Upstate CSCI 540 Unit testing
PPTX
Upstate CSCI 450 WebDev Chapter 9
PPTX
Upstate CSCI 450 WebDev Chapter 4
PPTX
Upstate CSCI 450 WebDev Chapter 4
PPTX
Upstate CSCI 450 WebDev Chapter 3
PPTX
Upstate CSCI 450 WebDev Chapter 2
PPTX
Upstate CSCI 450 WebDev Chapter 1
PPT
Upstate CSCI 525 Data Mining Chapter 3
PPT
Upstate CSCI 525 Data Mining Chapter 2
PPT
Upstate CSCI 525 Data Mining Chapter 1
PPTX
Upstate CSCI 200 Java Chapter 8 - Arrays
PPTX
Upstate CSCI 200 Java Chapter 7 - OOP
PPTX
CSCI 200 Java Chapter 03 Using Classes
PPTX
CSCI 200 Java Chapter 02 Data & Expressions
PPTX
CSCI 200 Java Chapter 01
PPTX
CSCI 238 Chapter 08 Arrays Textbook Slides
PPTX
Chapter 6 - More conditionals and loops
PPTX
Upstate CSCI 450 jQuery
PPTX
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 540 Agile Development
Upstate CSCI 540 Unit testing
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 200 Java Chapter 8 - Arrays
Upstate CSCI 200 Java Chapter 7 - OOP
CSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 01
CSCI 238 Chapter 08 Arrays Textbook Slides
Chapter 6 - More conditionals and loops
Upstate CSCI 450 jQuery
Upstate CSCI 450 PHP Chapters 5, 12, 13

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
01-Introduction-to-Information-Management.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Trump Administration's workforce development strategy
PPTX
Lesson notes of climatology university.
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
master seminar digital applications in india
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
A systematic review of self-coping strategies used by university students to ...
human mycosis Human fungal infections are called human mycosis..pptx
Yogi Goddess Pres Conference Studio Updates
01-Introduction-to-Information-Management.pdf
Complications of Minimal Access Surgery at WLH
O7-L3 Supply Chain Operations - ICLT Program
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Microbial disease of the cardiovascular and lymphatic systems
Trump Administration's workforce development strategy
Lesson notes of climatology university.
Orientation - ARALprogram of Deped to the Parents.pptx
master seminar digital applications in india
Pharma ospi slides which help in ospi learning
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Chinmaya Tiranga quiz Grand Finale.pdf
Microbial diseases, their pathogenesis and prophylaxis
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf

Java Chapter 05 - Conditions & Loops: part 2

  • 1. Chapter 5 Conditionals and Loops: Part 2 • Java Software Solutions • Foundations of Program Design • 9th Edition John Lewis William Loftus
  • 2. Key Concept A loop allows a program to execute a statement multiple times.
  • 3. Outline Boolean Expressions The if Statement Comparing Data The while Statement Iterators The ArrayList Class
  • 4. Key Concept Conditionals and loops allow us to control the flow of execution through a method.
  • 5. Flow of Control • Unless specified otherwise, the order of statement execution through a method is linear: one after another • Some programming statements allow us to make decisions and perform repetitions • These decisions are based on boolean expressions (also called conditions) that evaluate to true or false • The order of statement execution is called the flow of control
  • 6. Key Concept An if statement allows a program to choose whether to execute a particular statement.
  • 7. Conditional Statements • A conditional statement lets us choose which statement will be executed next • They are sometimes called selection statements • Conditional statements give us the power to make basic decisions • The Java conditional statements are the: – if and if-else statement – switch statement • We'll explore the switch statement in Chapter 6
  • 8. Boolean Expressions • A condition often uses one of Java's equality operators or relational operators, which all return boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to • Note the difference between the equality operator (==) and the assignment operator (=)
  • 9. Boolean Expressions • An if statement with its boolean condition: if (sum > MAX) delta = sum – MAX; • First, the condition is evaluated: the value of sum is either greater than the value of MAX, or it is not • If the condition is true, the assignment statement is executed; if it isn't, it is skipped • See Age.java
  • 10. //******************************************************************** // Age.java Author: Lewis/Loftus // // Demonstrates the use of an if statement. //******************************************************************** import java.util.Scanner; public class Age { //----------------------------------------------------------------- // Reads the user's age and prints comments accordingly. //----------------------------------------------------------------- public static void main(String[] args) { final int MINOR = 21; Scanner scan = new Scanner(System.in); System.out.print("Enter your age: "); int age = scan.nextInt(); continue
  • 11. continue System.out.println("You entered: " + age); if (age < MINOR) System.out.println("Youth is a wonderful thing. Enjoy."); System.out.println("Age is a state of mind."); } }
  • 12. continue System.out.println("You entered: " + age); if (age < MINOR) System.out.println("Youth is a wonderful thing. Enjoy."); System.out.println("Age is a state of mind."); } } Sample Run Enter your age: 47 You entered: 47 Age is a state of mind. Another Sample Run Enter your age: 12 You entered: 12 Youth is a wonderful thing. Enjoy. Age is a state of mind.
  • 13. Logical Operators • Boolean expressions can also use the following logical operators: ! Logical NOT && Logical AND || Logical OR • They all take boolean operands and produce boolean results • Logical NOT is a unary operator (it operates on one operand) • Logical AND and logical OR are binary operators (each operates on two operands)
  • 14. Logical NOT • The logical NOT operation is also called logical negation or logical complement • If some boolean condition a is true, then !a is false; if a is false, then !a is true • Logical expressions can be shown using a truth table: a !a true false false true
  • 15. Logical AND and Logical OR • The logical AND expression a && b is true if both a and b are true, and false otherwise • The logical OR expression a || b is true if a or b or both are true, and false otherwise
  • 16. Key Concept Logical operators are often used to construct sophisticated conditions.
  • 17. Logical AND and Logical OR • A truth table shows all possible true-false combinations of the terms • Since && and || each have two operands, there are four possible combinations of a and b a b a && b a || b true true true true true false false true false true false true false false false false
  • 18. Logical Operators • Expressions that use logical operators can form complex conditions if (total < MAX+5 && !found) System.out.println("Proc essing…"); • All logical operators have lower precedence than the relational operators • The ! operator has higher precedence than && and ||
  • 19. Boolean Expressions • Specific expressions can be evaluated using truth tables total < MAX found !found total < MAX && !found false false true false false true false false true false true true true true false false
  • 20. Short- Circuited Operators • The processing of && and || is “short-circuited” • If the left operand is sufficient to determine the result, the right operand is not evaluated if (count != 0 && total/count > MAX) System.out.println("Te sting."); • This type of processing should be used carefully