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

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Chapter 4 - Control Statements
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Intro to CS – Honors I Control Flow: Branches GEORGIOS PORTOKALIDIS
Chapter 5– Making Decisions Dr. Jim Burns. In Java, the simplest statement.. Is the if(..) statement if(someVariable==10) System.out.println(“The value.
Control Structures Corresponds with Chapters 3 and 4.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Flow of Control Recitation – 09/(11,12)/2008 CS 180 Department of Computer Science, Purdue University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
COMP 110 If / Else and Case Statements Tabitha Peck M.S. February 4, 2008 MWF 3-3:50 pm Philips
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Flow of Control Part 1: Selection
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
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
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general.
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.
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.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
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.
Chapter 3 1 l Branching l Loops l exit(n) method l Boolean data type and logic expressions Flow of Control – Part 1.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Catie Welsh February 7,  Grades ◦ Lab 2, Project 1 Grades are now on Blackboard 2.
COMP Loop Statements Yi Hong May 21, 2015.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Flow of Control Chapter 3. JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Branching statements.
Chapter 3 Control Statements
Selection (also known as Branching) Jumail Bin Taliba by
Decisions Chapter 4.
Flow of Control.
Chapter 3 Branching Statements
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
CS2011 Introduction to Programming I Selections (II)
SELECTIONS STATEMENTS
The Java switch Statement
Flow of Control Chapter 3.
Announcements Program 1 due noon Lab 1 due noon
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
CSS 161: Fundamentals of Computing
Presentation transcript:

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

Review  if … else … Q1: Write a program that Reads an integer from user Prints “Even” if the integer is even Otherwise, prints “Odd”  Boolean expression & Comparison Q2: How to compare values of primitive types? How about objects? Q3: Write the boolean expression for testing a leap year A leap year is divisible by 4, but not by 100 (except if divisible by 400)

Today  More if / else statements  The switch statements

Gotcha (Syntax)  if (boolean expression);  if (boolean expression) { …. } else { …. } DO NOT DO THIS!!!!!!! // NO boolean expression here

Tracing if / else code  Simple if-else statement: 2-choose-1  Nested if-else statement: N-choose-1 Translate to multiple 2-choose-1 operations  Example: Write a program that takes an input, your year in college (as an integer), and outputs your year as freshman, sophomore, junior, senior, or super senior

Flow Chart Which year? 1 Prompt user for year freshman sophomore 23 junior 4 senior Next step 5 super senior

With Nested if / else if (year == 1) System.out.println(“freshman”); else if (year == 2) System.out.println(“sophomore”); else if (year == 3) System.out.println(“junior”); else if (year == 4) System.out.println(“senior”); else if (year == 5) System.out.println(“super senior”); else System.out.println(“unknown”); Prompt user for year ==1 ? freshman sophomore junior ==2 ? ==3 ? senior ==4 ? ==5 ? super senior unknown

Switch Statement switch(year) { case 1: System.out.println(“freshman”); break; case 2: System.out.println(“sophomore”); break; case 3: System.out.println(“junior”); break; case 4: System.out.println(“senior”); break; case 5: System.out.println(“super senior”); break; default: System.out.println(“unknown”); break; } Controlling expression Case labels Break statements Default case: all other values

Switch Statement Syntax switch (Controlling_Expression) { case Case_label: statements; break; case Case_label: statements; break; default: statements; break; }  Only int, char, enum can be used in the controlling expression  Case labels must be of same type as controlling expression  The break statement ends the switch statement, go to the next step outside the braces in the code  The default case is optional String has been supported in JDK 7

 Write a switch statement that takes as the controlling expression the number of siblings a person has (as an int) and outputs an appropriate messages as follows: Practice with Switch Statements Number of SiblingsMessage 0An only child 1Just one you say 2Two siblings! 3Big Family! 4 or moreI don’t believe you

switch (numOfSiblings) { case 0: System.out.print(“An only child”); break; case 1: System.out.print(“Just one you say”); break; case 2: System.out.print(“Two siblings!”); break; case 3: System.out.print(“Big family!”); break; default: System.out.print(“I don’t believe you”); break; } Switch Statements 11

Enumerations  Lists the values that a variable can have E.g.: rate movies as either excellent, average, or bad enum MovieRating {EXCELLENT, AVERAGE, BAD} MovieRating rating; rating = MovieRating.AVERAGE; Provides a way to restrict the values of a variable Actually a class, typically define it within another class, but always outside of method definitions. more discussion in Chapter 6

An Example switch (rating) { case EXCELLENT: System.out.print(“You must see this movie!”); break; case AVERAGE: System.out.print(“This movie is OK, but not great.”); break; case BAD: System.out.print(“Skip it!”); break; default: System.out.print(“Something is wrong.”); break; }

 Avoid Logical Errors in the Multibranch if-else Statement

Order of Boolean Expressions  What’s the problem with the code below? int n; if (n % 2 == 0) { System.out.println(“Multiple of 2”); } else if (n % 3 == 0) { System.out.println(“Multiple of 3”); } else if (n % 4 == 0) { System.out.println(“Multiple of 4”); }  Ordering is important when boolean expressions are not mutually exclusive

Parallel & Mutually Exclusive Choices  If the choices are mutually exclusive, we can write them as a list of if-only statements  What’s the problem of doing this? if (year==1) { System.out.println(“Freshman”); } if (year==2) { System.out.println(“Sophomore”); } if (year==3) { System.out.println(“Junior”); }

An Example  Determine the number of days in each month of a year if (month == 4 || month == 6 || month == 9 || month == 11) { maxDay = 30; } else if (month == 2) { boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); if (isLeapYear) { maxDay = 29; } else { maxDay = 28; } } else { maxDay = 31; }

The Conditional Operator  Short-cut for if-else statements with return value boolean_expression ? value1 : value2 ; If true, return value1; otherwise, return value2 Example: if (n1 > n2) max = n1; else max = n2; can be written as max = (n1 > n2) ? n1 : n2;  The ? and : together are call the conditional operator or ternary operator.

Some Tips  Indentation in code makes it easy to read Corresponds to “level” in code logic  Eclipse can automatically fix indentation Try Source  Correct Indentation or Format

Applications and Applets  We will cover two kinds of Java programs: applications and applets  Applications are regular programs, run on your computer  Applets have graphical features, run within a Web browser

Next Class  Lab 2 & 3  About String, Java applets (Chapter 1.4), flow chart and if-else  Bring your laptop and textbook