ICT1412 - Introduction to Programming Chapter 4 – Control Structures I.

Slides:



Advertisements
Similar presentations
CSCI 51 Introduction to Programming Dr. Joshua Stough February 10, 2009.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
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.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Logical Operators and Conditional statements
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
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.
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
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.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Chapter 4: Control Structures I
COMP 14 Introduction to Programming
Chapter 4: Control Structures I
Selections Java.
EGR 2261 Unit 4 Control Structures I: Selection
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Chapter 4: Control Structures I
Logical Operators & Truth Tables.
Chapter 4: Control Structures I (Selection)
Chapter 3: Program Statements
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Control Structure.
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

ICT Introduction to Programming Chapter 4 – Control Structures I

Prepared by K.T. Ng2 Control Structures Three methods of processing a program: 1.In sequence statements are executed one after another in order 2.Branching altering the flow of program execution by making a selection or choice 3.Looping altering the flow of program execution by repetition of statement(s)

Prepared by K.T. Ng3 Flow of Execution

Prepared by K.T. Ng4 Examples of Conditionals Note: the following are not actual Java statements 1) if (score is greater than or equal to 90) grade is A 2) if (hours worked are less than or equal to 40) wages = rate * hours otherwise wages = (rate * 40) * (rate * (hours - 40)) 3) if (temperature is greater than 21 degrees and it is not raining) recommended activity is fishing

Prepared by K.T. Ng5 Relational Operators Relational Operator allows you to make comparisons in a program binary operator needs two operands Condition is represented by a logical (Boolean) expression expression that has a value of either true or false

Prepared by K.T. Ng6 Relational Operators (Cont.) Less than < Greater than > Equal to == Note: not assignment ‘ = ‘ Not equal to != Less than or equal to <= Greater than or equal to >=

Prepared by K.T. Ng7 Comparing Characters In Java, characters are ordered according to the Unicode / ASCII character set ‘a’ comes before ‘b’ in the character set, so we can say ‘a’ < ‘b’ Order space character (' ') digits (‘0’, ‘1’, …) uppercase lowercase Application: sort alphabetically

Prepared by K.T. Ng8 Comparing Characters (Cont.) 'a' > 'A' '6' < 7 ' ' <= 's' 97 > 65 true 54 < 7 false 32 <= 115 true

Prepared by K.T. Ng9 Comparing Floating-Point Be careful when using the equality (==) to compare floating point numbers Every bit in the representation must be equal computer can only store a certain number of digits after the decimal If the numbers are results of computation, it’s unlikely that two floating point numbers will be exactly equal

Prepared by K.T. Ng10 Comparing Floating-Point (Cont.) 3.0 / / 7.0 (3.0 / 7.0) + (2.0 / 7.0) + (2.0 / 7.0) == false If we did this math with exact arithmetic, this expression would be true

Prepared by K.T. Ng11 Comparing Strings Strings are compared on a character-by- character basis first character not in common determines how the strings compare e.g. : "Airplane" is less than "Airport" If two strings have different lengths, and one is a substring of the other, the shorter one is evaluated as less e.g. : "Air" is less than "Airplane"

Prepared by K.T. Ng12 Comparing Strings (Cont.) Don't use relational operators (==, ) for Strings str1.compareTo (str2) Returns an integer value: < 0 if str1 < str2 0 if str1 = str2 >0 if str1 > str2 str1.equals (str2) Returns a boolean value of true or false method parameter

Prepared by K.T. Ng13 Questions Given String str = "Homer"; str.compareTo("Marge") str.equals("homer") str.compareTo("Bart") negative value false positive value

Prepared by K.T. Ng14 Boolean Expressions Named for George Boole ( ), the founder of symbolic logic A boolean expression is an expression that can be evaluated to either true or false Remember that booleans in Java are true false Examples of boolean expressions: 2+2 is not equal to 5 a is equal to 0 10 is less than 5

Prepared by K.T. Ng15 Boolean Operators NOT!(unary) !(2+2==5) AND&&(binary) (2+2==5) && (1+1==2) OR||(binary) (2+2==5) || (1+1==2) true false true

