SlideShare a Scribd company logo
1 of 39
Download to read offline
Week 3
Conditional
Statements
© 2004 Pearson Addison-Wesley. All rights reserved 5-2
Copyright Warning
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been copied and communicated to you by or
on behalf of Bond University pursuant to Part VB of the
Copyright Act 1968 (the Act).
The material in this communication may be subject to copyright
under the Act. Any further copying or communication of this
material by you may be the subject of copyright protection
under the Act.
Do not remove this notice.
© 2004 Pearson Addison-Wesley. All rights reserved 5-3
Conditional Statements
• Now we will examine programming statements
that allow us to:
 make decisions
 perform commands based on those decisions
• Chapter 5 focuses on:
 boolean expressions
 conditional statements
 comparing data
© 2004 Pearson Addison-Wesley. All rights reserved 5-4
Outline
The if Statement and Conditions
Other Conditional Statements
Comparing Data
© 2004 Pearson Addison-Wesley. All rights reserved 5-5
Flow of Control
• Unless specified otherwise, the order of statement
execution through a method is linear: one
statement after another in sequence
• Some programming statements allow us to:
 decide whether or not to execute a particular statement
 execute a statement over and over, repetitively
• These decisions are based on boolean expressions
(or conditions) that evaluate to true or false
• The order of statement execution is called the flow
of control
© 2004 Pearson Addison-Wesley. All rights reserved 5-6
Conditional Statements
• A conditional statement lets us choose which
statement will be executed next
• Therefore they are sometimes called selection
statements
• Conditional statements give us the power to
make basic decisions
• The Java conditional statements are the:
 IF statement
 SWITCH statement
© 2004 Pearson Addison-Wesley. All rights reserved 5-7
The IF Statement
• Looks like the following
if ( condition )
{
statement1;
statement2;
}
else
{
statement3;
statement4;
}
• If the condition is true, statements 1 & 2 are
executed; if the condition is false, statements 3
& 4 are executed
• One section or the other will be executed, but not
both
© 2004 Pearson Addison-Wesley. All rights reserved 5-8
The IF Statement
• The else { … } section is optional
 If we don't need to do anything on a false decision, we
can leave it out
• Also, if there is only 1 line of code to do in each
section, we can leave out the curly brackets { }
• For example:
if ( age < 18 )
        System.out.println(“Too young to come in”);
