Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Similar documents
Chapter 4: Control Structures I

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, Third Edition

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

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

Chapter 4: Control Structures I (Selection)

In this chapter, you will:

Chapter 4 - Notes Control Structures I (Selection)

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

Topics. Chapter 5. Equality Operators

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

Chapter 3 Selection Statements

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Eng. Mohammed S. Abdualal

What methods does the String class provide for ignoring case sensitive situations?

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

UEE1302(1102) F10: Introduction to Computers and Programming

Course PJL. Arithmetic Operations

Java Basic Programming Constructs

CMPT 125: Lecture 4 Conditionals and Loops

Checking Multiple Conditions

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

CS 106 Introduction to Computer Science I

Selection as a Control Structure

3chapter C ONTROL S TATEMENTS. Objectives

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

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

CT 229 Java Syntax Continued

Java Coding 2. Decisions, decisions!

Getting started with Java

Conditional Programming

Introduction to Programming Using Java (98-388)

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Handout 4 Conditionals. Boolean Expressions.

Conditions, logical expressions, and selection. Introduction to control structures

CT 229 Fundamentals of Java Syntax

Week 4 Selection Structures. UniMAP Sem II-11/12 DKT121 Basic Computer Programming 1

COMPUTER APPLICATIONS

Flow of Control. Chapter 3

Important Java terminology

Data Types Reference Types

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

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

Flow of Control. Chapter 3

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CP122 Computer Science I. Binary, Strings, Conditionals, User Input

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

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Control Structures (Deitel chapter 4,5)

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.

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

CS Week 5. Jim Williams, PhD

Selection and Repetition Revisited

Full file at

Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Session 3. Basic Java Data Types, Control Structures. 8 primitive or built-in data types

Prof. Navrati Saxena TA: Rochak Sachan

Lecture 5 Tao Wang 1

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

Decisions in Java IF Statements

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

( &% class MyClass { }

Midterm Examination (MTA)

Introduction to Java

You ll be reading more about tools for branching in Sections 3.4, and We are skipping Sections 3.5, 3.11 and 3.13-end of chapter.

H212 Introduction to Software Systems Honors

CS 11 java track: lecture 1

Chapter 2 Primitive Data Types and Operations

JAVA OPERATORS GENERAL

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

1007 Imperative Programming Part II

Object-Oriented Programming

Chapter Goals. 3.1 The if Statement. Contents 1/30/2013 DECISIONS

Course Outline. Introduction to java

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

Lecture Set 4: More About Methods and More About Operators

Chapter 2: Using Data

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

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

Array. Prepared By - Rifat Shahriyar

PROGRAMMING FUNDAMENTALS

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Operators and Expressions

BRANCHING if-else statements

Branching. Chapter 5 11/14/16 & 11/15/16

Conditional Execution

SECONDARY SCHOOL, L-IMRIEĦEL HALF YEARLY EXAMINATIONS 2016/2017

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

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Chapter 4: Making Decisions

LECTURE 04 MAKING DECISIONS

What did we talk about last time? Examples switch statements

CIS 110: Introduction to Computer Programming

Transcription:

Chapter 4: Control Structures I Java Programming: Program Design Including Data Structures Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean) expressions Learn how to use the selection control structures if, if else, and switch in a program Java Programming: Program Design Including Data Structures 2 Control Structures Three methods of processing a program: In sequence Branching Looping Branch: Altering the flow of program execution by making a selection or choice Loop: Altering the flow of program execution by repeating statements Java Programming: Program Design Including Data Structures 3 1

Control Structures (continued) Java Programming: Program Design Including Data Structures 4 Relational Operators Relational operator: Allows you to make comparisons in a program Binary operator Condition is represented by a logical expression in Java Logical expression: An expression that has a value of either true or false Java Programming: Program Design Including Data Structures 5 Relational Operators (continued) Java Programming: Program Design Including Data Structures 6 2

Relational Operators and Primitive Data Types Can use with Integral data types Floating-point data types char data type Unicode Collating Sequence Java Programming: Program Design Including Data Structures 7 Relational Operators and Primitive Data Types (continued) Java Programming: Program Design Including Data Structures 8 Comparing Strings class String Method compareto Method equals Given string str1 and str2 an integer < 0 if string str1< str2 str1.compareto(str2) = 0 if string str1is equaltostringstr2 an integer > 0 if string str1 > str2 Java Programming: Program Design Including Data Structures 9 3

