COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

Chapter 4 - Control Statements
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
True or false A variable of type char can hold the value 301. ( F )
Flow of Control Recitation – 09/(11,12)/2008 CS 180 Department of Computer Science, Purdue University.
The Type boolean. Boolean Expressions and Boolean Variables  The type boolean is a primitive type  Variables of type boolean and Boolean expressions.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chapter 4 Making Decisions
CS 180 Recitation 9/20/07 - 9/21/07. Announcements Exam 1 is Tuesday, September 25 th  Rooms: Sec WTHR 104 Sec MATH 175  Time: 7:00.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
COMP Flow of Control: Branching 2 Yi Hong May 19, 2015.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 3 Edited by JJ Shepherd
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Module 3. Objectives Use Java branching statements Compare values of primitive types Compare objects such as strings Use the primitive.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Flow of Control Chapter 3 Flow of control Branching Loops
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
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 Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
COMP Loop Statements Yi Hong May 21, 2015.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Control Statements: Part1  if, if…else, switch 1.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 3Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 3 l Branching l Loops l exit(n) method l Boolean data type.
Lecture 3 Selection Statements
Chapter 3 Selection Statements
Chapter 3 Control Statements
CS0007: Introduction to Computer Programming
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Lecture Notes – Week 2 Lecture-2
Boolean Expressions to Make Comparisons
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Primitive Types and Expressions
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Announcements/Reminders
Announcements Program 1 due noon Lab 1 due noon
CSS 161: Fundamentals of Computing
Selection Statements August 22, 2019 ICS102: The course.
Presentation transcript:

COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring Announcements Program 1 due Wed by midnight Couple things

COMP 110: Spring Questions?

COMP 110: Spring Today in COMP 110 The type boolean switch statements Enumerations

COMP 110: Spring Review If-Statements Used to make decisions or check conditions in a program E.g. check the value of a variable Syntax if(Boolean_Expression) Statement_1 else Statement_2 If Boolean_Expression is true, Statement_1 is executed; otherwise Statement_2 is executed

COMP 110: Spring If Statement Exercise What is the output? if(x > 5) { System.out.print(‘A’); if(x < 10) System.out.print(‘B’); } else System.out.print(‘C’); xOutput 4C 5C 6AB 9 10A 11A

COMP 110: Spring Boolean Expressions An expression that evaluates to either true or false Consider ((x > 10) || (x < 100)) Why is this probably not what the programmer intended? It’s true for any x Consider ((2 < 5) && (x < 100)) Why is this probably not what the programmer intended? It’s the same as (x < 100)

COMP 110: Spring The Type boolean boolean is a primitive type in Java Stores the value true or false We can declare variables of type boolean just like we declare an int, double, etc. boolean ready; boolean error;

COMP 110: Spring Booleans Using booleans can make your programs easier to understand //a bit difficult to read if(temp = && cabinPressure > 30) System.out.println(“Launch”); else System.out.println(“Abort”);

COMP 110: Spring Booleans Booleans can be used inside boolean expressions boolean systemsGo = temp = && cabinPressure > 30; //much easier to read if(systemsGo) System.out.println(“Launch”); else System.out.println(“Abort”);

COMP 110: Spring True and False The words true and false are also reserved words in java We can use them to initialize boolean variables boolean ready = false; boolean initialized = true;

COMP 110: Spring Booleans There’s no need to write if(systemsGo == true) System.out.println(“Launch”); The more concise and equivalent way is if(systemsGo) System.out.println(“Launch”);

COMP 110: Spring Naming Booleans Choose names for boolean variables that sound true when the value of the variable is true boolean ready; //are we ready? boolean readingInput; //are we reading input? boolean errorEncountered; //have we encountered //an error?

COMP 110: Spring Precedence Java uses precedence rules when evaluating boolean expressions Example score >= 80 && score < 90 The expressions to the left and right of && are evaluated first

COMP 110: Spring Operator Precedence Highest Precedence First: the unary operators +, -, ++, --, ! Second: the binary operators *, /, % Third: the binary operators +, - Fourth: the boolean operators, >=, <= Fifth: the boolean operators ==, != Sixth: the boolean operator && Seventh: the boolean operator || Lowest Precedence

COMP 110: Spring Boolean Precedence Example 4 9 false || -8 > 9 false || false false

COMP 110: Spring Style It’s usually best to indicate precedence in boolean expressions explicitly with parentheses (score 90)

COMP 110: Spring Short-Circuit Evaluation In some cases, the result of a boolean expression can be determined before all subparts of the expression are evaluated Example true || (x >= 60) This expression is true regardless of the value of x

COMP 110: Spring Short-Circuit Evaluation Java uses what’s called short-circuit evaluation when evaluating boolean expressions If at any point in the evaluation of a boolean expression the outcome is determined, any remaining subparts are not evaluated

