Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.

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.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 5: Control Structures II (Repetition)
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.
C++ for Engineers and Scientists Third Edition
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.
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)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
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.
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
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.
TK 1914 : C++ Programming Control Structures I (Selection)
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
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.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
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)
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):
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
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
EGR 2261 Unit 4 Control Structures I: Selection
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)
Control Structure Chapter 3.
Control Structure.
Presentation transcript:

Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement Equally, relational and logic operators The precedence table Assignment Operator Increment and Decrement Operators

2 Objectives In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean) expressions Discover how to use the selection control structures if, if... else, and switch in a program Learn how to avoid bugs by avoiding partially understood concepts and techniques

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 met if it evaluates to true 3

Control Structures (continued) 4

Relational Operators A condition is represented by a logical (Boolean) expression that can be true or false Relational operators: –Allow comparisons –Require two operands (binary) –Evaluate to true or false 5

Relational Operators (continued) 6

Relational Operators and Simple Data Types You can use the relational operators with all three simple data types –8 < 15 evaluates to true –6 != 6 evaluates to false –2.5 > 5.8 evaluates to false –5.9 <= 7.5 evaluates to true 7

Comparing Characters 8

Relational Operators and the string Type 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

Relational Operators and the string Type (continued) Suppose we have the following declarations: string str1 = "Hello"; string str2 = "Hi"; string str3 = "Air"; string str4 = "Bill"; string str4 = "Big"; 10

Relational Operators and the string Type (continued) 11

Relational Operators and the string Type (continued) 12

Relational Operators and the string Type (continued) 13

Logical (Boolean) operators enable you to combine logical expressions Logical (Boolean) Operators and Logical Expressions unary binary 14

Logical (Boolean) Operators and Logical Expressions (continued) 15

16

17

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

Order of Precedence (continued) 19

Order of Precedence (continued) 20

21

Order of Precedence (continued) 22

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 23

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

Selection: if and if...else One-way selection Two-way selection Compound (block of) statements Multiple selections: nested if Comparing if...else statements with a series of if statements 25

One-Way Selection The syntax of one-way selection is: The statement is executed if the value of the expression is true The statement is bypassed if the value is false ; program goes to the next statement if is a reserved word 26

One-Way Selection (continued) 27

One-Way Selection (continued) 28

29

One-Way Selection (continued) 30

Two-Way Selection Two-way selection takes the form: If expression is true, statement1 is executed; otherwise, statement2 is executed –statement1 and statement2 are any C++ statements else is a reserved word 31

Two-Way Selection (continued) 32

Two-Way Selection (continued) 33

Two-Way Selection (continued) 34

Compound (Block of) Statement Compound statement (block of statements) A compound statement is a single statement 35

Compound (Block of) Statement (continued) if (age > 18) { cout << "Eligible to vote." << endl; cout << "No longer a minor." << endl; } else { cout << "Not eligible to vote." << endl; cout << "Still a minor." << endl; } 36

Multiple Selections: Nested if Nesting: one control statement in another An else is associated with the most recent if that has not been paired with an else 37

38

Multiple Selections: Nested if (continued) 39

Comparing if…else Statements with a Series of if Statements 40

Confusion Between == and = C++ allows you to use any expression that can be evaluated to either true or false as an expression in the if statement: if (x = 5) cout << "The value is five." << endl; The appearance of = in place of == resembles a silent killer –It is not a syntax error –It is a logical error 41

switch Structures switch structure: alternate to if-else switch (integral) expression is evaluated first Value of the expression determines which corresponding action is taken Expression is sometimes called the selector 42

43

switch Structures (continued) One or more statements may follow a case label Braces are not needed to turn multiple statements into a single compound statement The break statement may or may not appear after each statement switch, case, break, and default are reserved words –Use of default is not required, but recommended 44

45

Summary Control structures alter normal control flow Most common control structures are selection and repetition Relational operators: ==,, >=, != Logical expressions evaluate to 1 ( true ) or 0 ( false ) Logical operators: ! (not), && (and), || (or) Three selection structures: one-way selection, two- way selection, and switch statement 46

Summary (continued) The expression in an if or if... else structure is usually a logical expression No stand-alone else statement in C++ –Every else has a related if A sequence of statements enclosed between { and } is a compound statement or block of statements switch structure handles multiway selection break statement ends switch statement 47