CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS

Slides:



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

 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Chapter 4 Selection Structures: if and switch Statements Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering.
Lesson 5 - Decision Structure By: Dan Lunney
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 4 Selection Structures: if and switch Statements.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 TDBA66, VT-03, Lecture - Ch. 4 Control structures Conditions (expr1 op expr2 op expr3..) Choise (if statements) Repetition (loops) see Chap. 5.
1 CS 201 Selection Structures (1) Debzani Deb. 2 Error in slide: scanf Function scanf(“%lf”, &miles); function name function arguments format string variable.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Copyright © 2012 Pearson Education, Inc. Chapter 3 Control Structures: Selection.
1 ICS103 Programming in C Lecture 6: Selection Structures.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 4: Selection Structures: if and switch Statements Problem Solving.
Chapter 4 Selection Structures: if and switch Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
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 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving,
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
A. Abhari CPS1251 Topic 4: Control structures Control Structures Overview Conditions if Statement Nested if Statements switch Statement Common Programming.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
chap4 Chapter 4 Selection Structures: if and switch Statements.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
CHAPTER 6: REPETITION AND LOOP STATEMENTS Learning outcomes  Define the concept of repetition structure.  Specify.
Chapter 4 : Selection Structures: if and switch statement By Suraya Alias.
Decision making If.. else statement.
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
More on the Selection Structure
CHAPTER 6: REPETITION AND LOOP STATEMENTS
INSPIRING CREATIVE AND INNOVATIVE MINDS
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Selection—Making Decisions
EGR 2261 Unit 4 Control Structures I: Selection
The Selection Structure
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Topics The if Statement The if-else Statement Comparing Strings
Lecture 2: Logical Problems with Choices
Selection Structures: if and switch Statements
Objectives After studying this chapter, you should be able to:
Decision making If statement.
Visual Basic – Decision Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 4: Control Structures I (Selection)
Decision I (if Statement)
Topics discussed in this section:
Chapter 4: Selection Structures: if and switch Statements
Introduction: Some Representative Problems
Chapter 5: The Selection Structure
Control Structure.
Chapter 6 Dynamic Programming.
ICS103: Programming in C 4: Selection Structures
Chapter 4 Greedy Algorithms.
Structural Program Development: If, If-Else
Presentation transcript:

CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Learning outcomes ------------------------------------------- Define the concept of selection structure. Apply the if, if/else, if/else if, switch/case selection structure to select actions. Design selection using algorithm for solving problems. Convert problem design into C code using selection controls. Trace an algorithm or program to verify that it does what user expect. Write a program using selection statements that control the flow of program execution Hanly, Koffman, C Problem Solving and Program Design in C, Fifth Edition, Pearson International Edition. Reference... Prepared By: Pn. Nik Maria Nik Mahamood (Coordinator DDC 1012) Prepared by: Pn. Nik Maria Nik Mahamood

This chapter introduces to: Control Structure Conditions The if Statements If Statements with Compound Statements. Nested if Statements and Multiple-Alternative Decisions Decision Steps in Algorithms The switch Statement

CONTROL STRUCTURES Control structure – a combination of individual instructions into a single logical unit with one entry point and one exit point. Instruction are organized into three kinds of control structures to control execution flow: sequence, selection and repetition. Compound statement – a group of statements bracketed by { and } that are executed sequentially.

{ statement1; } Example: statement2; … statementN; A selection control structure chooses which alternative to execute.

CONDITIONS This topic already covered in chapter 4. You can refer this topic at page 147 – 157 (Main TextBook).

if (rest_heart_rate > 56) THE if STATEMENT In C, the if statement is the primary selection control structure. If statement with two alternatives The if statement if (rest_heart_rate > 56) printf(“Keep up your exercise!\n”); else printf(“Your heart is in execllent health!\n”); Syntax as follows: if (ungkapan) kenyataan;

Figure 4.4 Flowcharts of if Statements with (a) Two Alternatives and (b) One Alternative Source: This figure is taken from Pearson Addison-Wesley, 2004.

if Statement with one alternative if (x != 0.0) product = product * x; Syntax as follows: If (ungkapan) kenyataan; Another example if (k < j) min = k; prinf(“Akhir kenyataan if\n”); will be executed if k < j Always be executed even k >= j

IF STATEMENTS WITH COMPOUND STATEMENTS Example #1 if (k < j) { min = k; prinf(“Akhir kenyataan if\n”); } Example #2 Both will be executed if k < j Source: This figure is taken from Pearson Addison-Wesley, 2004.

DECISISON STEPS IN ALGORITHMS Decision step – an algorithm step that selects one of several actions. Case study : water Bill Problem See page 166 - 174

Figure 4.6 Structure Chart for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2004.

Figure 4.7 Program for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2004.

Figure 4.7 Program for Water Bill Problem (cont’d) Source: This figure is taken from Pearson Addison-Wesley, 2004.

Figure 4.7 Program for Water Bill Problem (cont’d) Source: This figure is taken from Pearson Addison-Wesley, 2004.

Figure 4.7 Program for Water Bill Problem (cont’d) Source: This figure is taken from Pearson Addison-Wesley, 2004. Figure 4.8 Sample Run of Water Bill Program Source: This figure is taken from Pearson Addison-Wesley, 2004.

NESTED if STATEMENTS AND MULTIPLE-ALTERNATIVE DECISIONS Nested if statements – an if statement with another if statement as its true task or its false task. Example: if (x > 0) num_pos = num_pos + 1; else { if (x < 0) num_neg = num_neg + 1; else num_zero = num_zero + 1; }

Comparison of nested if and sequence of if. See Table 4.10 at page 180 Multiple – Alternative Decision form of nested if: if (x > 0) num_pos = num_pos + 1; else if (x < 0) num_neg = num_neg + 1; else if num_zero = num_zero + 1;

For more exercises, see example 4.16 at page 182 – 183 Example 4.19 at page 186 – 188 (Flowchart of example 4.19 shown as below) Source: This figure is taken from Pearson Addison-Wesley, 2004.

THE switch STATEMENT The switch statement may also be used in C to select one of several alternatives. Used expression in implementation. This expression also known as controlling expression The value of this expression may be of type char int But not of type double

Figure 4.12 Example of a switch Statement with Type char Case Labels Source: This figure is taken from Pearson Addison-Wesley, 2004.