Docsity

Java Programming: Loops and Logic, Slides of Java Programming

Partial preview of the text

Download Java Programming: Loops and Logic and more Slides Java Programming in PDF only on Docsity!

1

Chapter 3: Loops and Logic

2

Control Statements

  • If statement
  • Example NumberCheck.java
  • Relational operators (<, <=, >, >=, ==, !=)
  • Using code blocks with If
  • The else statement
  • Nested If
  • Example: LetterCheck.java

4

  • It has the following form and always returns a value of one of the two exps.
  • Logical exp? exp1:exp2;
  • Example: ConditionalOp.java
  • The switch statement can perform multiple selections.
  • Using the break statement
  • Local variables are visible only in limited scope
  • Example: Scope.java

Conditional Operator and switch

5

  • Different types (for, while, do—while)
  • Initialization, test, & increment conditions
  • Loop counters can be int or float
  • Nested loops, example Factorial.java
  • Break, continue, labeled break and continue
  • Assertion: “ assert logical-exp; ”
  • Compile with “-source 1.4” and run with “-ea”, example TryAssertions.java

Loops