Chapter 4 Selection Structures: if and switch Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )

Slides:



Advertisements
Similar presentations
Selection Structures: if and switch Statements
Advertisements

ICS103: Programming in C 4: Selection Structures Muhamed F. Mudawar.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 CS 201 Selection Structures (1) Debzani Deb. 2 Error in slide: scanf Function scanf(“%lf”, &miles); function name function arguments format string variable.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
C++ for Engineers and Scientists Third Edition
1 ICS103 Programming in C Lecture 6: Selection Structures.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Chapter 5 Repetition and Loop Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Adapted from Pearson Addison-Wesley Addison Wesley is an imprint of Chapter 4: Selection Structures: if Statement Adapted from Problem Solving & Program.
Chapter 2 Overview of C Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Flow of Control Part 1: Selection
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
chap4 Chapter 4 Selection Structures: if and switch Statements.
Department of CSE Control Structures: Selection.
Control statements Mostafa Abdallah
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS Prepared By: Pn. Nik Maria Nik Mahamood Reference: Hanly, Koffman, C Problem Solving and Program.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4 : Selection Structures: if and switch statement By Suraya Alias.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
Decisions Chapter 4.
CHAPTER 5: SELECTION STRUCTURES: if and switch STATEMENTS
Chapter 7 Conditional Statements
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CprE 185: Intro to Problem Solving (using C)
ICS103: Programming in C 4: Selection Structures
Control Structures.
Presentation transcript:

Chapter 4 Selection Structures: if and switch Statements Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-2 Control Structures in C Control structuresControl structures control the flow of execution in a program or function. There are three kinds of execution flow: –Sequence –Sequence: the execution of the program is sequential. –Selection –Selection: A control structure which chooses alternative to execute. –Repetition –Repetition: A control structure which repeats a group of statements. selectionWe will focus on the selection control structure in this chapter.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-3 Conditions A program may choose among alternative statements by testing the value of key variables. –e.g., if( your_grade > 60 ) printf(“you are passed!”); ConditionCondition is an expression that is either false (represented by 0) or true (represented by 1). –e.g., “your_grade > 60” is a condition. relationalequality operatorsConditions may contain relational or equality operators, and have the following forms. relational-operator –variable relational-operator variable (or constant) equality-operator –variable equality-operator variable (or constant)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-4 Operators Used in Conditions OperatorMeaningType <Less thanRelational >Greater thanRelational <=Less than or equal toRelational >=Greater than or equal toRelational ==Equal toEquality !=Not equal toEquality

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-5 Examples of Conditions OperatorConditionMeaning <=x <= 0x less than or equal to 0 < Power < MAX_POWPower less than MAX_POW == mom_or_dad == ‘M’ mom_or_dad equal to ‘M’ != num != SETINELnum not equal to SETINEL

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-6 Logical Operators logical operators.There are three kinds of logical operators. –&& : and –|| : or –! : not Logical expressionLogical expression is an expression which uses one or more logical operators, e.g., –(temperature > 90.0 && humidity > 0.90) –! (n = 100).

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-7 The Truth Table of Logical Operators Op 1Op 2 Op 1 && Op2Op 1 || Op2 nonzero Op 1 ! Op 1 nonzero0 01

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-8 Operator Precedence precedenceAn operator’s precedence determines its order of evaluation. Unary operatorUnary operator is an operator that has only one operand. –!, + (plus sign), - (minus sign), and & (address of) –They are evaluated second only after function calls. OperatorPrecedence function callshighest ! + - & * / % + - = > == != && || = lowest

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-9 Evaluation for !flag || (y + z >= x - z) Evaluation tree The result of this expression is true

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-10 Comparing Characters relationalequality operatorsWe can also compare characters in C using the relational and equality operators. ExpressionValue ‘9’ >= ‘0’1 (true) ‘a’ < ‘e’1 (true) ‘Z’ == ‘z’0 (false) ‘a’ <= ‘A’system dependent

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-11 DeMorgan’s Theorem DeMorgan’s theoremDeMorgan’s theorem gives us a way of transforming a logical expression into its complement. –The complement of expr 1 && expr 2 is comp 1 || comp 2, where comp 1 and comp 2 are the complement of expr 1 and expr 2, respectively. –The complement of expr 1 || expr 2 is comp 1 && comp 2. e.g., age > 25 && (status == ‘S’ || status ==‘D’) is equal to !(age <=25 || (status != ‘S’) && status != ‘D’)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-12 The if Statement selectionThe if statement is the primary selection control structure. Syntax: if (condition) statement; else statement; An example of two alternatives: if ( rest_heart_rate > 56 ) printf(“Keep up your exercise program!\n”); else printf(“Your heart is in excellent health!\n”); An example of one alternative: if ( x != 0.0 ) product = product * x;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-13 Flowchart FlowchartFlowchart is a diagram that shows the step-by- step execution of a control structure. diamond-shaped box –A diamond-shaped box represents a decision. rectangular box –A rectangular box represents an assignment statement or a process.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-14 Flowcharts Flowcharts of if Statements with (a) Two Alternatives and (b) One Alternative Decision

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-15 Nested if Statements Nested if statement another if statementNested if statement is an if statement with another if statement as its true task or false task. e.g., if (road_status == ‘S’) else printf(“Drive carefully!\n”); if (temp > 0) { printf(“Wet roads ahead!\n”); } else { printf(“Icy roads ahead!\n”); }

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-16 An Example for the Flowchart of Nested if Statements Another if statement Main if statement

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-17 Multiple-Alternative Decisions multiple-alternative decisionIf there are many alternatives, it is better to use the syntax of multiple-alternative decision. Syntax: if (condition 1 ) statement 1 else if (condition 2 ) statement 2 … else if (condition n ) statement n else statement e

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-18 An Example of Multiple-Alternative Decisions

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-19 The switch Statement a single variablean expressionThe switch statement is used to select one of several alternatives when the selection is based on the value of a single variable or an expression. switch (controlling expression) { case label 1 : statement 1 break ; case label 2 : statement 2 break ; … case label n : statement n break ; default: statement d ; } If the result of this controlling expression matches label 1, execute staement 1 and then break this switch block. If the result matches none of all labels, execute the default statement d.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-20 An Example of a switch Statement with Type char Case Labels class is a char variable. Two or more cases can execute the same statement.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-21 Homework #3 (1/2) input the boiling pointWrite a program that prompts the user to input the boiling point in degree Celsius. output the substanceThe program should output the substance corresponding to the boiling point listed in the table. “substance unknown”The program should output the message “substance unknown” when it does not match any substance. SubstanceBoiling point Water100°C Mercury357°C Copper1187°C Silver2193°C Gold2660°C

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4-22 Homework #3 (2/2) Examples of the scenario of your program. within a range of boiling pointsYou can determine the substance within a range of boiling points to get bonus (e.g., +5 degrees). –Please refer to pages in the text book. You can apply any technique in this chapter. Please input: 357 The substance is Mercury. Please input: 3333 Substance unknown. Please input: 359 The substance is Mercury.