© 2004 Pearson Addison-Wesley. All rights reserved 5-9
Logic of an IF statement
condition
evaluated
True block
true false
False block
© 2004 Pearson Addison-Wesley. All rights reserved 5-10
Block Statements
• Several statements can be grouped together into a
block statement delimited by braces
• A block statement can be used wherever a
statement is called for in the Java syntax rules
if (total > MAX)
{
System.out.println ("Error!!");
errorCount++;
}
© 2004 Pearson Addison-Wesley. All rights reserved 5-11
Block Statements
• In an IF statement, the if portion, or the else
portion, or both, could be block statements
if (total > MAX)
{
System.out.println ("Error!!");
errorCount++;
}
else
{
System.out.println ("Total: " + total);
current = total*2;
}
© 2004 Pearson Addison-Wesley. All rights reserved 5-12
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 (=)
© 2004 Pearson Addison-Wesley. All rights reserved 5-13
The if Statement
• An example of an if statement:
if (sum > MAX)
delta = sum - MAX;
System.out.println ("The sum is " + sum);
• 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.
• Either way, the call to println is executed next
© 2004 Pearson Addison-Wesley. All rights reserved 5-14
Indentation
• The statement controlled by the if statement is
indented to indicate that relationship
• The use of a consistent indentation style makes a
program easier to read and understand
• Although it makes no difference to the compiler,
proper indentation is crucial
"Always code as if the person who ends up
maintaining your code will be a violent
psychopath who knows where you live."
-- Martin Golding
© 2004 Pearson Addison-Wesley. All rights reserved 5-15
The if Statement
• What do the following statements do?
if (top >= MAXIMUM)
top = 0;
if (total != stock + warehouse)
inventoryError = true;
• The precedence of the arithmetic operators is
higher than the precedence of the equality and
relational operators
© 2004 Pearson Addison-Wesley. All rights reserved 5-16
Logical Operators: Combining
Comparisons Together
• 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)
© 2004 Pearson Addison-Wesley. All rights reserved 5-17
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
truefalse
falsetrue
!aa
© 2004 Pearson Addison-Wesley. All rights reserved 5-18
Logical AND Operator
• The logical AND expression
a && b
is true if both a and b are true, and false otherwise
• Example:
if ( (x > 10) && (x < 20) )
• The whole condition is true if x > 10 AND x < 20
AT THE SAME TIME!
© 2004 Pearson Addison-Wesley. All rights reserved 5-19
Logical OR Operator
• The logical OR expression
a || b
is true if a or b or both are true, and false
otherwise
• Example:
if ( (ch == 'a') || (ch == 'A') )
• The whole condition is true if ch is either the letter
'a' OR the letter 'A'.
© 2004 Pearson Addison-Wesley. All rights reserved 5-20
Logical Operators
• Expressions that use logical operators can form
complex conditions
if (total < MAX+5 && !found)
System.out.println ("Processing…");
• All logical operators have lower precedence than
the relational operators
• Logical NOT has higher precedence than logical
AND and logical OR
• When in doubt, use parentheses to enforce the
order of evaluation
© 2004 Pearson Addison-Wesley. All rights reserved 5-21
Short-Circuited Operators
• The processing of logical AND and logical OR is
“short-circuited”
• This is known as lazy evaluation
• If the left operand is sufficient to determine the
result, the right operand is not evaluated
• This type of processing must be used carefully
if (count != 0 && total/count > MAX)
System.out.println ("Testing…");
© 2004 Pearson Addison-Wesley. All rights reserved 5-22
Indentation Revisited
• Remember that indentation is for the human
reader, and is ignored by the computer
if (total > MAX)
System.out.println ("Error!!");
errorCount++;
Despite what is implied by the indentation, the
increment will occur whether the condition is
true or not
© 2004 Pearson Addison-Wesley. All rights reserved 5-23
Nested if Statements
• The statement executed as a result of an if
statement or else clause could be another if
statement
• These are called nested if statements
• See MinOfThree.java (page 219)
• An else clause is matched to the last unmatched
if (no matter what the indentation implies)
• Braces can be used to specify the if statement to
which an else clause belongs
© 2004 Pearson Addison-Wesley. All rights reserved 5-24
The SWITCH Statement
• The switch statement provides another way to
decide which statement to execute next
• The switch statement evaluates an expression,
then attempts to match the result to one of several
possible cases
• Each case contains a value and a list of
statements
• The flow of control transfers to statement
associated with the first case value that matches
© 2004 Pearson Addison-Wesley. All rights reserved 5-25
The SWITCH Statement
• The general syntax of a switch statement is:
switch ( expression )
{
case value1 :
statement-list1
case value2 :
statement-list2
case value3 :
statement-list3
case ...
}
switch
and
case
are
reserved
words
If expression
matches value2,
control jumps
to here
© 2004 Pearson Addison-Wesley. All rights reserved 5-26
The switch Statement
• Often a break statement is used as the last
statement in each case's statement list
• A break statement causes control to transfer to
the end of the switch statement
• If a break statement is not used, the flow of
control will continue into the next case
• Sometimes this may be appropriate, but often we
want to execute only the statements associated
with one case
© 2004 Pearson Addison-Wesley. All rights reserved 5-27
The switch Statement
switch (option)
{
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
}
• An example of a switch statement:
© 2004 Pearson Addison-Wesley. All rights reserved 5-28
The switch Statement
• A switch statement can have an optional default
case
• The default case has no associated value and
simply uses the reserved word default
• If the default case is present, control will transfer
to it if no other case value matches
• If there is no default case, and no other value
matches, control falls through to the statement
after the switch
© 2004 Pearson Addison-Wesley. All rights reserved 5-29
The switch Statement
• The expression of a switch statement must result
in an integral type, meaning an integer (byte,
short, int, long) or a char
• It cannot be a boolean value or a floating point
value (float or double)
• The implicit boolean condition in a switch
statement is equality
• You cannot perform relational checks with a
switch statement
• See GradeReport.java (page 225)
© 2004 Pearson Addison-Wesley. All rights reserved 5-30
Outline
The if Statement and Conditions
Other Conditional Statements
Comparing Data
© 2004 Pearson Addison-Wesley. All rights reserved 5-31
Comparing Data
• When comparing data using boolean expressions,
it's important to understand the nuances of certain
data types
• Let's examine some key situations:
 Comparing floating point values for equality
 Comparing characters
 Comparing strings (alphabetical order)
 Comparing object vs. comparing object references
