Chapter 4: Control Structures I

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I Instructor: Mohammad Mojaddam
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Chapter 4: Control Structures I (Selection)
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Control Structures I (Selection)
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures I
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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: Learn about control structures Examine relational and logical operators Explore how.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Chapter 4: Control Structures SELECTION STATEMENTS.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
TK 1914 : C++ Programming Control Structures I (Selection)
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
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.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 4: Control Structures I (Selection)
C++ Programming Control Structures I (Selection).
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 3 Selection Statements
Java Fundamentals 4.
Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I
Chapter 4: Control Structures I (Selection)
CNG 140 C Programming (Lecture set 3)
Selections Java.
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
SELECTION STATEMENTS (1)
Java Programming: Guided Learning with Early Objects
Control Structures – Selection
Chapter 5 Control Statements
Chapter 4: Control Structures I
Chapter 4: Control Structures I (Selection)
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Week 3 – Program Control Structure
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Control Structure.
Presentation transcript:

Chapter 4: Control Structures I Java Programming: From Problem Analysis to Program Design, Second Edition

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: From Problem Analysis to Program Design, Second Edition

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: From Problem Analysis to Program Design, Second Edition

Control Structures Java Programming: From Problem Analysis to Program Design, Second Edition

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: From Problem Analysis to Program Design, Second Edition

Relational Operators Java Programming: From Problem Analysis to Program Design, Second Edition

Relational Operators and Primitive Data Types Can be used with integral and floating-point data types. Can be used with the char data type. Unicode Collating Sequence. Java Programming: From Problem Analysis to Program Design, Second Edition

Relational Operators and Primitive Data Types Java Programming: From Problem Analysis to Program Design, Second Edition

Comparing Strings class String Given string str1 and str2 Method compareTo Method equals Given string str1 and str2 Java Programming: From Problem Analysis to Program Design, Second Edition

Comparing Strings String str1 = "Hello"; String str2 = "Hi"; String str3 = "Air"; String str4 = "Bill"; String str5 = "Bigger"; Java Programming: From Problem Analysis to Program Design, Second Edition

Comparing Strings Java Programming: From Problem Analysis to Program Design, Second Edition

Comparing Strings Java Programming: From Problem Analysis to Program Design, Second Edition

Comparing Strings Java Programming: From Problem Analysis to Program Design, Second Edition

Comparing Strings Java Programming: From Problem Analysis to Program Design, Second Edition

Short-Circuit Evaluation A process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known. Java Programming: From Problem Analysis to Program Design, Second Edition

Selection One-way selection Two-way selection Compound (block of) statements Multiple selections (nested if) Conditional operator switch structures Java Programming: From Problem Analysis to Program Design, Second Edition

One-Way Selection Syntax: if (expression) statement Expression referred to as decision maker. Statement referred to as action statement. Java Programming: From Problem Analysis to Program Design, Second Edition

One-Way Selection 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: From Problem Analysis to Program Design, Second Edition

One-Way Selection 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: From Problem Analysis to Program Design, Second Edition

Two-Way Selection Syntax: if (expression) statement1 else statement2 else statement must be paired with an if. Java Programming: From Problem Analysis to Program Design, Second Edition

Two-Way Selection Java Programming: From Problem Analysis to Program Design, Second Edition

Two-Way Selection Example 4-14 if (hours > 40.0) wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); else wages = hours * rate; Java Programming: From Problem Analysis to Program Design, Second Edition

Two-Way Selection Example 4-15 if (hours > 40.0); //Line 1 wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); //Line 2 else //Line 3 wages = hours * rate; //Line 4 Because a semicolon follows the closing parenthesis of the if statement (Line 1), the else statement stands alone. The semicolon at the end of the if statement (see Line 1) ends the if statement, so the statement at Line 2 separates the else clause from the if statement. That is, else is by itself. Because there is no separate else statement in Java, this code generates a syntax error. Java Programming: From Problem Analysis to Program Design, Second Edition

Compound (Block of) Statements Syntax: { statement1 statement2 . statementn } Java Programming: From Problem Analysis to Program Design, Second Edition

Compound (Block of) Statements 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: From Problem Analysis to Program Design, Second Edition

Multiple Selection: Nested if Syntax: if (expression1) statement1 else if (expression2) statement2 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: From Problem Analysis to Program Design, Second Edition

Conditional (? :) Operator Ternary operator Syntax: expression1 ? expression2 : expression3 If expression1 = true, then the result of the condition is expression2. Otherwise, the result of the condition is expression3. Java Programming: From Problem Analysis to Program Design, Second Edition

switch Structures Expression is also known as selector. switch (expression) { case value1: statements1 break; 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: From Problem Analysis to Program Design, Second Edition

switch Structures Java Programming: From Problem Analysis to Program Design, Second Edition

switch Structures Example 4-24 switch (grade) { case 'A': System.out.println("The grade is A."); break; 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: From Problem Analysis to Program Design, Second Edition

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: From Problem Analysis to Program Design, Second Edition

Programming Example: Cable Company Billing Solution: Prompt user for information. Use switch statements based on customer’s type. Use an if statement nested within a switch statement to determine the amount due by each customer. Java Programming: From Problem Analysis to Program Design, Second Edition

Chapter Summary Control structures are used to process programs. Logical expressions and order of precedence of operators are used in expressions. Compare strings. If statements. if…else statements. switch structures. Proper syntax for using control statements. Java Programming: From Problem Analysis to Program Design, Second Edition