1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I Instructor: Mohammad Mojaddam
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)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Chapter 4: Control Structures I (Selection)
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
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.
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)
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
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.
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.
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.
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.
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 STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 4: Control Structures I (Selection)
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
C++ Programming Control Structures I (Selection).
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.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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 (Selection)
Chapter 4: Control Structures I
Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I
Sequence, Selection, Iteration The IF Statement
EGR 2261 Unit 4 Control Structures I: Selection
Topics The if Statement The if-else Statement Comparing Strings
Java Programming: Guided Learning with Early Objects
Control Structures – Selection
Chapter 4: Control Structures I
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Control Structures I (Selection)
Chapter 4 Selection.
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Control Structure.
Presentation transcript:

1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006

2 Contents Control structures Relational and logical operators Logical (Boolean) expressions Selection control structures: if, if...else,

3 Control Structures A computer can proceed: In sequence Selectively (branch) - making a choice Repetitively (iteratively) - looping Some statements are executed only if certain conditions are met A condition is represented by a logical (Boolean) expression that can be true or false A condition is met if it evaluates to true

4

5 Relational Operators Relational operators: Allow comparisons Require two operands (binary) Return 1 if expression is true, 0 otherwise Comparing values of different data types may produce unpredictable results For example, 8 < '5' should not be done Any nonzero value is treated as true

6

7

8 Comparing string Types Relational operators can be applied to strings Strings are compared character by character, starting with the first character Comparison continues until either a mismatch is found or all characters are found equal If two strings of different lengths are compared and the comparison is equal to the last character of the shorter string The shorter string is less than the larger string

9 string Comparison Example Suppose we have the following declarations: string str1 = "Hello"; string str2 = "Hi"; string str3 = "Air"; string str4 = "Bill";

10 Logical (Boolean) Operators Logical (Boolean) operators enable you to combine logical expressions Three logical (Boolean) operators: ! - not && – and || - or Logical operators take logical values as operands and yield logical values as results ! is unary; && and || are binary operators Putting ! in front of a logical expression reverses its value

11

12 Precedence of Operators Relational and logical operators are evaluated from left to right The associativity is left to right Parentheses can override precedence

13

14 Short-Circuit Evaluation Short-circuit evaluation: evaluation of a logical expression stops as soon as the value of the expression is known Example: (age >= 21) || ( x == 5)//Line 1 (grade == ‘ A ’ ) && (x >= 7)//Line 2

15 Logical (Boolean) Expressions The bool Data Type and Logical (Boolean) Expressions The data type bool has logical (Boolean) values true and false bool, true, and false are reserved words The identifier true has the value 1 The identifier false has the value 0

16 Logical (Boolean) Expressions (continued) Logical expressions can be unpredictable The following expression appears to represent a comparison of 0, num, and 10: 0 <= num <= 10 It always evaluates true because 0 <= num evaluates to either 0 or 1, and 0 <= 10 is true and 1 <= 10 is true The correct way to write this expression is: 0 <= num && num <= 10

17 One-Way (if) Selection The syntax of one-way selection is: if(expression) statement Statement is executed if the value of the expression is true Statement is bypassed if the value is false; program goes to the next statement

18 One-Way (if) Selection (continued) The expression is sometimes called a decision maker because it decides whether to execute the statement that follows it The statement following the expression is sometimes called the action statement The expression is usually a logical expression The statement is any C++ statement if is a reserved word

19

20 Two-Way (if … else) Selection Two-way selection takes the form: if(expression) statement1 else statement2 If expression is true, statement1 is executed otherwise statement2 is executed statement1 and statement2 are any C++ statements else is a reserved word

21

22 End of lecture 7 Thank you!