© 2004 Pearson Addison-Wesley. All rights reserved 5-32
Comparing Float Values
• You should rarely use the equality operator (==)
when comparing two floating point values (float
or double)
• Two floating point values are equal only if their
underlying binary representations match exactly
• Computations often result in slight differences that
may be irrelevant
• In many situations, you might consider two
floating point numbers to be "close enough" even
if they aren't exactly equal
© 2004 Pearson Addison-Wesley. All rights reserved 5-33
Comparing Float Values
• To determine the equality of two floats, you may
want to use the following technique:
if (Math.abs(f1 - f2) < TOLERANCE)
System.out.println ("Essentially equal");
• If the difference between the two floating point
values is less than the tolerance, they are
considered to be equal
• The tolerance could be set to any appropriate
level, such as 0.000001
© 2004 Pearson Addison-Wesley. All rights reserved 5-34
Comparing Characters
• As we've discussed, Java character data is based
on the Unicode character set
• Unicode establishes a particular numeric value for
each character, and therefore an ordering
• We can use relational operators on character data
based on this ordering
• For example, the character '+' is less than the
character 'J' because it comes before it in the
Unicode character set
• Appendix C provides an overview of Unicode
© 2004 Pearson Addison-Wesley. All rights reserved 5-35
Comparing Characters
• In Unicode, the digit characters (0-9) are
contiguous and in order
• Likewise, the uppercase letters (A-Z) and
lowercase letters (a-z) are contiguous and in order
97 through 122a – z
65 through 90A – Z
48 through 570 – 9
Unicode ValuesCharacters
© 2004 Pearson Addison-Wesley. All rights reserved 5-36
Comparing Strings
• Remember that in Java a character string is an
object
• This means that you should not use == to test if
two strings have the same value
• The equals method can be called with strings to
determine if two strings contain exactly the same
characters in the same order
• The equals method returns a boolean result
if (name1.equals(name2))
System.out.println ("Same name");
© 2004 Pearson Addison-Wesley. All rights reserved 5-37
Comparing Strings
• We cannot use the relational operators to compare
strings
• The String class contains a method called
compareTo to determine if one string comes
before another
• A call to name1.compareTo(name2)
 returns zero if name1 and name2 are equal (contain the
same characters)
 returns a negative value if name1 is less than name2
 returns a positive value if name1 is greater than name2
© 2004 Pearson Addison-Wesley. All rights reserved 5-38
Comparing Strings
if (name1.compareTo(name2) < 0)
System.out.println (name1 + "comes first");
else
if (name1.compareTo(name2) == 0)
System.out.println ("Same name");
else
System.out.println (name2 + "comes first");
• Because comparing characters and strings is
based on a character set, it is called a
lexicographic ordering
© 2004 Pearson Addison-Wesley. All rights reserved 5-39
Lexicographic Ordering
• Lexicographic ordering is not strictly alphabetical
when uppercase and lowercase characters are
mixed
• For example, the string "Great" comes before the
string "fantastic" because all of the uppercase
letters come before all of the lowercase letters in
Unicode
• Also, short strings come before longer strings
with the same prefix (lexicographically)
• Therefore "book" comes before "bookcase"

More Related Content

Similar to Week03

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...mshakeel44514451
 
Java Chapter 05 - Conditions & Loops: part 2
Java Chapter 05 - Conditions & Loops: part 2Java Chapter 05 - Conditions & Loops: part 2
Java Chapter 05 - Conditions & Loops: part 2DanWooster1
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Muhammad Tahir Bashir
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming LanguageAhmad Idrees
 
03_Gaddis Python_Lecture_ppt_ch03.pptx
03_Gaddis Python_Lecture_ppt_ch03.pptx03_Gaddis Python_Lecture_ppt_ch03.pptx
03_Gaddis Python_Lecture_ppt_ch03.pptxssuser58efc81
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3dplunkett
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsDanWooster1
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 Bdplunkett
 