COMP 110: Spring Short-Circuit Evaluation Example We’re computing an average homework score Print “Good work!” if the average is above 60 if(numAssignments > 0 && ((total / numAssignments) > 60) System.out.println(“Good work!”); Without short-circuit evaluation, we would divide by zero if numAssignments == 0

COMP 110: Spring Reading in Booleans We can read in booleans from the keyboard just like any other variable boolean bVar; Scanner keyboard = new Scanner(System.in); bVar = keyboard.nextBoolean();

COMP 110: Spring Switch Statements If-statements with many branches can be difficult to read The switch statement can be used as an alternative to a multi-branch if- statement in certain cases

COMP 110: Spring Switch Statement A switch statement begins like this switch(Controlling_Expression) { … } Controlling_Expression must have type int or char

COMP 110: Spring Switch Statements Inside the body of a switch statement, a number of case labels will appear The different cases are separated with a break statement case Case_Label_1: Statements_1 break; … case Case_Label_n: Statements_n break;

COMP 110: Spring Switch Statement Example int year; … switch(year) { case 1: System.out.println(“You are a freshman”); break; case 2: System.out.println(“You are a sophomore”); break; case 3: System.out.println(“You are a junior”); break; case 4: System.out.println(“You are a senior”); break; default: System.out.println(“This is a default case”); break; }

COMP 110: Spring Switch Statement Example int year = 1; … switch(year) { case 1: System.out.println(“You are a freshman”); break; case 2: System.out.println(“You are a sophomore”); break; case 3: System.out.println(“You are a junior”); break; case 4: System.out.println(“You are a senior”); break; default: System.out.println(“This is a default case”); break; }

COMP 110: Spring Switch Statement Example int year = 3; … switch(year) { case 1: System.out.println(“You are a freshman”); break; case 2: System.out.println(“You are a sophomore”); break; case 3: System.out.println(“You are a junior”); break; case 4: System.out.println(“You are a senior”); break; default: System.out.println(“This is a default case”); break; }

COMP 110: Spring Switch Statement Syntax switch(Controlling_Expression) { case Case_Label: Statements break; case Case_Label : Statements break; … default: Statements break; }

COMP 110: Spring Switch Statements If no break statement is specified, for a case, execution will continue down to the next case int n; … switch(n) { case 1: System.out.println(“one”); case 2: System.out.println(“two”); break; } If n == 1, this code prints onetwo

COMP 110: Spring Switch Statement Example char input; … switch(input) { case 'y': case 'Y': System.out.println(“You entered yes”); break; case 'n': case 'N': System.out.println(“You entered no”); break; default: System.out.println(“Invalid input”); break; }

COMP 110: Spring Default Case When using a switch statement you should always provide a default case This catches any conditions you may not have checked for, such as errors default: System.out.println(“This is a default case”); break;

COMP 110: Spring Conversion to If The previous example can also be written equivalently with an if-statement if(input == 'y' || input == 'Y') System.out.println(“You entered yes”); else if(input == 'n' || input == 'N') System.out.println(“You entered no”); else System.out.println(“Invalid input”);

COMP 110: Spring If/Switch How to know whether to use a switch or an if- statement? Switch statement Can only be used with type char or int When you want to choose between many, specific values such as 5,6, 'y' etc. If statement Can only be used with boolean expressions When the number of choices is relatively small When you want to check a range of possibilities, e.g. x > 5, y <= 1000

COMP 110: Spring Enumerations Suppose you wanted to write a computer program that stores different flavors of ice cream Vanilla, Chocolate, Strawberry, etc. How would we store them in a computer program?

COMP 110: Spring Enumerations We can give the flavors an underlying numeric representation Vanilla = 0, Chocolate = 1, Strawberry = 2 We could declare a variable integer to store our flavor, but this is error-prone int flavor = 0; //vanilla flavor = 1; //chocolate

COMP 110: Spring Enumerations An enumeration allows us to give unique numeric values to a list of items enum Flavor {Vanilla, Chocolate, Strawberry} This statement assigns a unique numeric value to each of {Vanilla, Chocolate, Strawberry}

COMP 110: Spring Switch/Enum Example enum Flavor {Vanilla, Chocolate, Strawberry} Flavor flavor; //declare a variable of type Flavor … switch(flavor) { case Vanilla: System.out.println(“That’s Vanilla!”); break; case Chocolate: System.out.println(“That’s Chocolate!”); break; case Strawberry: System.out.println(“That’s Strawberry!”); break; default: System.out.println(“I don’t recognize that flavor”); break; }

COMP 110: Spring Programming Demo Write a program that takes two numbers as input from the user The user should then be able to choose from among the following options Add the two numbers Subtract the two numbers Multiply the two numbers Divide the two numbers

COMP 110: Spring Programming Demo Pseudocode Ask user to input two numbers Provide the user with a list of the options Perform the operation selected by the user Output the result

COMP 110: Spring Programming Demo Programming

COMP 110: Spring Wednesday Loops Keep up with the reading We’ll start Ch 4 on Wed