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
DanWooster1
 
PPTX
Java Chapter 05 - Conditions & Loops: part 1
DanWooster1
 
PPTX
Java Chapter 05 - Conditions & Loops: part 5
DanWooster1
 
PPTX
Java Chapter 05 - Conditions & Loops: part 3
DanWooster1
 
PPTX
Flow control in Python
Md. Shafiuzzaman Hira
 
PPTX
Introduction to Python Programming
Md. Shafiuzzaman Hira
 
PPTX
Managing state in modern React web applications
Jon Preece
 
PPTX
Redux
Maulik Shah
 
Java Chapter 05 - Conditions & Loops: part 6
DanWooster1
 
Java Chapter 05 - Conditions & Loops: part 1
DanWooster1
 
Java Chapter 05 - Conditions & Loops: part 5
DanWooster1
 
Java Chapter 05 - Conditions & Loops: part 3
DanWooster1
 
Flow control in Python
Md. Shafiuzzaman Hira
 
Introduction to Python Programming
Md. Shafiuzzaman Hira
 
Managing state in modern React web applications
Jon Preece
 

What's hot (20)

PDF
Rethinking the debugger
Iulian Dragos
 
PPTX
Akka framework
mitesh_sharma
 
PDF
Java se 8 language enhancements & features
Juarez Junior
 
KEY
Introduction to Actor Model and Akka
Yung-Lin Ho
 
ODP
Life after Calc core change
Kohei Yoshida
 
PPTX
Introduction to actor model with examples on Akka.NET
Arthur Shvetsov
 
PDF
Pharo: A Reflective System
Marcus Denker
 
PDF
Introduction to the Actor Model
BoldRadius Solutions
 
PPTX
The Actor Model - Towards Better Concurrency
Dror Bereznitsky
 
PPTX
React Hooks
Joao Marins
 
PDF
Introduction to Akka
Knoldus Inc.
 
PPT
Do,Do while loop .net Visual Basic
manish maurya
 
PPTX
Real time operating systems (rtos) concepts 7
Abu Bakr Ramadan
 
PPTX
Vb.net (loop structure)
Abhishek Pachisia
 
PPTX
.NET Standard - NuGet Analysis
Immo Landwerth
 
PDF
RoelTyper
ESUG
 
PDF
Test Driven Development
Maris Prabhakaran M
 
PDF
Eclipse and Java 8 - Eclipse Day India 2013
Noopur Gupta
 
PDF
Introduction to Functional Reactive Programming
Đặng Thái Sơn
 
PDF
Ansible Galaxy @ Berlin Meetup 0415
Dan Vaida
 
Rethinking the debugger
Iulian Dragos
 
Akka framework
mitesh_sharma
 
Java se 8 language enhancements & features
Juarez Junior
 
Introduction to Actor Model and Akka
Yung-Lin Ho
 
Life after Calc core change
Kohei Yoshida
 
Introduction to actor model with examples on Akka.NET
Arthur Shvetsov
 
Pharo: A Reflective System
Marcus Denker
 
Introduction to the Actor Model
BoldRadius Solutions
 
The Actor Model - Towards Better Concurrency
Dror Bereznitsky
 
React Hooks
Joao Marins
 
Introduction to Akka
Knoldus Inc.
 
Do,Do while loop .net Visual Basic
manish maurya
 
Real time operating systems (rtos) concepts 7
Abu Bakr Ramadan
 
Vb.net (loop structure)
Abhishek Pachisia
 
.NET Standard - NuGet Analysis
Immo Landwerth
 
RoelTyper
ESUG
 
Test Driven Development
Maris Prabhakaran M
 
Eclipse and Java 8 - Eclipse Day India 2013
Noopur Gupta
 
Introduction to Functional Reactive Programming
Đặng Thái Sơn
 
Ansible Galaxy @ Berlin Meetup 0415
Dan Vaida
 
Ad

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

PPT
Ap Power Point Chpt3
dplunkett
 
PPT
slides03.ppt
Anjali127411
 
PPTX
Lewis_Cocking_AP_Decision_Making_For_Coding
GeorgeTsak
 
PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
PPT
Ap Power Point Chpt3 B
dplunkett
 
PPTX
ICSE Class X Conditional Statements in java
VanitaKarthik2
 
PPTX
Understand Decision structures in c++ (cplusplus)
Muhammad Tahir Bashir
 
PDF
Week04
hccit
 
PPT
Web development basics (Part-4)
Rajat Pratap Singh
 
PDF
Week03
hccit
 
PPTX
C language (Part 2)
Dr. SURBHI SAROHA
 
PPTX
Java chapter 3
Abdii Rashid
 
PPTX
Adobe Flash Actionscript language basics chapter-2
Nafis Ahmed
 
PPTX
Java Control Statement Control Statement.pptx
changepass
 
PPTX
06-Control-Statementskkkkkkkkkkkkkk.pptx
kamalsmail1
 
PPT
java_lect_04 Decision Structures in Java.ppt
javidmiakhil63
 
PPT
classes js adsfdsfdfdfdd dfdfadfdfda.ppt
sandhyadevit
 
PDF
C++ problem solving operators ( conditional operators,logical operators, swit...
mshakeel44514451
 
PPT
Variables Arguments and control flow_UiPath.ppt
Rohit Radhakrishnan
 
PDF
Java input Scanner
Huda Alameen
 
Ap Power Point Chpt3
dplunkett
 
slides03.ppt
Anjali127411
 
Lewis_Cocking_AP_Decision_Making_For_Coding
GeorgeTsak
 
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
DrNeetuSharma5
 
Ap Power Point Chpt3 B
dplunkett
 
ICSE Class X Conditional Statements in java
VanitaKarthik2
 
Understand Decision structures in c++ (cplusplus)
Muhammad Tahir Bashir
 
Week04
hccit
 
Web development basics (Part-4)
Rajat Pratap Singh
 
Week03
hccit
 
C language (Part 2)
Dr. SURBHI SAROHA
 
Java chapter 3
Abdii Rashid
 
Adobe Flash Actionscript language basics chapter-2
Nafis Ahmed
 
Java Control Statement Control Statement.pptx
changepass
 
06-Control-Statementskkkkkkkkkkkkkk.pptx
kamalsmail1
 
java_lect_04 Decision Structures in Java.ppt
javidmiakhil63
 
classes js adsfdsfdfdfdd dfdfadfdfda.ppt
sandhyadevit
 
C++ problem solving operators ( conditional operators,logical operators, swit...
mshakeel44514451
 
Variables Arguments and control flow_UiPath.ppt
Rohit Radhakrishnan
 
Java input Scanner
Huda Alameen
 
Ad

More from DanWooster1 (20)

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

Recently uploaded (20)

PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
TumwineRobert
 
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
MariellaTBesana
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PDF
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
Introduction and Scope of Bichemistry.pptx
shantiyogi
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Landforms and landscapes data surprise preview
jpinnuck
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Cardiovascular Pharmacology for pharmacy students.pptx
TumwineRobert
 
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
RAKESH SAJJAN
 
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
MariellaTBesana
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 

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