Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "AP Computer Science A – Healdsburg High School 1 Unit 7 - Conditional statements - Logical operators in Java - Example."— Presentation transcript:

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

2 AP Computer Science A – Healdsburg High School 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.

3 AP Computer Science A – Healdsburg High School 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

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

5 AP Computer Science A – Healdsburg High School 5 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++) LogicJava operator AND && OR || NOT ! (in front of condition)

6 AP Computer Science A – Healdsburg High School 6 Truth Tables in Logic pqp and qp or qnot pnot q TT TF FT FF

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

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

9 AP Computer Science A – Healdsburg High School 9 Order of Operations 1234567891011 () []. ++ -- ! (type) - (neg) */%*/% +-+- <>≥≤<>≥≤ == != &&||?:= += -= *= /= %/

10 AP Computer Science A – Healdsburg High School 10 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 - if neither of the Vics have CDs, just move both Vics to the next slot


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

Similar presentations


Ads by Google