Programming 2 Decision-Making.

Slides:



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

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Chapter 5– Making Decisions Dr. Jim Burns. In Java, the simplest statement.. Is the if(..) statement if(someVariable==10) System.out.println(“The value.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Chapter 2 Review Questions
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Introduction to Computer Programming Decisions If/Else Booleans.
Loops – While, Do, For Repetition Statements Introduction to Arrays
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
C++ for Engineers and Scientists Third Edition
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Computer Science Selection Structures.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 4 Introduction to Control Statements Section 1 - Additional Java Operators Section 2 - If Statements Section 3 - If-Else Statements Section 4.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
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.
1 2. Program Construction in Java. 2.4 Selection (decisions)
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
1 if / else statements. 2 Conditionals “If you eat your vegetables, then you can have dessert.” “If you do your homework, then you may go outside to play,
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Decisions, Decisions, Decisions Conditional Statements In Java.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Decision Statements, Short- Circuit Evaluation, Errors.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Midterm preview.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Java Programming Fifth Edition
Selections Java.
Introduction to Computer Science / Procedural – 67130
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 4 – Control Structures Part 1
SELECTION STATEMENTS (1)
Control Statement Examples
Making Decisions in a Program
Chapter 3:Decision Structures
CMSC 202 Java Primer 2.
Recap Week 2 and 3.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 2 Programming Basics.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
PROGRAM FLOWCHART Selection Statements.
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.
Unit 3: Variables in Java
Presentation transcript:

Programming 2 Decision-Making

Planning Decision-Making Logic Pseudocode & flowcharts are very important when it comes to the planning of decision-making logic Don’t just sit down and start coding! Even if it seems simple, begin the practice of designing before coding

The ‘if’ Structure { System.out.println(“Perfect”)’ } Int quizScore =9; //if the following statement is TRUE, do the //stuff in curly braces If (quizScore ==10) { System.out.println(“Perfect”)’ }

Comparison Operators Equal to (==) Not equal to (!=) Less than (<) Greater than (>) Less than or equal to (<=) Greater than or equal to (>=) The ‘answer’ is always going to be true or false

Equal To / Not Equal To If (quizScore ==10) { //do stuff } TRUE if quizScore is equal to 10 FALSE if quizScore is anything but 10 If (quizScore !=10) TRUE if quizScore is anything but 10 FALSE if quizScore is equal to 10

Greater Than / Less Than If (quizScore >10) { //do stuff } TRUE if quizScore is 11 or above FALSE if quizScore is 10 or less If (quizScore <10) TRUE if quizScore is 9 or less FALSE if quizScore is 10 or greater

No semi—colon at end of ‘if’ Int quizScore = 9; If (quizScore == 10); { system.out.println(“Perfect”); }

Greater Than or Equal to Less Than or Equal to If (quizScore >= 10) { //do stuff } TRUE if quizScore is 10 or above FALSE if quizScore is 9 or less If (quizScore <= 10) TRUE if quizScore is 10 or less FALSE if quizScore is 11 or greater

Make sure Not to Use Assignment Operator Instead of Equivalency Int quizScore = 9; If (quizScore ==10) //correct! { System.out.println (“Perfect”); } ********************************** If (quizScore = 10) //Not Correct!

The ‘if/else’ Structure Int quizScore = 9; //if the statement below is TRUE, do the stuff in curlys If (quizScore ==10) //correct! { System.out.println (“Perfect”); } //if the ‘if’ above is FALSE, do THIS stuff in curlys else System.out.println (“ Not Perfect”);

Multiple statements in ‘if/else’

Variable Scope public Class CompareNumbers { Any variable declared inside a block is local to that block The System.out.println below will not work public Class CompareNumbers { publicstatic void main (String [] args) int a =1; int b = 2; if (a==b) sum = a + b; } System.out.println(sum); A Block is lines of code between curly braces; To correct the problem, declare the variable sum outside of the block See next slide

public Class CompareNumbers { publicstatic void main (String [] args) int a =1; int b = 2; int sum; if (a==b) sum = a + b; } System.out.println(sum); Sum will print in this example because the variable was declared outside of the block

Constants Constant variables are variables whose values CANNOT change public class CalcInterest { public static void main (String[] args) final double RATE = .008; double savingsTotal = 2000.98 double interestEarned = RATE * savingsTotal; System.out.println(interestEarned); RATE = .007; // can’t do this; cannot reassign }

Nested If/Else Statements

Logical AND & OR operators OR (||) – piping symbol Shift of \ Public class CalcSales { public static void main (String[] args) Double salesAmount = 2500 Int bonus; If( (salesAmount > 1000) && (salesAmount < 5000) ) bonus = 400; }

OR Operators Public class CalcSales { public static void main (String[] args) Double salesAmount = 2500 Int hoursWorked = 3200 Int bonus; If( (salesAmount > 5000) || (hoursWorked > 3000) ) bonus = 400; }

Combining AND and OR Public class CalcSales { public static void main (String[] args) Double salesAmount = 2500; Int hoursWorked = 3200; int yearsOfService = 20; Int bonus; If(( (salesAmount > 5000) || (hoursWorked > 3000) ) && (yearsOfService >=20 )) bonus = 400; }

Switch Statements

Switch Statements - continued

Boolean Values Public class CalcSales { public static void main (String[] args) boolean isMale = true; if (isMale) // do stuff } else // do other stuff

Boolean Values Public class CalcSales { public static void main (String[] args) boolean wonGame = false; Int myScore = 11; if (myScore >10) wonGame = true; } if (wonGame) System.out.println(“Congrats!”);

Code to get user input

Code to get user input Import java.swing.JOptionPane; Public class HelloNameDialog { Public static void main (String[] args) String result; Result = JOptionPane.showInputDialog(null, “What is your name?”); JOptionPane.ShowMessageDialog(null, “hello, “ + result + “!”); }

Code to convert input from string to integer