Java Decision Making and booleans (Java: An Eventful Approach, Ch 4), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture 14 26 October 2012.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS&E 1111 ExIFs IFs and Nested IFs in Excel Objectives: Using the If function in spreadsheets Nesting If functions.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
CS0007: Introduction to Computer Programming
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
CSCI1402: Lecture 1 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Visual C++ Programming: Concepts and Projects
CIS101 Introduction to Computing Week 12 Spring 2004.
Q and A for Section 4.4 CS 106, Fall Q1 Q: The syntax for an if statement (or conditional statement) is: if _______________ : _____________ A: if.
Lecture 2: Static Methods, if statements, homework uploader.
Decision Structures and Boolean Logic
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.
Decision Structures Chapter 4 Part 2. Chapter 4 Objectives To understand o What relational operators are and how they are used o Boolean logic o Testing.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Java Loops (Java: An Eventful Approach, Ch 7 and 13), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture 16 6 November 2012.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Flow Control: boolean expressions, “if” selection statements (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September.
Conditional Statements Consider: if the mouse location is contained in the rectangle, display message “success” Some programming constructs can choose.
Chapter 4 Conditional Statements. A Programmer's Lament I really hate this damned machine; I wish that they would sell it It never does quite what I want.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
COMP 110: Spring Announcements Lab 1 was due at noon Lab 2 on Friday (Bring Laptops) Assignment 1 is online TA Office hours online 30-min quiz at.
Java Working with Numbers (Java: An Eventful Approach, Ch 3), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture October 2012.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture October.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
COMP Loop Statements Yi Hong May 21, 2015.
1 Control Structures (Chapter 3) 3 constructs are essential building blocks for programs Sequences  compound statement Decisions  if, switch, conditional.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Control Statements: Part1  if, if…else, switch 1.
Java Objects (Java: An Eventful Approach, Ch 2), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture October 2012.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
Decision making If.. else statement.
If/Else Statements.
Chapter 3: Decisions and Loops
Topics The if Statement The if-else Statement Comparing Strings
Simple Control Structures
Chapter 4: Decision Structures and Boolean Logic
Java Classes (Java: An Eventful Approach, Ch. 6),
Control Structures: Selection Statement
Chapter 4: Decision Structures and Boolean Logic
Decision making If statement.
Control structures Chapter 3.
Selection Statements.
Control structures Chapter 3.
Lecture 6: Conditionals AP Computer Science Principles
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control structures Chapter 3.
Control Structures: Selection Statement
Chapter 4: Decision Structures and Boolean Logic
Control Structure.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

Java Decision Making and booleans (Java: An Eventful Approach, Ch 4), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture October 2012

Conditional Statements Consider: if the mouse location is contained in the rectangle, display message “success” Some programming constructs can choose between blocks of code to execute

A Vote Counting Example Complete source code

Code to Update Votes: // Update votes and display vote counts public void onMouseClick( Location point ) { if ( point.getX() < MID_X ) { countA++; infoA.setText( "So far there are " + countA + " vote(s) for A." ); } else { countB++; infoB.setText( "So far there are " + countB + " vote(s) for B." ); }

Syntax of the if Statement The condition is evaluated. If true, the bracketed code following if is executed; otherwise, the bracketed code after else is executed if (condition) { //do something } else { //do something else } The else part can be omitted if (condition) { //do something here }

if if-else

if Statement and 2D Objects To check if the mouse is within a 2D object: public void onMouseClick ( Location point ) { if ( anObject.contains ( point ) ) { //do something here }

Comparison Operators OperatorDescription >Greater than <Less than ==Equal to <=Less than or equal to >=Greater than or equal to !=Not equal to

Examples If a=25, b=30 a<b evaluates to true a<=b evaluates to true a==b evaluates to false a!=b evaluates to true a>b evaluates to false a>=b evaluates to false Since these expressions are evaluated to either true or false, they are called boolean expressions

The boolean Data Type A data type that has only two values: true or false Can be declared with the word boolean Ex:boolean a=true; //holds the value true boolean b=c>d; //holds the result of c > d boolean k; //creates k, which can take on true or false

Dragging a Box //boolean variable to determine whether the box is grabbed private boolean boxGrabbed; // Save starting point and whether point was in box public void onMousePress( Location point ) { lastPoint = point; boxGrabbed = box.contains( point ); } // if mouse is in box, then drag the box public void onMouseDrag( Location point ) { if ( boxGrabbed ) { box.move( point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY() ); lastPoint = point; } Complete source code

More Uses of The if else Statement Picks one choice among many EX:Converting a score into a letter grade if ( score >= 90 ) { gradeDisplay.setText( "The grade is A" ); } else if ( score >= 80 ) { gradeDisplay.setText( "The grade is B" ); } else if ( score >= 70 ) { gradeDisplay.setText( "The grade is C" ); } else { gradeDisplay.setText( "No credit is given"); }

Combining Multiple Conditionals && (and) combines adjoining conditions in a way that the final result will be true only if all are true Ex: a && b && c is true if a,b,c are true || (or) combines adjoining conditions in a way that if any of them is true, the final result will be true Ex: a || b || c is true if any of a, b, c, is true

The Craps Example A block of code that uses || (or) to determine whether the player wins a game of Craps if ( roll == 7 || roll == 11 ) { // 7 or 11 wins on first throw status.setText( "You win!" ); } else if ( roll == 2 || roll == 3 || roll == 12 ) { // 2, 3, or 12 loses status.setText( "You lose!" ); } else { // Set roll to be the point to be made status.setText( "Try for your point!" ); point = roll; … } Complete source code

Nesting Suppose we want to decide among several choices based on several conditions, such as shown by the table: To do this, we use conditionals inside a conditional. This is called nesting. SunnyNot sunny RichOutdoor ConcertIndoor Concert Not RichUltimate FrisbeeWatch TV

Nesting Example if ( sunny ) { if ( rich ) { activityDisplay.setText( "outdoor concert" ); } else { // not rich activityDisplay.setText( "play ultimate" ); } } else { // not sunny if ( rich ) { activityDisplay.setText( "indoor concert" ); } else { // not rich activityDisplay.setText( "watch TV" ); }

Creating a Game of Craps By adding an outer conditional, we can effectively determine whether the player wins: if ( newGame ) {//Starting a new game if ( roll == 7 || roll == 11 ) { // 7 or 11 wins on first throw status.setText( "You win!" ); } else if ( roll == 2 || roll == 3 || roll == 12 ) { // 2, 3, or 12 loses status.setText( "You lose!" ); } else { // Set roll to be the point to be made status.setText( "Try for your point!" ); point = roll; newGame = false; // no longer a new game } Complete source code

Student To Do’s HW6 (Three problems) –Bring your solutions to lab. –(If no class on Monday, check your for submission instructions) Read Java: An Eventful Approach –Ch. 4 (Today) –Ch. 5-6 (Next week) Don’t fall behind! We’re moving quickly through Java topics because we already know the concepts from Alice! 18