Control Structure.

Slides:



Advertisements
Similar presentations
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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.
COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
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.
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.
Computer Science Selection Structures.
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.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
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.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
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.
Control statements Mostafa Abdallah
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#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
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#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
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)
Chapter 4: Control Structures I
Selections Java.
Selection—Making Decisions
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Branching Statements
Java Programming: Guided Learning with Early Objects
Control Structures – Selection
Chapter 4: Control Structures I
Chapter 4: Control Structures I (Selection)
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
The System.exit() Method
Control Structure Chapter 3.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Presentation transcript:

Control Structure

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.

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.

Control Structures

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.

Relational Operators

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. 8 < 5 always evaluates to false. 8 < ‘5’ always evaluates to true. //‘5’= 53

Relational Operators and Primitive Data Types

Comparing Strings Strings are compared character by character, using the collating sequence, until one of three conditions is met: 1. A mismatch is found. 2. One string is exhausted. 3. The last characters have been compared and are equal.

Comparing Strings “Air” < “Big” // because ‘A’ < ‘B’ For example, “Air” < “Big” // because ‘A’ < ‘B’ “Air” < “An” // because ‘i’ < ‘n’ “Hello” < “hello” // because ‘H’ < ‘h’

Comparing Strings Strings can not be compared with the usual <, <=, >, or >= operators, and the == and != operators don't compare the characters in the strings.

Comparing Strings class String Method compareTo (<0 , 0 , >0) Given string str1 and str2

Comparing Strings String str1 = "Hello"; String str2 = "Hi"; String str3 = "Air"; String str4 = "Bill"; String str5 = "Bigger";

Comparing Strings

public class Example4_2 { public static void main(String[] args) { String str1 = "Hello"; //Line 1 String str2 = "Hi"; //Line 2 String str3 = "Air"; //Line 3 String str4 = "Bill"; //Line 4 String str5 = "Bigger"; //Line 5 System.out.println("Line 6: " + "str1.compareTo(str2) evaluates to " + str1.compareTo(str2)); //Line 6 System.out.println("Line 7: " + "str1.compareTo(\"Hen\") evaluates to " + str1.compareTo("Hen")); //Line 7 System.out.println("Line 8: " + "str4.compareTo(str3) evaluates to " + str4.compareTo(str3)); //Line 8 System.out.println("Line 9: " +"str1.compareTo(\"hello\") evaluates to " + str1.compareTo("hello")); //Line 9 System.out.println("Line 10: " + "str2.compareTo(\"Hi\") evaluates to " + str2.compareTo("Hi")); //Line 10 System.out.println("Line 11: " + "str4.compareTo(\"Billy\") evaluates to " + str4.compareTo("Billy")); //Line 11 System.out.println("Line 12: " + "str5.compareTo(\"Big\") evaluates to " + str5.compareTo("Big")); //Line 12 } }

Comparing Strings Values such as -4, -2, 1 and so on, are differences of the collating sequence of the first unmatched characters of the string. For example: in line 6: where, str1= “Hello”, str2=“Hi” ‘e’  101 ‘i’ 105 101 – 105  -4

Comparing Strings In addition to the method compareTo, you can use the method equals of the class String. Returns true or false. Example: str1 = “Hello”, str2= “Hi” str1.equals(“Hello”); // returns true str1.equals(str2); //returns false

Comparing Strings You should use one of the following tests to compare the contents of two strings: string1.equals(string2) string1.compareTo(string2) Here's the wrong way to do it: string1 == string2

Logical (Boolean) Operators ! is unary operator. && is binary operator. || is binary operator.

Logical (Boolean) Operators This is called the truth table for the operator ! Example: !(‘A’ > ‘B’) is true. Because ‘A’ > ‘B’ is false  !(‘A’ > ‘B’) is true

Logical (Boolean) Operators

Logical (Boolean) Operators Examples

11 + 5 <= 9 || 6 < 15 && 7 >= 8 Order of Precedence which to solve first: arithmetic, relational or logical ?

(17 < 4*3+5)||(8*2 == 4*4) && !(3+3 == 6) Precedence Rules Example 4_6: Evaluate the following expression: (17 < 4*3+5)||(8*2 == 4*4) && !(3+3 == 6) = (17 < 12+5)||(16 == 16) && !(6 == 6) = (17 < 17) || true && ! (true) = false || true && false = false || false = false

Precedence Rules Example: suppose the following declarations: boolean found = true; boolean flag =false; double x =5.2; Evaluate: !found  false x > 4.0  true flag && found  false

Short-Circuit Evaluation Suppose: int age = 25; char grade=‘B’; (age >= 21) || ( 3 + 8 == 5) Because (25 >= 21) is true and the operator used is ||,due to short-circuit evaluation the computer does not evaluate ( 3 + 8 == 5). (grade == 'A') && (3 - 2 >= 7) Because (‘B’ == 'A') is false and the operator used is &&,due to short-circuit evaluation the computer does not evaluate ( 3 - 2 >= 7).

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. Example: (x>y) || (x==5) // if (x>y) is true, (x==5) is not evaluated (a==b) && (x>=7) // if (a==b) is false, (x>=7) is not evaluated (x>0) && ( (y = z*2) > 5) // if (x>0) is false, ((y = z*2) > 5) is not evaluated and the value of y will not change

IF statement

put ; after end of statement One-Way Selection Must be in ( ) and no ; Syntax: if (expression) statement ; Expression referred to as decision maker. If the value of the expression is true  statement executes. If the value of the expression is false  statement does not executes. Statement referred to as action statement. put ; after end of statement

If .. Else

Two-Way Selection Syntax: if (expression) statement1 else statement2 else statement must be paired with an if.

Compound (Block of) Statements Syntax: { statement1 statement2 . statementn }

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, D.S. Malik

Nested if

Multiple Selection: Nested if Syntax: if (expression1) statement1 else if (expression2) statement2 statement3 Multiple if statements can be used if there is more than two alternatives else is associated with the most recent if that does not have an else. Java Programming: From Problem Analysis to Program Design, D.S. Malik

Multiple Selection: Nested if // Assume that score is of type int. Based on the value of score, the following code determines the grade if (score >= 90) System.out.println (“Grade is A”); else if (score >=80 ) System.out.println (“Grade is B”); if (score >=70 ) System.out.println (“Grade is C”); if (score >=60 ) System.out.println (“Grade is D”); System.out.println (“Grade is F”);

Multiple Selection: Nested if if( tempreture >= 50 ) if (tempreture >= 80) System.out.println (“Good swimming day”); else System.out.println (“Good golfing day”); System.out.println (“Good tennis day”);

Switch

Switch Structures Expression is also known as selector. switch (expression) { case value1: statements1 break; case value2: statements2 ... case value n: statements n default: statements } Expression is also known as selector. Expression can be an identifier. Value can only be integral.

Switch With break Statements x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? break; switch (N) { case 1: x = 10; break; case 2: x = 20; case 3: x = 30; }

Switch With No break Statements switch (N) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ?

Switch With Break And Default Statements 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."); }

With Nested If if (grade == 'A') System.out.println("The grade is A."); else if (grade == 'B') System.out.println("The grade is B."); else if (grade == 'C') System.out.println("The grade is C."); else if (grade == 'D') System.out.println("The grade is D."); else if (grade == 'F') System.out.println("The grade is F."); else System.out.println("The grade is invalid.");