Copyright © 2018 Pearson Education, Inc.C H A P T E R 3.docx
Copyright © 2018 Pearson Education, Inc.C H A P T E R  3.docxCopyright © 2018 Pearson Education, Inc.C H A P T E R  3.docx
Copyright © 2018 Pearson Education, Inc.C H A P T E R 3.docxdickonsondorris
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrolsteach4uin
 

Similar to Week03 (20)

slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
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...
 
Java Chapter 05 - Conditions & Loops: part 2
Java Chapter 05 - Conditions & Loops: part 2Java Chapter 05 - Conditions & Loops: part 2
Java Chapter 05 - Conditions & Loops: part 2
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Control structure
Control structureControl structure
Control structure
 
03_Gaddis Python_Lecture_ppt_ch03.pptx
03_Gaddis Python_Lecture_ppt_ch03.pptx03_Gaddis Python_Lecture_ppt_ch03.pptx
03_Gaddis Python_Lecture_ppt_ch03.pptx
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
 
Chapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loopsChapter 6 - More conditionals and loops
Chapter 6 - More conditionals and loops
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
 
Selection
SelectionSelection
Selection
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Lect8
Lect8Lect8
Lect8
 
PL/SQL CONDICIONALES Y CICLOS
PL/SQL CONDICIONALES Y CICLOSPL/SQL CONDICIONALES Y CICLOS
PL/SQL CONDICIONALES Y CICLOS
 
Copyright © 2018 Pearson Education, Inc.C H A P T E R 3.docx
Copyright © 2018 Pearson Education, Inc.C H A P T E R  3.docxCopyright © 2018 Pearson Education, Inc.C H A P T E R  3.docx
Copyright © 2018 Pearson Education, Inc.C H A P T E R 3.docx
 
Using decision statements
Using decision statementsUsing decision statements
Using decision statements
 
ch05.ppt
ch05.pptch05.ppt
ch05.ppt
 
Ch04
Ch04Ch04
Ch04
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
 

More from hccit

Snr ipt 10_syll
Snr ipt 10_syllSnr ipt 10_syll
Snr ipt 10_syllhccit
 
Snr ipt 10_guide
Snr ipt 10_guideSnr ipt 10_guide
Snr ipt 10_guidehccit
 
3 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr113 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr11hccit
 
10 ict photoshop_proj_2014
10 ict photoshop_proj_201410 ict photoshop_proj_2014
10 ict photoshop_proj_2014hccit
 
Photoshop
PhotoshopPhotoshop
Photoshophccit
 
Flash
FlashFlash
Flashhccit
 
Griffith sciences pathway programs overview
Griffith sciences pathway programs overviewGriffith sciences pathway programs overview
Griffith sciences pathway programs overviewhccit
 
Griffith info tech brochure
Griffith info tech brochureGriffith info tech brochure
Griffith info tech brochurehccit
 
Pm sql exercises
Pm sql exercisesPm sql exercises
Pm sql exerciseshccit
 
Repairs questions
Repairs questionsRepairs questions
Repairs questionshccit
 
Movies questions
Movies questionsMovies questions
Movies questionshccit
 
Australian birds questions
Australian birds questionsAustralian birds questions
Australian birds questionshccit
 
Section b
Section bSection b
Section bhccit
 
Section a
Section aSection a
Section ahccit
 
Ask manual rev5
Ask manual rev5Ask manual rev5
Ask manual rev5hccit
 
Case study report mj
Case study report mjCase study report mj
Case study report mjhccit
 
Mj example case_study_layout_intro_completedq
Mj example case_study_layout_intro_completedqMj example case_study_layout_intro_completedq
Mj example case_study_layout_intro_completedqhccit
 
Case study plan mj
Case study plan mjCase study plan mj
Case study plan mjhccit
 

More from hccit (20)

Snr ipt 10_syll
Snr ipt 10_syllSnr ipt 10_syll
Snr ipt 10_syll
 
Snr ipt 10_guide
Snr ipt 10_guideSnr ipt 10_guide
Snr ipt 10_guide
 
3 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr113 d modelling_task_sheet_2014_yr11
3 d modelling_task_sheet_2014_yr11
 
