COMP 110-001 Flow of Control: Branching 1 Yi Hong May 19, 2015.

Slides:



Advertisements
Similar presentations
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Introduction to Computer Programming Decisions If/Else Booleans.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
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;
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Computer Science Selection Structures.
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 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
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.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
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.
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.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
COMP Loop Statements Yi Hong May 21, 2015.
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.
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 4: Control Structures I
Chapter 4: Control Structures I
Chapter 3 Edited by JJ Shepherd
Flow of Control.
Chapter 3 Branching Statements
Building Java Programs Chapter 4
Chapter 5: Control Structures II
Building Java Programs
SELECTION STATEMENTS (1)
Building Java Programs
Chapter 4: Control Structures I
Building Java Programs
AP Java Review If else.
Building Java Programs
Introduction to Computer Programming
Self study.
Building Java Programs
Control Structure Chapter 3.
Building Java Programs
AP Java Review If else.
Building Java Programs
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Building Java Programs
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.
Building Java Programs
Presentation transcript:

COMP Flow of Control: Branching 1 Yi Hong May 19, 2015

Today  The if-else statement  Boolean expressions

Flow of Control  The order in which a program performs actions Continuation (unconditional) Until now, actions are taken sequentially More complicated flow of control: A branching statement chooses an action from a list of two or more possible actions A loop statement repeats an action again and again until some stopping condition is met

Simple order  Continuation (unconditional)  Perform actions sequentially  A single path in the flow chart Leave Home Check Time Take Bus Reach School

Branching  Choose a path in the flow chart by checking conditions Leave Home Check Time Take Bus Reach School Before 7am? Take Subway YesNo

Branching  Choose a path in the flow chart by checking conditions Leave Home Check Time Take Bus Reach School Before 7am? Take Subway YesNo

Branching  Choose a path in the flow chart by checking conditions Leave Home Check Time Take Bus Reach School Before 7am? Take Subway YesNo

Another Example  In homework 1 Print “day” #days==1 Print “days” Yes No Compute #days Compute #hours Print #days

The if-else Statement  A branching statement that chooses between two possible actions  Syntax if (Boolean_Expression) Statement_1 else statement_2 Example: if (days == 1) system.out.print(“ day”); else System.out.print(“ days”);

The if-else Statement  What if you have multiple statements in each branch? if (Boolean_Expression){ Statement_1.1 Statement_ } else { Statement_2.1 Statement_ }

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"); } } } 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

Boolean Expressions  if (boolean_expression) { statements }  True or false  “atomic” expressions 5 == 3// always false myInt <= 6// depends on the value of myInt // depends on the value of myInt, anotherInt myInt != anotherInt

Review of Boolean Operators  The effect of the boolean operators && (and), || (or), and ! (not) on boolean values

Use && for and  Form a larger boolean expression out of many smaller boolean expressions  Syntax (Sub_Expression_1) && (Sub_Expression_2) && …  Will be true if and only if ALL statements are true

Use || for or  Also form a larger boolean expression out of many boolean expressions  Syntax (Sub_Expression_1) || (Sub_Expression_2) || …  Will be true if ONE expression is true

Example in Homework 1  Check if #days is not 1 days ! = 1 !(days == 1) days 1 !(days >= 1 && days <= 1)

Comparison for Objects  Call object’s method equals() method  E.g.: Compare two strings str1 = str2; ( ✗ assignment statement) str1 == str2; (Do NOT, == tests whether they are stores in the same memory location) str1.equals(str2); (GOOD)

Compare Strings  Syntax String.equals(Other_String) String.equalsIgnoreCase(Other_String)  Example String name1 = “COMP110”; String name 2 = new String(“COMP110”); if (name1.equals(name2)){ System.out.println(“The same”); } else { System.out.println(“Different”); }

If Without Else  You can use just an if statement if (Boolean_Expression) { Statement_1.1 Statement_ } the rest of your code Wake up Check Time Have Breakfast Leave Home Before 7am? Yes No

A Different Implement for Homework 1 Print “day” #days ==1 Print “days” YesNo Print #days dayText= “ day” #days ==1 Yes No Print #days dayText = “ days” Print dayText

A Different Implement for Homework 1 System.out.print( days ); String dayText = “ days”; if (days==1){ dayText = “ day”; } System.out.print( dayText ); dayText= “ day” #days ==1 Yes No Print #days dayText = “ days” Print dayText

Nested if-else Statements  An if-else statement can contain any sort of statements within it  You can nest an if-else statement within another if-else statement if (boolean expression) { if (boolean expression){ stuff goes here } else { more stuff } } else { … } if (boolean expression) { … } else if (boolean expression){ stuff goes here } else { more stuff }

Pseudocode in Flowchart Format 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

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 if (inputInt > 1) System.out.println(”how may I help you"); else System.out.println(“Negative”); } }

Next Class  More if / else statements  The switch statements