SlideShare a Scribd company logo
1 of 20
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

What's hot

What's hot (20)

Rethinking the debugger
Rethinking the debuggerRethinking the debugger
Rethinking the debugger
 
Akka framework
Akka frameworkAkka framework
Akka framework
 
Java se 8 language enhancements & features
Java se 8   language enhancements & featuresJava se 8   language enhancements & features
Java se 8 language enhancements & features
 
Introduction to Actor Model and Akka
Introduction to Actor Model and AkkaIntroduction to Actor Model and Akka
Introduction to Actor Model and Akka
 
Life after Calc core change
Life after Calc core changeLife after Calc core change
Life after Calc core change
 
Introduction to actor model with examples on Akka.NET
Introduction to actor model with examples on Akka.NETIntroduction to actor model with examples on Akka.NET
Introduction to actor model with examples on Akka.NET
 
Pharo: A Reflective System
Pharo: A Reflective SystemPharo: A Reflective System
Pharo: A Reflective System
 
Introduction to the Actor Model
Introduction to the Actor ModelIntroduction to the Actor Model
Introduction to the Actor Model
 
The Actor Model - Towards Better Concurrency
The Actor Model - Towards Better ConcurrencyThe Actor Model - Towards Better Concurrency
The Actor Model - Towards Better Concurrency
 
React Hooks
React HooksReact Hooks
React Hooks
 
Introduction to Akka
Introduction to AkkaIntroduction to Akka
Introduction to Akka
 
Do,Do while loop .net Visual Basic
Do,Do while loop .net Visual BasicDo,Do while loop .net Visual Basic
Do,Do while loop .net Visual Basic
 
Real time operating systems (rtos) concepts 7
Real time operating systems (rtos) concepts 7Real time operating systems (rtos) concepts 7
Real time operating systems (rtos) concepts 7
 
Vb.net (loop structure)
Vb.net (loop structure)Vb.net (loop structure)
Vb.net (loop structure)
 
.NET Standard - NuGet Analysis
.NET Standard - NuGet Analysis.NET Standard - NuGet Analysis
.NET Standard - NuGet Analysis
 
RoelTyper
RoelTyperRoelTyper
RoelTyper
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013Eclipse and Java 8 - Eclipse Day India 2013
Eclipse and Java 8 - Eclipse Day India 2013
 
Introduction to Functional Reactive Programming
Introduction to Functional Reactive ProgrammingIntroduction to Functional Reactive Programming
Introduction to Functional Reactive Programming
 
Ansible Galaxy @ Berlin Meetup 0415
Ansible Galaxy @ Berlin Meetup 0415Ansible Galaxy @ Berlin Meetup 0415
Ansible Galaxy @ Berlin Meetup 0415
 

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

Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
dplunkett
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
dplunkett
 
Week04
Week04Week04
Week04
hccit
 
Week03
Week03Week03
Week03
hccit
 
LOOPSFor LoopFor loop executes group of Java statements as long.pdf
LOOPSFor LoopFor loop executes group of Java statements as long.pdfLOOPSFor LoopFor loop executes group of Java statements as long.pdf
LOOPSFor LoopFor loop executes group of Java statements as long.pdf
amaresh6333
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
DanWooster1
 

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

Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
 
Week04
Week04Week04
Week04
 
Web development basics (Part-4)
Web development basics (Part-4)Web development basics (Part-4)
Web development basics (Part-4)
 
Week03
Week03Week03
Week03
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
Adobe Flash Actionscript language basics chapter-2
Adobe Flash Actionscript language basics chapter-2Adobe Flash Actionscript language basics chapter-2
Adobe Flash Actionscript language basics chapter-2
 