10 ict photoshop_proj_2014
10 ict photoshop_proj_201410 ict photoshop_proj_2014
10 ict photoshop_proj_2014
 
Photoshop
PhotoshopPhotoshop
Photoshop
 
Flash
FlashFlash
Flash
 
Griffith sciences pathway programs overview
Griffith sciences pathway programs overviewGriffith sciences pathway programs overview
Griffith sciences pathway programs overview
 
Griffith info tech brochure
Griffith info tech brochureGriffith info tech brochure
Griffith info tech brochure
 
Pm sql exercises
Pm sql exercisesPm sql exercises
Pm sql exercises
 
Repairs questions
Repairs questionsRepairs questions
Repairs questions
 
Movies questions
Movies questionsMovies questions
Movies questions
 
Australian birds questions
Australian birds questionsAustralian birds questions
Australian birds questions
 
Section b
Section bSection b
Section b
 
B
BB
B
 
A
AA
A
 
Section a
Section aSection a
Section a
 
Ask manual rev5
Ask manual rev5Ask manual rev5
Ask manual rev5
 
Case study report mj
Case study report mjCase study report mj
Case study report mj
 
Mj example case_study_layout_intro_completedq
Mj example case_study_layout_intro_completedqMj example case_study_layout_intro_completedq
Mj example case_study_layout_intro_completedq
 
Case study plan mj
Case study plan mjCase study plan mj
Case study plan mj
 

Recently uploaded

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 

