Docsity

Java Programming: Loops, Logic and Control Statements, Slides of Object Oriented Programming

Partial preview of the text

Download Java Programming: Loops, Logic and Control Statements and more Slides Object Oriented 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. Example: TrySwitch.java
  • 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 counter can be integer or float-point
  • Nested loops, example Factorial.java
  • Break, continue, labeled break and

continue

  • Assertion: “ assert logical-exp; ”
  • Run with “java –ea ”
  • Example TryAssertions.java

Loops