Flow of Control. Chapter 3 Part 3 The Switch Statement

Similar documents
Flow of Control. Chapter 3

Flow of Control. Chapter 3

Flow of Control. Chapter 3

CONDITIONAL EXECUTION: PART 2

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

In this chapter, you will:

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS111: PROGRAMMING LANGUAGE II

Flow Control. Key Notion. Statement Categories. 28-Oct-10

Flow Control. Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya

Following is the general form of a typical decision making structure found in most of the programming languages:

CS313D: ADVANCED PROGRAMMING LANGUAGE

In this lab, you will learn more about selection statements. You will get familiar to

Introduction to Programming Using Java (98-388)

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Operators. Java operators are classified into three categories:

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

++x vs. x++ We will use these notations very often.

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

CT 229 Java Syntax Continued

Scanner Objects. Zheng-Liang Lu Java Programming 82 / 133

Flow of Control. Chapter 3. Chapter 3 1

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

The Arithmetic Operators

Oct Decision Structures cont d

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Pace University. Fundamental Concepts of CS121 1

4. Java language basics: Function. Minhaeng Lee

Java Basic Programming Constructs

C++ Programming: From Problem Analysis to Program Design, Third Edition

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

Programming with Java

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137

Arithmetic Compound Assignment Operators

Chapter 3. Selections

CS111: PROGRAMMING LANGUAGE II

Flow Control. CSC215 Lecture

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

APCS Semester #1 Final Exam Practice Problems

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

CS 139 Practice Midterm Questions #2

Handout 4 Conditionals. Boolean Expressions.

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Introduction to Computer Science Unit 2. Notes

Control Structures. CIS 118 Intro to LINUX

Java Notes. 10th ICSE. Saravanan Ganesh

PROGRAMMING FUNDAMENTALS

CMPT 125: Lecture 4 Conditionals and Loops

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 4 Control Structures

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

Review of the C Programming Language for Principles of Operating Systems

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

H212 Introduction to Software Systems Honors

IEEE Floating-Point Representation 1

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Lesson 7 Part 2 Flags

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

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

