Title: Simple Logic Summary
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Simple Logic Summary

"if" Statement Basics

- Simple if statements
- if-then-else statements
- Using { ... } to create compound statements
- boolean expressions using the operators ==, !=, <, >, <=, >=
- if-then-else if- .... else statements

"if" Statement Examples

- Examples to demonstrate "if" statement concepts
  - IncomeTax.java
  - MinOfThree.java
  - Grades.java

"while" loops

- while(boolean_expression) statement;
- executes 0 or more times depending on the "boolean_expression"
- Using { ... } to create compound statements
- boolean expressions are identical to those used in "if" statements
- do statement; while(boolean_expression);
- executes 1 or more times depending on the "boolean_expression"
- Examples: WhileExamples.java, Averager.java, WhileDemo.java

"while" loop examples

- More Meaningful Examples:
  - Power.java (uses while loop)
  - GuessingGame.java (using a while loop)
  - GuessingGame.java (using a do-while loop)
  - Multiplication.java (uses nested while loops to create a multiplication table)

String Equals Comparisons

- Operators that we used for numbers like ==, <, >, etc. don't work as you would expect with Strings
- Use the equals method for Strings, or the other methods: equalsIgnoreCase, compareToIgnoreCase
- Examples: StringCompare.java, StringComparisonDemo.java

"for" loops

- for (initialization statement(s) ; boolean_condition ; end of loop statement(s)) statement;
- executes 0 or more times depending on the "boolean_expression"
- Using { ... } to create compound statements
- Counter declared in "for" loop versus declaring before the "for" loop
- Unusual versions: leaving blank entries, and multiple initializations, or multiple increments
- Common Pitfall: Misplaced ";" ... can happen in a "while" loop or "if" statement as well
- Example: ForLoops.java

"for" loop examples

- Multiplication Tables using nested "for" loops
- Miscellaneous.java to demonstrate "continue" and "break" key words.
- GuessingGame.java with a "for" loop limit on the number of guesses

"switch" examples

- SwitchDemo.java - comparison of the "if" statement approach to a switch statement approach with months of the year
- switch statement is normally used for integer like primitives: byte, int, short, long, char, or enumerated types(covered later)
- switch statements don't work with floats or doubles
- As of release 1.7, java now supports switch statements with the String class.

More boolean concepts

- Review of String issues with == and using the method equals, or possibly the methods equalsIgnoreCase, or compareTo
- New boolean operators: &&, ||, and !
- How to evaluate ranges like: 5 < x <= 20 using the && operator
- Using the operator precedence table to evaluate expressions ... Parentheses can change the order or clarify the existing evaluation order
- Short circuit logic with && and || and taking advantage of this to write robust code

Last Modified: May 31, 2014 11:19 AM