Comparing Strings (continued) String str1 = "Hello"; String str2 = "Hi"; String str3 = "Air"; String str4 = "Bill"; String str5 = "Bigger"; Java Programming: Program Design Including Data Structures 10 Comparing Strings (continued) Java Programming: Program Design Including Data Structures 11 Comparing Strings (continued) Java Programming: Program Design Including Data Structures 12 4

Comparing Strings (continued) Java Programming: Program Design Including Data Structures 13 Comparing Strings (continued) Java Programming: Program Design Including Data Structures 14 Short-Circuit Evaluation Computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known Java Programming: Program Design Including Data Structures 15 5

Selection One-way selection Two-way selection Compound (block of) statements Multiple selections (nested if) Conditional operator switch structures Java Programming: Program Design Including Data Structures 16 One-Way Selection Syntax: if (expression) statement Expression called decision maker Statement called action statement Java Programming: Program Design Including Data Structures 17 One-Way Selection (continued) Example 4-11 //Determine the absolute value of an integer import javax.swing.joptionpane; public class AbsoluteValue public static void main(string[] args) int number; int temp; String numstring; numstring = JOptionPane.showInputDialog ("Enter an integer:"); //Line 1 number = Integer.parseInt(numString); //Line 2 temp = number; //Line 3 Java Programming: Program Design Including Data Structures 18 6

One-Way Selection (continued) if (number < 0) //Line 4 number = -number; //Line 5 JOptionPane.showMessageDialog(null, "The absolute value of " + temp + " is " + number, "Absolute Value", JOptionPane.INFORMATION_MESSAGE); //Line 6 System.exit(0); Java Programming: Program Design Including Data Structures 19 Two-Way Selection Syntax: if (expression) statement1 else statement2 else statement must be paired with an if. Java Programming: Program Design Including Data Structures 20 Two-Way Selection (continued) Java Programming: Program Design Including Data Structures 21 7

Two-Way Selection (continued) Example 4-14 if (hours > 40.0) wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); else wages = hours * rate; Java Programming: Program Design Including Data Structures 22 Two-Way Selection (continued) Example 4-15 if (hours > 40.0); // Line 1 wages = 40.0 * rate + // Line 2 1.5 * rate * (hours - 40.0); else //Line 3 wages = hours * rate; //Line 4 Java Programming: Program Design Including Data Structures 23 Compound (Block of) Statements Syntax: statement1 statement2... statementn Java Programming: Program Design Including Data Structures 24 8

Compound (Block of) Statements (continued) if (age > 18) System.out.println("Eligible to vote."); System.out.println("No longer a minor."); else System.out.println("Not eligible to vote."); System.out.println("Still a minor."); Java Programming: Program Design Including Data Structures 25 Multiple Selection: Nested if Syntax: if (expression1) statement1 else if (expression2) statement2 else statement3 Else is associated with the most recent incomplete if Multiple if statements can be used in place of if else statements May take longer to evaluate Java Programming: Program Design Including Data Structures 26 Conditional (? :) Operator Ternary operator Syntax: expression1? expression2 : expression3 If expression1 = true, then the result of the condition is expression2 Otherwise, the result is expression3 Java Programming: Program Design Including Data Structures 27 9

switch Structures switch (expression) case value1: statements1 case value2: statements2... case valuen: statementsn default: statements Expression is also known as selector Expression can be an identifier Value can only be integral Java Programming: Program Design Including Data Structures 28 switch Structures (continued) Java Programming: Program Design Including Data Structures 29 switch Structures (continued) Example 4-24 switch (grade) case 'A': System.out.println("The grade is A."); case 'B': System.out.println("The grade is B."); case 'C': System.out.println("The grade is C."); case 'D': System.out.println("The grade is D."); case 'F': System.out.println("The grade is F."); default: System.out.println("The grade is invalid."); Java Programming: Program Design Including Data Structures 30 10

Programming Example: Cable Company Billing Input: Customer s account number, customer code, number of premium channels to which customer subscribes, number of basic service connections (in the case of business customers) Output: Customer s account number and the billing amount Java Programming: Program Design Including Data Structures 31 Programming Example: Cable Company Billing (continued) Solution: 1. Prompt user for information 2. Use switch statements based on customer s type 3. Use an if statement nested within a switch statement to determine the amount due by each customer Java Programming: Program Design Including Data Structures 32 Chapter Summary Control structures are used to process programs Logical expressions and order of precedence of operators are used in expressions Comparing strings If statement if else statements switch structures Proper syntax for using control statements Java Programming: Program Design Including Data Structures 33 11