C++ problem solving operators ( conditional operators,logical operators, swit...
C++ problem solving operators ( conditional operators,logical operators, swit...C++ problem solving operators ( conditional operators,logical operators, swit...
C++ problem solving operators ( conditional operators,logical operators, swit...
 
Variables Arguments and control flow_UiPath.ppt
Variables Arguments and control flow_UiPath.pptVariables Arguments and control flow_UiPath.ppt
Variables Arguments and control flow_UiPath.ppt
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
 
Ifi7107 lesson4
Ifi7107 lesson4Ifi7107 lesson4
Ifi7107 lesson4
 
data types.pdf
data types.pdfdata types.pdf
data types.pdf
 
LOOPSFor LoopFor loop executes group of Java statements as long.pdf
LOOPSFor LoopFor loop executes group of Java statements as long.pdfLOOPSFor LoopFor loop executes group of Java statements as long.pdf
LOOPSFor LoopFor loop executes group of Java statements as long.pdf
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Adsa u1 ver 1.0
Adsa u1 ver 1.0Adsa u1 ver 1.0
Adsa u1 ver 1.0
 
c++ Data Types and Selection
c++ Data Types and Selectionc++ Data Types and Selection
c++ Data Types and Selection
 

More from DanWooster1

Upstate CSCI 540 Agile Development
Upstate CSCI 540 Agile DevelopmentUpstate CSCI 540 Agile Development
Upstate CSCI 540 Agile Development
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3
DanWooster1
 
Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2
DanWooster1
 
CSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook SlidesCSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook Slides
DanWooster1
 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13
DanWooster1
 

More from DanWooster1 (20)

Upstate CSCI 540 Agile Development
Upstate CSCI 540 Agile DevelopmentUpstate CSCI 540 Agile Development
Upstate CSCI 540 Agile Development
 
Upstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testingUpstate CSCI 540 Unit testing
Upstate CSCI 540 Unit testing
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
 
Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4Upstate CSCI 450 WebDev Chapter 4
Upstate CSCI 450 WebDev Chapter 4
 
Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3Upstate CSCI 450 WebDev Chapter 3
Upstate CSCI 450 WebDev Chapter 3
 
Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2Upstate CSCI 450 WebDev Chapter 2
Upstate CSCI 450 WebDev Chapter 2
 
Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1Upstate CSCI 450 WebDev Chapter 1
Upstate CSCI 450 WebDev Chapter 1
 
Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3Upstate CSCI 525 Data Mining Chapter 3
Upstate CSCI 525 Data Mining Chapter 3
 
Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2Upstate CSCI 525 Data Mining Chapter 2
Upstate CSCI 525 Data Mining Chapter 2
 
Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1Upstate CSCI 525 Data Mining Chapter 1
Upstate CSCI 525 Data Mining Chapter 1
 
Upstate CSCI 200 Java Chapter 8 - Arrays
Upstate CSCI 200 Java Chapter 8 - ArraysUpstate CSCI 200 Java Chapter 8 - Arrays
Upstate CSCI 200 Java Chapter 8 - Arrays
 
Upstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOPUpstate CSCI 200 Java Chapter 7 - OOP
Upstate CSCI 200 Java Chapter 7 - OOP
 
CSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using ClassesCSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using Classes
 
CSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & ExpressionsCSCI 200 Java Chapter 02 Data & Expressions
CSCI 200 Java Chapter 02 Data & Expressions
 
CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01CSCI 200 Java Chapter 01
CSCI 200 Java Chapter 01
 
CSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook SlidesCSCI 238 Chapter 08 Arrays Textbook Slides
CSCI 238 Chapter 08 Arrays Textbook Slides
 
Upstate CSCI 450 jQuery
Upstate CSCI 450 jQueryUpstate CSCI 450 jQuery
Upstate CSCI 450 jQuery
 
Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13Upstate CSCI 450 PHP Chapters 5, 12, 13
Upstate CSCI 450 PHP Chapters 5, 12, 13
 
Upstate CSCI 450 PHP
Upstate CSCI 450 PHPUpstate CSCI 450 PHP
Upstate CSCI 450 PHP
 

Recently uploaded

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 

Recently uploaded (20)

9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).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