M4 notes - CSC 211 NOTES W i nt e r 2 0 1 1 : A mb e r S e...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSC 211 NOTES Winter 2011: Amber Settle Module: Logical expressions and branching statements Flow of control So far all of our main methods consisted of what is called straight-line code . This means that the sequence of statements executed was the same each time we ran the program. We can write much more interesting programs if we can modify the flow of control , that is, change what statements are executed and in which order based on certain conditions. There are two fundamental types of control statements: 1. Branching statements : Execute one piece of code or another 2. Looping statements : Execute a piece of code repeatedly Both types of statements require you to execute code if a certain condition holds. To construct statements that allow decision-making, we need to learn about relational and logical operators. Boolean expressions, relational and logical operators When we make decisions about how to act, it is often based on whether a certain condition holds or does not hold. Example : If the temperature is above 60 degrees and it is not raining, I will ride my scooter to work. Otherwise I will take the train to work. Boolean expressions (also called test or conditions) are expressions that evaluate to either true or false. Relational operators take two values, compare them in some way, and return either true or false based on the comparison. The relational operators in Java: = = (equality) != (inequality) < (less than) <= (less than or equal to) > (greater than)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 >= (greater than or equal to) Important points: The relational operators above only work for primitive data types – do not use them for classes such as String, Scanner, etc. Note that = = (test for equality) and = (assignment) are different. Example: DoCompare.java The difficulty of comparison in Java: Integers : Comparisons are straightforward Characters : These are compared according to their Unicode values (see the Java API or Appendix C beginning on page 897 of the textbook for more information) Decimal values o Floating point values can appear to be equal but fail the = = test o Imprecision in arithmetic can lead to erroneous results. Example: (1.0/3.0) * 3.0 may not be equal to 1.0 as it depends
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/05/2011 for the course CSC 211 taught by Professor Chan during the Spring '11 term at CA Health Sciences.

Page1 / 7

M4 notes - CSC 211 NOTES W i nt e r 2 0 1 1 : A mb e r S e...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online