Unit 7 Conditional statements Logical operators in Java

  • Slides: 10
Download presentation
Unit 7 - Conditional statements - Logical operators in Java - Example 1 AP

Unit 7 - Conditional statements - Logical operators in Java - Example 1 AP Computer Science A – Healdsburg High School

Conditional Statements in Java (also called “decision” statements) We have 3 ways of making

Conditional Statements in Java (also called “decision” statements) We have 3 ways of making decisions: 1. The “if” statement Syntax: if(condition) { statement; … } If the condition is true, the program will execute these statements If the condition is false, the program will resume after the curly brace. AP Computer Science A – Healdsburg High School

2. The “if-else” statement Syntax: if(condition) { statement; … } else { statement; …

2. The “if-else” statement Syntax: if(condition) { statement; … } else { statement; … } If the condition is true, the program will execute these statements If the condition is false, the program will execute these statements AP Computer Science A – Healdsburg High School

3. The “if-else if” statement Syntax: if(condition 1) { statement; … } else if(condition

3. The “if-else if” statement Syntax: if(condition 1) { statement; … } else if(condition 2) { statement; … } else { statement; … } If the condition 1 is true, the program will execute these statements If the condition 2 is true, the program will execute these statements If neither condition is true, the program will execute these statements AP Computer Science A – Healdsburg High School

Logical Operators At times we need to ask more complex questions: • if more

Logical Operators At times we need to ask more complex questions: • if more than 1 condition is true (AND) • if either condition is true (OR) • if a condition is not true (NOT) Logical operators in Java (and in C and C++) Logic 5 Java operator AND && OR || NOT ! (in front of condition) AP Computer Science A – Healdsburg High School

Truth Tables in Logic 6 p q T T T F F p and

Truth Tables in Logic 6 p q T T T F F p and q p or q not p AP Computer Science A – Healdsburg High School not q

Relational Operators Relational operators in Java (and in C and C++) Math 7 Java

Relational Operators Relational operators in Java (and in C and C++) Math 7 Java operator Means < < less than ≤ <= less than or equal to > ≥ > >= greater than or equal to = ≠ == != equal to not equal to AP Computer Science A – Healdsburg High School

How to use Operators in your Program Examples int num. Students = APCalculus. get.

How to use Operators in your Program Examples int num. Students = APCalculus. get. Num. Students(); if(num. Students == 30) System. out. println(“class is full”); else if (num. Students < 30) System. out. println(“Open seats: ” + (30 -num. Students)); else System. out. println(“Overage: ” + (num. Students-30)); AP Computer Science A – Healdsburg High School

Order of Operations 1 2 () ++ [] -. 9 3 4 5 6

Order of Operations 1 2 () ++ [] -. 9 3 4 5 6 ! * / % + - < > ≥ ≤ (type) - (neg) 7 8 9 10 11 == && || ? : = != += -= *= /= %/ AP Computer Science A – Healdsburg High School

Example Write an application that creates two Vic objects and performs the following task:

Example Write an application that creates two Vic objects and performs the following task: For each slot that both Vics have valid slots, do the following: -if both Vics have CDs, just move both Vics to the next slot -if only one of the Vics has a CD in its slot, take the CD and put it in the other Vics slot, and move both Vics to the next slot 10 - if neither of the Vics have CDs, just move both Vics to the next slot AP Computer Science A – Healdsburg High School