COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips 367 1.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

Computer Programming Lab 8.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
COMP 110 More Loops and Strings Tabitha Peck M.S. February 18, 2008 MWF 3-3:50 pm Philips
1 COMP 110 More Arrays Tabitha Peck M.S. April 2, 2008 MWF 3-3:50 pm Philips 367.
COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips
1 COMP 110 Designing and Overloading Methods Tabitha Peck M.S. March 26, 2008 MWF 3-3:50 pm Philips 367.
COMP 110 Designing Programs Tabitha Peck M.S. January 16, 2008 MWF 3-3:50 pm Philips
COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips
COMP 110 If / Else and Case Statements Tabitha Peck M.S. February 4, 2008 MWF 3-3:50 pm Philips
COMP 110 Errors, Strings, and Review Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
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.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
COMP More About Classes Yi Hong May 22, 2015.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Catie Welsh January 12, 2011 MWF 1-1:50 pm Sitterson
COMP Flow of Control: Branching 2 Yi Hong May 19, 2015.
Computer Science Selection Structures.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
COMP Mid-Term Review Yi Hong May 27, 2015.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
CS1101: Programming Methodology Aaron Tan.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
Catie Welsh February 7,  Grades ◦ Lab 2, Project 1 Grades are now on Blackboard 2.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Review if Online Time For loop Quiz on Thursday.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Catie Welsh February 14,  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.
1 Simple Input Interactive Input - The Class Scanner n Command-Line Arguments.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
(Dreaded) Quiz 2 Next Monday.
INTERMEDIATE PROGRAMMING USING JAVA
Computer Programming Methodology Introduction to Java
SELECTION STATEMENTS (1)
BIT115: Introduction to Programming
Michele Weigle - COMP 14 - Spr 04
Java so far Week 7.
Know for Quiz Everything through Last Week and Lab 7
AP Java Review If else.
Programming 2 Decision-Making.
Self study.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
AP Java Review If else.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Building Java Programs
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Presentation transcript:

COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips 367 1

Announcements Program 1 Due Today Hand in Pseudocode after class Program 2 Assigned Today Lab 2 Due Friday 2

Questions? 3

Today in COMP 110 Formatting decimals Review Worksheet If/Else statements Boolean Expressions 4

Formatting Decimals import java.text.*; DecimalFormat df = new DecimalFormat("0.00"); df.format(myVariable); Example code on class website 5

Review Worksheet public class MyProgram { public static void main(String[] args) { String myString = “This is a string”; int len = myString.length(); System.out.print(“the length is “ + len); String shortString = myString.substring(10); } 6

Integer division Double myDouble = (1 / 2) * 5.0; int i = 1 / 2; 0.5 O 7

Classes Suppose that mary is an object of class Person, and suppose that increaseAge is a method of class Person that uses one argument, an integer. Write the invocation of the method increaseAge for the object mary using the argument 5. mary.increaseAge(5); Person mary = new Person; 8

Flow Chart 9

Check time; if (time < 7am) { take bus; } else //time >= 7am { take subway; } Reach school; 10

Java Example import java.util.*; public class FlowChart { public static void main(String[] args) { System.out.println("Give me an integer:"); Scanner keyboard = new Scanner(System.in); int inputInt = keyboard.nextInt(); if( inputInt > 5) { System.out.println("Big number"); } else { System.out.println("Small number"); } } } 11 Start Prompt User for int Is user input greater than 5? Print: “small number” Print: “big number” YES NO

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

Boolean Expressions True of False Example expressions 5 == 3; Variable <= 6; myInt != temp; if (boolean expression) {statements } 13

&& (and) What if you need multiple expressions to be true (expression) && (expression) && … Expressions go in ( ) Will only be true if ALL statements are true 14

|| (or) What if you need ONE expression to be true out of many expressions (expression) || (expression) || … Expressions go in ( ) Will be true if ONE expression is true 15

Gotcha == var1 = var2 (assignment statement) Error!!!!!!! var1 == var2 (boolean expression) Do NOT use == to compare Strings string1 == string2 //BAD string1.equals(string2); //GOOD 16

If Without Else You can use just an if statement if (boolean expression) { (statements) } the rest of your code 17

Nested If Else if (boolean expression) { if (boolean expression) { stuff goes here } else { more stuff } } else 18

Start Prompt User for int What is the integer? Print: “hello” Print: “how may I help you” inputInt > 1 inputInt == 0 Print: “how are you” inputInt == 1 19

import java.util.*; public class FlowChart { public static void main(String[] args) { System.out.println("Give me an integer:"); Scanner keyboard = new Scanner(System.in); int inputInt = keyboard.nextInt(); if ( inputInt == 0) System.out.println(”hello"); else if ( inputInt == 1) System.out.println(”how are you"); else System.out.println(”how may I help you"); } } 20

Friday Recitation Bring Laptop Bring Book 21