CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

Selection Structures: if and switch Statements
 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.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
1 ICS103 Programming in C Ch4: Selection Structures.
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.
Chapter 4 Selection Structures: if and switch Statements Instructor: Alkar / Demirer.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
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.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
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.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
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.
Pseudocode. Algorithm A procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3: Top-Down Design with Functions Problem Solving & Program.
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
CNG 140 C Programming (Lecture set 3)
More on the Selection Structure
CHAPTER 6: REPETITION AND LOOP STATEMENTS
INSPIRING CREATIVE AND INNOVATIVE MINDS
Selection—Making Decisions
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
Lecture 2: Logical Problems with Choices
2-1 Making Decisions Sample assignment statements
Computers & Programming Languages
Selection Structures: if and switch Statements
Decision making If statement.
Visual Basic – Decision Statements
Chapter 4: Control Structures I (Selection)
Decision I (if Statement)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 4: Selection Structures: if and switch Statements
ICS103: Programming in C 4: Selection Structures
Structural Program Development: If, If-Else
Presentation transcript:

CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program Design in C, Sixth Edition, Pearson International Edition. Refer chapter 4 (Pg. 178 – 240) INSPIRING CREATIVE AND INNOVATIVE MINDS DDC1012: PROGRAMMING

SELECTION STRUCTURES: if and switch STATEMENTS 5.1Control Structures 5.2Conditions 5.3The if Statements 5.4If Statements with Compound Statements. 5.5Nested if Statements and Multiple-Alternative Decisions 5.6Decision Steps in Algorithms 5.7The switch Statement INSPIRING CREATIVE AND INNOVATIVE MINDS

Control structures – control the flow of execution in a program. Control structures - 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 –Repetition. Compound statement – a group of statements bracketed by { and } that are executed sequentially. INSPIRING CREATIVE AND INNOVATIVE MINDS 5.1 CONTROL STRUCTURES

Example: { statement1; statement2; … statementN; } A selection control structure chooses which alternative to execute. INSPIRING CREATIVE AND INNOVATIVE MINDS 5.1 CONTROL STRUCTURES

This topic already covered in chapter 4. You can refer this topic at page (Main TextBook). INSPIRING CREATIVE AND INNOVATIVE MINDS 5.2 CONDITIONS

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.3 THE if STATEMENTS if Statement with one alternative –Syntax as follows: If (condition) statements; –Example #1 if (x != 0.0) product = product * x; –Example #2 if (k < j) min = k; prinf(“Akhir kenyataan if\n”); will be executed if k < j Always be executed even k >= j

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.3 THE if STATEMENTS If statement with two alternatives – Syntax as follows: if (condition) statement; else statement; – Example if (rest_heart_rate > 56) printf(“Keep up your exercise!\n”); else printf(“Your heart is in execllent health!\n”);

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.4 if STATEMENTS WITH COMPOUND STATEMENTS Example #1 if (k < j) { min = k; prinf(“END if STATEMENT\n”); } Example #2 Source: This figure is taken from Pearson Addison-Wesley, 2009

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.4 if STATEMENTS WITH COMPOUND STATEMENTS Example #3 (Taken from Example 4.13 page 195) if (ctri < = MAX_SAFE_CTRI) { printf(“Car #%d: Safe\n”, auto_id); safe = safe + 1; } else { printf(“Car #%d: unsafe\n”, auto_id); unsafe = unsafe + 1; }

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Decision step – an algorithm step that selects one of several actions. Refer Case study : water Bill Problem (See page 198 – 206)

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.6 Structure Chart for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.7 Program for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.7 Program for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.7 Program for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.7 Program for Water Bill Problem Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.5 DECISION STEPS IN ALGORITHM Figure 4.8 Sample Run of Water Bill Program Source: This figure is taken from Pearson Addison-Wesley, 2009.

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.6 NESTED if STATEMENTS AND MULTIPLE ALTERNATIVES DECISION  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; }

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.6 NESTED if STATEMENTS AND MULTIPLE ALTERNATIVES DECISION  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;

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.6 NESTED if STATEMENTS AND MULTIPLE ALTERNATIVES DECISION  For more exercises, see - Example 4.16 at page Example 4.17 at page Example 4.18 at page Example 4.19 at page

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.7 THE switch STATEMENTS  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

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.7 THE switch STATEMENTS  Question taken from example 4.20 at page 222  Using switch statements to implement the following requirement: Class IDShipp Class B or bBattleship C or cCruiser D or dDestroyer F or fFrigate

INSPIRING CREATIVE AND INNOVATIVE MINDS 5.7 THE switch STATEMENTS Figure 4.12 Example of a switch Statement with Type char Case Labels Source: This figure is taken from Pearson Addison-Wesley, 2009.