Recently uploaded (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

Week03

  • 2. © 2004 Pearson Addison-Wesley. All rights reserved 5-2 Copyright Warning COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been copied and communicated to you by or on behalf of Bond University pursuant to Part VB of the Copyright Act 1968 (the Act). The material in this communication may be subject to copyright under the Act. Any further copying or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.
  • 3. © 2004 Pearson Addison-Wesley. All rights reserved 5-3 Conditional Statements • Now we will examine programming statements that allow us to:  make decisions  perform commands based on those decisions • Chapter 5 focuses on:  boolean expressions  conditional statements  comparing data
  • 4. © 2004 Pearson Addison-Wesley. All rights reserved 5-4 Outline The if Statement and Conditions Other Conditional Statements Comparing Data
  • 5. © 2004 Pearson Addison-Wesley. All rights reserved 5-5 Flow of Control • Unless specified otherwise, the order of statement execution through a method is linear: one statement after another in sequence • Some programming statements allow us to:  decide whether or not to execute a particular statement  execute a statement over and over, repetitively • These decisions are based on boolean expressions (or conditions) that evaluate to true or false • The order of statement execution is called the flow of control
  • 6. © 2004 Pearson Addison-Wesley. All rights reserved 5-6 Conditional Statements • A conditional statement lets us choose which statement will be executed next • Therefore they are sometimes called selection statements • Conditional statements give us the power to make basic decisions • The Java conditional statements are the:  IF statement  SWITCH statement
  • 7. © 2004 Pearson Addison-Wesley. All rights reserved 5-7 The IF Statement • Looks like the following if ( condition ) { statement1; statement2; } else { statement3; statement4; } • If the condition is true, statements 1 & 2 are executed; if the condition is false, statements 3 & 4 are executed • One section or the other will be executed, but not both
  • 8. © 2004 Pearson Addison-Wesley. All rights reserved 5-8 The IF Statement • The else { … } section is optional  If we don't need to do anything on a false decision, we can leave it out • Also, if there is only 1 line of code to do in each section, we can leave out the curly brackets { } • For example: if ( age < 18 )         System.out.println(“Too young to come in”);
  • 9. © 2004 Pearson Addison-Wesley. All rights reserved 5-9 Logic of an IF statement condition evaluated True block true false False block
  • 10. © 2004 Pearson Addison-Wesley. All rights reserved 5-10 Block Statements • Several statements can be grouped together into a block statement delimited by braces • A block statement can be used wherever a statement is called for in the Java syntax rules if (total > MAX) { System.out.println ("Error!!"); errorCount++; }
  • 11. © 2004 Pearson Addison-Wesley. All rights reserved 5-11 Block Statements • In an IF statement, the if portion, or the else portion, or both, could be block statements if (total > MAX) { System.out.println ("Error!!"); errorCount++; } else { System.out.println ("Total: " + total); current = total*2; }
  • 12. © 2004 Pearson Addison-Wesley. All rights reserved 5-12 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 (=)
  • 13. © 2004 Pearson Addison-Wesley. All rights reserved 5-13 The if Statement • An example of an if statement: if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); • 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. • Either way, the call to println is executed next
  • 14. © 2004 Pearson Addison-Wesley. All rights reserved 5-14 Indentation • The statement controlled by the if statement is indented to indicate that relationship • The use of a consistent indentation style makes a program easier to read and understand • Although it makes no difference to the compiler, proper indentation is crucial "Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding
  • 15. © 2004 Pearson Addison-Wesley. All rights reserved 5-15 The if Statement • What do the following statements do? if (top >= MAXIMUM) top = 0; if (total != stock + warehouse) inventoryError = true; • The precedence of the arithmetic operators is higher than the precedence of the equality and relational operators
  • 16. © 2004 Pearson Addison-Wesley. All rights reserved 5-16 Logical Operators: Combining Comparisons Together • 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)
  • 17. © 2004 Pearson Addison-Wesley. All rights reserved 5-17 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 truefalse falsetrue !aa
  • 18. © 2004 Pearson Addison-Wesley. All rights reserved 5-18 Logical AND Operator • The logical AND expression a && b is true if both a and b are true, and false otherwise • Example: if ( (x > 10) && (x < 20) ) • The whole condition is true if x > 10 AND x < 20 AT THE SAME TIME!
  • 19. © 2004 Pearson Addison-Wesley. All rights reserved 5-19 Logical OR Operator • The logical OR expression a || b is true if a or b or both are true, and false otherwise • Example: if ( (ch == 'a') || (ch == 'A') ) • The whole condition is true if ch is either the letter 'a' OR the letter 'A'.
  • 20. © 2004 Pearson Addison-Wesley. All rights reserved 5-20 Logical Operators • Expressions that use logical operators can form complex conditions if (total < MAX+5 && !found) System.out.println ("Processing…"); • All logical operators have lower precedence than the relational operators • Logical NOT has higher precedence than logical AND and logical OR • When in doubt, use parentheses to enforce the order of evaluation
  • 21. © 2004 Pearson Addison-Wesley. All rights reserved 5-21 Short-Circuited Operators • The processing of logical AND and logical OR is “short-circuited” • This is known as lazy evaluation • If the left operand is sufficient to determine the result, the right operand is not evaluated • This type of processing must be used carefully if (count != 0 && total/count > MAX) System.out.println ("Testing…");
  • 22. © 2004 Pearson Addison-Wesley. All rights reserved 5-22 Indentation Revisited • Remember that indentation is for the human reader, and is ignored by the computer if (total > MAX) System.out.println ("Error!!"); errorCount++; Despite what is implied by the indentation, the increment will occur whether the condition is true or not
  • 23. © 2004 Pearson Addison-Wesley. All rights reserved 5-23 Nested if Statements • The statement executed as a result of an if statement or else clause could be another if statement • These are called nested if statements • See MinOfThree.java (page 219) • An else clause is matched to the last unmatched if (no matter what the indentation implies) • Braces can be used to specify the if statement to which an else clause belongs
  • 24. © 2004 Pearson Addison-Wesley. All rights reserved 5-24 The SWITCH Statement • The switch statement provides another way to decide which statement to execute next • The switch statement evaluates an expression, then attempts to match the result to one of several possible cases • Each case contains a value and a list of statements • The flow of control transfers to statement associated with the first case value that matches
  • 25. © 2004 Pearson Addison-Wesley. All rights reserved 5-25 The SWITCH Statement • The general syntax of a switch statement is: switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } switch and case are reserved words If expression matches value2, control jumps to here
  • 26. © 2004 Pearson Addison-Wesley. All rights reserved 5-26 The switch Statement • Often a break statement is used as the last statement in each case's statement list • A break statement causes control to transfer to the end of the switch statement • If a break statement is not used, the flow of control will continue into the next case • Sometimes this may be appropriate, but often we want to execute only the statements associated with one case
  • 27. © 2004 Pearson Addison-Wesley. All rights reserved 5-27 The switch Statement switch (option) { case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; } • An example of a switch statement:
  • 28. © 2004 Pearson Addison-Wesley. All rights reserved 5-28 The switch Statement • A switch statement can have an optional default case • The default case has no associated value and simply uses the reserved word default • If the default case is present, control will transfer to it if no other case value matches • If there is no default case, and no other value matches, control falls through to the statement after the switch
  • 29. © 2004 Pearson Addison-Wesley. All rights reserved 5-29 The switch Statement • The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int, long) or a char • It cannot be a boolean value or a floating point value (float or double) • The implicit boolean condition in a switch statement is equality • You cannot perform relational checks with a switch statement • See GradeReport.java (page 225)
  • 30. © 2004 Pearson Addison-Wesley. All rights reserved 5-30 Outline The if Statement and Conditions Other Conditional Statements Comparing Data
  • 31. © 2004 Pearson Addison-Wesley. All rights reserved 5-31 Comparing Data • When comparing data using boolean expressions, it's important to understand the nuances of certain data types • Let's examine some key situations:  Comparing floating point values for equality  Comparing characters  Comparing strings (alphabetical order)  Comparing object vs. comparing object references
  • 32. © 2004 Pearson Addison-Wesley. All rights reserved 5-32 Comparing Float Values • You should rarely use the equality operator (==) when comparing two floating point values (float or double) • Two floating point values are equal only if their underlying binary representations match exactly • Computations often result in slight differences that may be irrelevant • In many situations, you might consider two floating point numbers to be "close enough" even if they aren't exactly equal
  • 33. © 2004 Pearson Addison-Wesley. All rights reserved 5-33 Comparing Float Values • To determine the equality of two floats, you may want to use the following technique: if (Math.abs(f1 - f2) < TOLERANCE) System.out.println ("Essentially equal"); • If the difference between the two floating point values is less than the tolerance, they are considered to be equal • The tolerance could be set to any appropriate level, such as 0.000001
  • 34. © 2004 Pearson Addison-Wesley. All rights reserved 5-34 Comparing Characters • As we've discussed, Java character data is based on the Unicode character set • Unicode establishes a particular numeric value for each character, and therefore an ordering • We can use relational operators on character data based on this ordering • For example, the character '+' is less than the character 'J' because it comes before it in the Unicode character set • Appendix C provides an overview of Unicode
  • 35. © 2004 Pearson Addison-Wesley. All rights reserved 5-35 Comparing Characters • In Unicode, the digit characters (0-9) are contiguous and in order • Likewise, the uppercase letters (A-Z) and lowercase letters (a-z) are contiguous and in order 97 through 122a – z 65 through 90A – Z 48 through 570 – 9 Unicode ValuesCharacters
  • 36. © 2004 Pearson Addison-Wesley. All rights reserved 5-36 Comparing Strings • Remember that in Java a character string is an object • This means that you should not use == to test if two strings have the same value • The equals method can be called with strings to determine if two strings contain exactly the same characters in the same order • The equals method returns a boolean result if (name1.equals(name2)) System.out.println ("Same name");
  • 37. © 2004 Pearson Addison-Wesley. All rights reserved 5-37 Comparing Strings • We cannot use the relational operators to compare strings • The String class contains a method called compareTo to determine if one string comes before another • A call to name1.compareTo(name2)  returns zero if name1 and name2 are equal (contain the same characters)  returns a negative value if name1 is less than name2  returns a positive value if name1 is greater than name2
  • 38. © 2004 Pearson Addison-Wesley. All rights reserved 5-38 Comparing Strings if (name1.compareTo(name2) < 0) System.out.println (name1 + "comes first"); else if (name1.compareTo(name2) == 0) System.out.println ("Same name"); else System.out.println (name2 + "comes first"); • Because comparing characters and strings is based on a character set, it is called a lexicographic ordering
  • 39. © 2004 Pearson Addison-Wesley. All rights reserved 5-39 Lexicographic Ordering • Lexicographic ordering is not strictly alphabetical when uppercase and lowercase characters are mixed • For example, the string "Great" comes before the string "fantastic" because all of the uppercase letters come before all of the lowercase letters in Unicode • Also, short strings come before longer strings with the same prefix (lexicographically) • Therefore "book" comes before "bookcase"