Conditional Statements Consider: if the mouse location is contained in the rectangle, display message “success” Some programming constructs can choose.

Slides:



Advertisements
Similar presentations
CSC 121 Computers and Scientific Thinking Fall Conditional Execution.
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.
Chapter 4 - Control Statements
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.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
true (any other value but zero) false (zero) expression Statement 2
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,
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
Running JavaScript Chapter 18.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Java Programming: From The ground Up  Chapter 4 Selection and Decision: if Statements.
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - 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.
22/11/ Selection If selection construct.
Java Decision Making and booleans (Java: An Eventful Approach, Ch 4), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture October 2012.
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.
Decision Structures, String Comparison, Nested Structures
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.
Lesson thirteen Conditional Statement "if- else" ©
1 Control Structures (Chapter 3) 3 constructs are essential building blocks for programs Sequences  compound statement Decisions  if, switch, conditional.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
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,
Computer Science Up Down Controls, Decisions and Random Numbers.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
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.
C-Language Lecture By B.S.S.Tejesh, S.Neeraja
Topics The if Statement The if-else Statement Comparing Strings
Simple Control Structures
Chapter 4: Decision Structures and Boolean Logic
Control Structures – Selection
Topics The if Statement The if-else Statement Comparing Strings
If statement.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
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.
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.
CHAPTER 5: Control Flow Tools (if statement)
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:

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