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

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Selection Statements Make a decision based on conditions Allows the computer to be intelligent.
If Statements, Try Catch and Validation. The main statement used in C# for making decisions depending on different conditions is called the If statement.
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
If Statements & Relational Operators Programming.
1 Fall 2007ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Fall 2008ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Conditions, logical expressions, and selection Introduction to control structures.
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
3.2 – Truth Tables and Equivalent Statements
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
Conditionals & boolean operators
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program.
Simple Control Structures IF, IF-ELSE statements in C.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
If Statements If statements: allow the computer to make a decision Syntax: if (some condition) { then do this; } else { then do that; } no semicolons on.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Control statements Mostafa Abdallah
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Logical Thinking CS 104 9/12/11. Agenda Today  College Board survey reminder  Note: Simple “how to” guide on Scratch posted on eLearning  Review HW.
CSI 3125, Preliminaries, page 1 Control Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS 139 – Programming Fundamentals Lecture 08A. Relational/comparison Operators Relational Operator Meaning > is greater than < is less than >= is greater.
CS305j Introduction to Computing More Conditional Execution 1 Topic 13 More Conditional Execution " Great dancers are not great because of their technique;
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 3: Decisions and Loops
Sequence, Selection, Iteration The IF Statement
Section 7.1 Logical Operators
Operator Precedence Operators Precedence Parentheses () unary
Control Structures.
JavaScript Selection Statement Creating Array
IF if (condition) { Process… }
Unit 3 - The while Loop - Extending the Vic class - Examples
Pages:51-59 Section: Control1 : decisions
Computer Science Core Concepts
Conditional Logic Presentation Name Course Name
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Pages:51-59 Section: Control1 : decisions
If-Statements and If/Else Statements
Life is Full of Alternatives Part 3
Presentation transcript:

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

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.

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

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

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)

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

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

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));

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

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