 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.

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.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
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)
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.
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
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.
IXA 1234: C++ PROGRAMMING CHAPTER 3. O BJECTIVES In this chapter you will: Learn about control structures Examine relational and logical operators Explore.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
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)
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
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)
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Chapter 4: Control Structures SELECTION STATEMENTS.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
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.
Week 4 Program Control Structure
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.
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.
Java Fundamentals 4.
Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I
Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I
Week 3 C Program Structures (Selection Structures)
Week 3 – Selection Structures
DKT121: Fundamental of Computer Programming
SELECTION STATEMENTS (1)
Java Programming: Guided Learning with Early Objects
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
Control Structure.
Presentation transcript:

 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, 3e 2

 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 repetition of statement(s) Java Programming: From Problem Analysis to Program Design, 3e 3

4

 Relational Operator › Allows you to make comparisons in a program › Binary operator  Condition is represented by a logical expression in Java  Logical expression: expression that has a value of either true or false Java Programming: From Problem Analysis to Program Design, 3e 5

6

 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, 3e 7

8

 class String › Method compareTo › Method equals  Given string str1 and str2 Java Programming: From Problem Analysis to Program Design, 3e 9

String str1 = "Hello"; String str2 = "Hi"; String str3 = "Air"; String str4 = "Bill"; String str5 = "Bigger"; Java Programming: From Problem Analysis to Program Design, 3e 10

Java Programming: From Problem Analysis to Program Design, 3e 11

Java Programming: From Problem Analysis to Program Design, 3e 12

Java Programming: From Problem Analysis to Program Design, 3e 13

Java Programming: From Problem Analysis to Program Design, 3e 14

Java Programming: From Problem Analysis to Program Design, 3e 15

Java Programming: From Problem Analysis to Program Design, 3e 16

Java Programming: From Problem Analysis to Program Design, 3e 17

 Definition: 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, 3e 18

 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, 3e 19

 Syntax: if (expression) statement  Expression referred to as decision maker  Statement referred to as action statement Java Programming: From Problem Analysis to Program Design, 3e 20

Java Programming: From Problem Analysis to Program Design, 3e 21

Example 4-11 //Program to 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, 3e 22

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, 3e 23

 Syntax: if (expression) statement1 else statement2  else statement must be paired with an if Java Programming: From Problem Analysis to Program Design, 3e 24

Java Programming: From Problem Analysis to Program Design, 3e 25

Java Programming: From Problem Analysis to Program Design, 3e 26 Example 4-14 if (hours > 40.0) wages = 40.0 * rate * rate * (hours ); else wages = hours * rate;

Java Programming: From Problem Analysis to Program Design, 3e 27 Example 4-15 if (hours > 40.0); //Line 1 wages = 40.0 * rate * rate * (hours ); //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 Since there is no separate else statement in Java, this code generates a syntax error

 Syntax Java Programming: From Problem Analysis to Program Design, 3e 28

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, 3e 29

 Syntax if (expression1) statement1 else if (expression2) statement2 else statement3  Else associated with 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, 3e 30

 Ternary operator  Syntax expression1 ? expression2 : expression3  If expression1 = true, then the result of the condition is expression 2; otherwise, the result of the condition is expression3 Java Programming: From Problem Analysis to Program Design, 3e 31

Java Programming: From Problem Analysis to Program Design, 3e 32

Java Programming: From Problem Analysis to Program Design, 3e 33 In Java, switch, case, break, and default are reserved words In a switch structure, the expression is evaluated first The value of the expression is then used to perform the actions specified in the statements that follow the reserved word case The expression is usually an identifier The value of the identifier or the expression can be only integral, that is, an integer

 Integral values also include values of type char  The expression is sometimes called the selector; its value determines which statements are selected for execution  A particular case value must appear only once  One or more statements may follow a case label, so you do not need to use braces to turn multiple statements into a single compound statement Java Programming: From Problem Analysis to Program Design, 3e 34

 The break statement may or may not appear after each statements1, statements2,..., statementsn  A switch structure may or may not have the default label Java Programming: From Problem Analysis to Program Design, 3e 35

Java Programming: From Problem Analysis to Program Design, 3e 36

Java Programming: From Problem Analysis to Program Design, 3e 37

Example 4-24 switch (grade) { case 'A': System.out.println("The grade is A."); break; case 'B': System.out.println("The grade is B."); break; case 'C': System.out.println("The grade is C."); break; Java Programming: From Problem Analysis to Program Design, 3e 38

case 'D': System.out.println("The grade is D."); break; case 'F': System.out.println("The grade is F."); break; default: System.out.println("The grade is invalid."); } Java Programming: From Problem Analysis to Program Design, 3e 39

 Input: customer’s account number, customer code, number of premium channels to which customer subscribes, number of basic service connections (in case of business customers)  Output: customer’s account number and the billing amount Java Programming: From Problem Analysis to Program Design, 3e 40

 Solution: › Prompt user for information › Use switch statements based on customer’s type › Use an if statement nested within switch statement to determine amount due by each customer Java Programming: From Problem Analysis to Program Design, 3e 41

 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, 3e 42