Flow of Control. Chapter 3

Similar documents
Flow of Control. Chapter 3

Flow of Control. Chapter 3. Chapter 3 1

CONDITIONAL EXECUTION: PART 2

Flow of Control. Chapter 3

Flow of Control. Chapter 3 Part 3 The Switch Statement

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

Chapter 3. Flow of Control. Branching Loops exit(n) method Boolean data type and expressions

Flow of Control. Branching Loops exit(n) method Boolean data type and expressions

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

Chapter 2: Using Data

Control Structures in Java if-else and switch

More about JOptionPane Dialog Boxes

Operators. Java operators are classified into three categories:

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Control Structures in Java if-else and switch

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

Java Basic Programming Constructs

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Handout 4 Conditionals. Boolean Expressions.

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

Decision Making in C

CS313D: ADVANCED PROGRAMMING LANGUAGE

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CS 106 Introduction to Computer Science I

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

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

CS111: PROGRAMMING LANGUAGE II

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

CT 229 Java Syntax Continued

Software Design & Programming I

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

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

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

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Flow Control. CSC215 Lecture

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

CSC 1214: Object-Oriented Programming

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

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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

Review of the C Programming Language for Principles of Operating Systems

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

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

8. Decision-Making Statements. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Chapter 3 Selection Statements

Chapter 7. Expressions and Assignment Statements ISBN

If Control Construct

Introduction. C provides two styles of flow control:

Object-Oriented Programming in Java

Chapter 3. Selections

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

More Programming Constructs -- Introduction

PROGRAMMING FUNDAMENTALS

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

Review of the C Programming Language

Selection and Repetition Revisited

Selection as a Control Structure

Expressions & Assignment Statements

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

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

Chapter 02: Using Data

Java is an objet-oriented programming language providing features that support

CSC Java Programming, Fall Java Data Types and Control Constructs

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

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Chapter 2: Using Data

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

Topics. Chapter 5. Equality Operators

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Lecture 3 Tao Wang 1

Flow of Control. 3.1 Branching Mechanism Boolean Expressions Loops Debugging 144

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

Chapter 3. More Flow of Control

Introduction to Object-Oriented Programming

In this chapter, you will:

Week 6: Review. Java is Case Sensitive

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

Full file at

Object Oriented Programming with Java

1007 Imperative Programming Part II

Introduction to Programming Using Java (98-388)

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

Programming with Java

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS111: PROGRAMMING LANGUAGE II

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Flow of Control. Chapter

Chapter 7. Expressions and Assignment Statements

Chapter 3: Operators, Expressions and Type Conversion

Selection and Repetition

Java Notes. 10th ICSE. Saravanan Ganesh

CMPT 125: Lecture 4 Conditionals and Loops

Transcription:

Flow of Control Chapter 3

The Conditional Operator if (n1 > n2) else max = n1; max = n2; can be written as max = (n1 > n2)? n1 : n2; The? and : together are call the conditional operator or ternary operator.

The Conditional Operator The conditional operator is useful with print and println statements. System.out.print("You worked " + ((hours > 1)? "hours" ; "hour"));

The exit Method Sometimes a situation arises that makes continuing the program pointless. A program can be terminated normally by System.exit(0).

The exit Method Example if (numberofwinners == 0) { } System.out.println ("Error: Dividing by zero."); else { } System.exit (0); oneshare = payoff / numberofwinners; System.out.println ("Each winner will receive $" + oneshare);

A Dialog Box for a Yes-or-No Question Used to present the user with a yes/no question The window contains The question text Two buttons labeled yes and no.

A Dialog Box for a Yes-or-No Question Example int answer = JOptionPane.showConfirmDialog(null, "End program?", "Click Yes or No:", JOptionPane.YES_NO_OPTION); if (answer == JOptionPane.YES_OPTION) System.exit(0); else if (answer == JOptionPane.NO_OPTION) System.out.println("One more time");

A Dialog Box for a Yes-or-No Question Figure 3.11 A Yes-or No-Dialog Box

The Type boolean The type boolean is a primitive type with only two values: true and false. Boolean variables can make programs more readable. if (systemsareok) instead of if((temperature <= 100) && (thrust >= 12000) && (cabinpressure > 30) && )

Boolean Expressions and Variables Variables, constants, and expressions of type boolean all evaluate to either true or false. A boolean variable can be given the value of a boolean expression by using an assignment operator. boolean ispositive = (number > 0);... if (ispositive)...

Naming Boolean Variables Choose names such as ispositive or systemsareok. Avoid names such as numbersign or systemstatus.

Precedence Rules Parentheses should be used to indicate the order of operations. When parentheses are omitted, the order of operation is determined by precedence rules.

Precedence Rules Operations with higher precedence are performed before operations with lower precedence. Operations with equal precedence are done leftto-right (except for unary operations which are done right-to-left).

Precedence Rules Figure 3.9

Precedence Rules In what order are the operations performed? score < min/2-10 score > 90

Precedence Rules In what order are the operations performed? score < min/2-10 score > 90 score < (min/2) - 10 score > 90 score < ((min/2) - 10) score > 90 (score < ((min/2) - 10)) score > 90 (score < ((min/2) - 10)) (score > 90)

Short-circuit Evaluation Sometimes only part of a boolean expression needs to be evaluated to determine the value of the entire expression. If the first operand associated with an is true, the expression is true. If the first operand associated with an && is false, the expression is false. This is called short-circuit or lazy evaluation.

Short-circuit Evaluation Short-circuit evaluation is not only efficient, sometimes it is essential! A run-time error can result, for example, from an attempt to divide by zero. if ((number!= 0) && (sum/number > 5)) Complete evaluation can be achieved by substituting & for && or for.

The switch Statement The switch statement is a multiway branch that makes a decision based on an integral (integer or character) expression. Java 7 allows String expressions The switch statement begins with the keyword switch followed by an integral expression in parentheses and called the controlling expression.

The switch Statement A list of cases follows, enclosed in braces. Each case consists of the keyword case followed by A constant called the case label A colon A list of statements. The list is searched for a case label matching the controlling expression.

The switch Statement The action associated with a matching case label is executed. If no match is found, the case labeled default is executed. The default case is optional, but recommended, even if it simply prints a message. Repeated case labels are not allowed.

The switch Statement Syntax switch (Controlling_Expression) { case Case_Label: Statement(s); break; case Case_Label: default: }

The switch Statement View sample program Listing 3.5 class MultipleBirths Sample screen output

The switch Statement 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.

Enumerations Consider a need to restrict contents of a variable to certain values An enumeration lists the values a variable can have Example enum MovieRating {E, A, B} MovieRating rating; rating = MovieRating.A;

Enumerations Now possible to use in a switch statement

Enumerations An even better choice of descriptive identifiers for the constants enum MovieRating {EXCELLENT, AVERAGE, BAD} rating = MovieRating.AVERAGE; case EXCELLENT:...

Summary You have learned about Java branching statements. You have learned about the type boolean. You have learned to use the JOptionPane yes/no window.