Computer Components. Software{ User Programs. Operating System. Hardware

AP CS Unit 3: Control Structures Notes

Object Oriented Programming with Java

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Expressions & Flow Control

Eng. Mohammed S. Abdualal

CSS 161 Fundamentals of Compu3ng. Flow control (2) October 10, Instructor: Uma Murthy

Computer Components. Software{ User Programs. Operating System. Hardware

Boolean Algebra Boolean Algebra

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CS159. Nathan Sprague

LAMBDA EXPRESSIONS. Summer 2018

Example. Generating random numbers. Write a program which generates 2 random integers and asks the user to answer the math expression.

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

If Control Construct

More Programming Constructs -- Introduction

Array. Array Declaration:

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

COMP 202 Java in one week

2.2 - Making Decisions

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Example. Write a program which generates 2 random integers and asks the user to answer the math expression.

(Not Quite) Minijava

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Conditional Programming

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Transcription:

Flow of Control Chapter 3 Part 3 The Switch Statement

Agenda Hw 03 comments Review of Ch03 - Parts 1 & 2 Conditional operator I/O of boolean values The switch statement Random numbers Methods with arguments - Chapter 03: Branching Slide # 2

The Conditional Operator 1/2 Used instead of writing a simple if statement if (n1 > n2) else max = n1; max = n2; can be written as max = (n1 > n2)? n1 : n2; Needs three operands; the condition, and two actions for true and false respectively The? and : together are call the conditional operator or ternary operator Chapter 03: Branching - Slide # 3

The Conditional Operator 2/2 The conditional operator is useful with print and println statements Example: System.out.println("You worked " + hours + ((hours > 1)? hours" : hour")); Chapter 03: Branching - Slide # 4

Input and Output of Boolean Values For output, just write the Boolean variable name For input, use the nextboolean()method and save the result into the Boolean variable Example boolean booleanvar = false; System.out.println(booleanVar); System.out.println("Enter a boolean value:"); Scanner keyboard = new Scanner(System.in); booleanvar = keyboard.nextboolean(); System.out.println("You entered " + booleanvar); Chapter 03: Branching - Slide # 5

The switch Statement 1/8 The switch statement is a multiway branch that makes a decision based on an integral (integer or character) expression Java 7 allows String expressions as well Begins with the keyword switch followed by an integral expression in parentheses and called the controlling expression A list of cases follows, enclosed in braces Chapter 03: Branching - Slide # 6

The switch Statement 2/8 Each case consists of the keyword case followed by A constant called the case label A colon (:) A list of statements An optional break statement The list is searched for a case label matching the controlling expression The action associated with a matching case label is executed If you do not add the break statement, control will continue to the next case If no match is found in all case entries, the case labeled default is executed Chapter 03: Branching - Slide # 7

The switch Statement 3/8 The default case is optional, but recommended, even if it simply prints a message Repeated case labels are not allowed Syntax: switch (Controlling_Expression) { case Case_Label: Statement(s); break; case Case_Label: default: } Chapter 03: Branching - Slide # 8

switch Statement Example Chapter 03: Branching - Slide # 9

The switch Statement 4/8 The action for each case typically ends with the word break The optional break statement prevents the consideration of other cases The controlling expression can be anything that evaluates to an integral type Chapter 03: Branching - Slide # 10

The switch Statement 5/8 You can omit the break statement to simplify the statements Example: Checking for upper- and lower-case responses that are the same Chapter 03: Branching - Slide # 11

The switch Statement 6/8 More examples from: http://math.hws.edu/javanotes/c3/s6.html Chapter 03: Branching - Slide # 12

The switch Statement 7/8 More examples from: http://math.hws.edu/javanotes/c3/s6.html Chapter 03: Branching - Slide # 13

The switch Statement 8/8 More examples from: http://math.hws.edu/javanotes/c3/s6.html Using strings Chapter 03: Branching - Slide # 14

Switch Statement Videos thenewboston: Java Programming Tutorial - 12 - Switch Statement (6:46) EJ Media: Java Tutorial for Beginners - 8 - Switch statements (6:12) BrandonioProductions: Learning Java: Part 22: The Switch Statement (17:08) - Chapter 03: Branching Slide # 15

Intro Methods with Arguments You can pass as many arguments to a java method You can return nothing (void) only one result from a method of any data type Videos Learneroo Java Methods and Parameters (2:03) BrandonioProductions Learning Java: Part 5: Custom methods with return statements and parameters (7:49) thenewboston Java Programming Tutorial - 15 - Use Methods with Parameters (6:40) - Chapter 03: Branching Slide # 16

Random class Random Numbers -1/3 Must import Random library Must create an object and apply the methods on that object More flexible Generates different data types Generates uniformly distributed random numbers Math.random() Method Returns a double value in the range 0.0 up to, but not including 1.0 0.0 <= x < 1.0 Needs more work to translate into the desired range You can use multiplication to expand the range Addition to shift the range Casting to convert to integer No need to create an object Random class vs. Math.random() method Stack Overflow - Chapter 03: Branching Slide # 17

Random Numbers -2/3 Examples: generate a double between 0.0 and 10.0 double randdouble = Math.random() * 10; generate a integer between 0 and 10 int randint = Math.round(Math.random() * 10); int randint = (int) (Math.random() * 10); int randint = Random.nextInt(10); Generate a range from 40.0 to 90.0 double randdouble = Math.random() * 50 + 40; Generate a range from 50 to 99 int randint = (int) ( Math.random() * 50) + 50; General format: Return a random number between a and a+b, excluding a+b double randdouble = a + Math.random() * b - Chapter 03: Branching Slide # 18

Some videos: Adam Gaweda: Random Numbers -3/3 Using Java's Math.random() Method (5:12) The Java Hub - Free Tutorials Java for the Absolute Beginner - #14 - Random Numbers (7:08) Dice uses loops & Random class TheNewBoston: Java Programming Tutorial - 26 - Random Number Generator (5:14) Dice uses loops & Random class BrandonioProductions: Learning Java: Part 8: Generating Random Numbers (3:55) OnliveGamer Java Tutorial 8 - Random Number Generator (7:56) TheKaleb32 How to Generate Random Numbers in Java (10:35) - Chapter 03: Branching Slide # 19

Summary You have learned about Conditional operator The type boolean I/O of boolean values Java switch statement Random class Chapter 03: Branching - Slide # 20