Prepared by K.T. Ng16 NOT Operator

Prepared by K.T. Ng17 AND and OR Operators

Prepared by K.T. Ng18 Boolean Expressions Examples Simple: 2+2 is not equal to 52+2 != 5 10 is less than 510 < 5 'A' is equal to 7'A' == 7 Compound: a is equal to 3 and b is equal to 5 (a==3) && (b==5) x is not equal to 2 or y is less than 4 (x!=2) || (y<4) s is not equal to b and t is greater than or equal to 3 or t is less than 0 (s!=b) && (t>=3) || (t<0)

Prepared by K.T. Ng19 Short Circuited Operators The processing of logical AND ( && ) and logical OR ( || ) is “short-circuited” 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, the remaining right operand is not evaluated count != 0 && total/count > MAX

Prepared by K.T. Ng20 Precedence of Operators

Prepared by K.T. Ng21 Boolean Expressions Common Error Logical expression that evaluates to true if the value of num is between 0 and 10 0 <= num <= 10 0 <= num && num <= 10 incorrect syntax correct syntax

Prepared by K.T. Ng22 Questions 1.What type of primitive variable can the result of a logical expression be stored in? 2.Under what conditions would the expression (ch >= 'A' && ch <= 'Z') evaluate to false? 3.What method do you use to compare two Strings? 4.Why is 'a' greater than 'A'? boolean ch 'Z' compareTo Because 'a' comes after 'A' in the ASCII (or Unicode) character table

Prepared by K.T. Ng23 Questions (Cont.) Given the following declarations, evaluate each Boolean expression: int count = 0, sum = 54; double x = 4.3, y = 1.2; boolean wrong = true; 1.(wrong && sum > 60) 2.((x > 5) || !(sum == 55)) 3.!((y > 1.0) && (x < 4)) 4.((count != 4) || (sum > 100) && wrong) false true (false || !(false)) !(true && false) (true || false && true) (true && false) (true || false)

Prepared by K.T. Ng24 Conditional Statements Let us choose which statement will be executed next also called selection statements Java's conditional statements: the if statement the if-else statement the switch statement (next chapter)

Prepared by K.T. Ng25 One-Way Selection Syntax: if (expression) statement

Prepared by K.T. Ng26 The if Statement if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.

Prepared by K.T. Ng27 The if Statement if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); First, the condition is evaluated. The value of sum is either greater than the value of MAX, or it is not. If the condition is true, the assignment statement is executed. If it is not, the assignment statement is skipped. Either way, the call to println is executed next.

Prepared by K.T. Ng28 Block Statements Syntax: { statement1 statement2. statementn } We use curly braces to group a set of individual statements. This way we can have multiple statements execute based on a decision. If we don't use curly braces, only a single statement will be executed based on the result of a decision.

Prepared by K.T. Ng29 The if Statement if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); A if (sum > MAX) { delta = sum - MAX; } System.out.println ("The sum is " + sum); B if (sum > MAX) { delta = sum - MAX; System.out.println ("The sum is " + sum); } C

Prepared by K.T. Ng30 Curly Braces int num = 87, max = 25; if (num >= max*2) { System.out.println ("apple"); } System.out.println ("orange"); System.out.println ("pear"); int num = 87, max = 25; if (num >= max*2) System.out.println ("apple"); System.out.println ("orange"); System.out.println ("pear");

Prepared by K.T. Ng31 Watch out! if (score >= 90); grade = "A"; No matter the result of the condition, grade will be assigned "A". The semicolon after the if statement is a semantic error.

Prepared by K.T. Ng32 Two-Way Selection

Prepared by K.T. Ng33 The if-else Statement if ( condition ) statement1; else statement2; If the condition is true, statement1 is executed; if the condition is false, statement2 is executed One or the other will be executed, but not both

Prepared by K.T. Ng34 The if-else Statement (example) if (height <= MAX) { adjustment = 0; } else { adjustment = MAX-height; }

Prepared by K.T. Ng35 Conditional (? :) Operator 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 e.g. max = (a >= b)? a : b; is equivalent to if (a >= b) max = a; else max = b;