1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical operators: and, or, and not ❏ To understand how a C program.

Slides:



Advertisements
Similar presentations
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Chapter 4: Control Structures: Selection
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
C++ Programming: From Problem Analysis to Program Design, Fourth 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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
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
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand the rules of precedence and associativity in evaluating expressions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Chapter 4: Control Structures SELECTION STATEMENTS.
Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide.
1 2. Program Construction in Java. 2.4 Selection (decisions)
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.
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.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
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.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Computer Science: A Structured Programming Approach Using C1 5-5 Incremental Development Part II In Chapter 4, we introduced the concept of incremental.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
Random Functions Selection Structure Comparison Operators Logical Operator
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Introduction to C++ Programming Language
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
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Topics discussed in this section:
Topics discussed in this section:
Topics discussed in this section:
Topics discussed in this section:
Control Structure Chapter 3.
Topics discussed in this section:
Computer Programming Basics
Selection—Making Decisions
Topics discussed in this section:
Control Structure.
Structural Program Development: If, If-Else
Presentation transcript:

1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical operators: and, or, and not ❏ To understand how a C program evaluates a logical expression ❏ To write programs using logical and comparative operators ❏ To write programs that use two-way selection: if... else statements ❏ To write programs that use multi-way selection: switch and else...if ❏ To understand C’s classification of characters ❏ To write programs that use C’s character functions ❏ To be able to design a structure chart that specifies function selection Chapter 5 Chapter 5 Selection—Making Decisions Selection—Making Decisions

2 5-1 Logical Data and Operators A piece of data is called logical if it conveys the idea of true or false. In real life, logical data (true or false) are created in answer to a question that needs a yes– no answer. In computer science, we do not use yes or no, we use true or false. Logical Data in C Logical Operators Evaluating Logical Expressions Comparative Operators Topics discussed in this section:

3 FIGURE 5-1 true and false on the Arithmetic Scale

4 FIGURE 5-2 Logical Operators Truth Table

5 FIGURE 5-3 Short-circuit Methods for and /or

6 PROGRAM 5-1Logical Expressions

7 FIGURE 5-4 Relational Operators ( 關係運算子 )

8 FIGURE 5-5 Comparative Operator Complements ( 比較運算子的互補 )

9 Table 5-1Examples of Simplifying Operator Complements

10 PROGRAM 5-2 Comparative Operators ( 比較運算子 )

Two-Way Selection The decision is described to the computer as a conditional statement that can be answered either true or false. If the answer is true, one or more action statements are executed. If the answer is false, then a different action or set of actions is executed. if…else and Null else Statement Nested if Statements and Dangling else Problem Simplifying if Statements Conditional Expressions Topics discussed in this section:

12 FIGURE 5-7 if...else Logic Flow

13 Table 5-2Syntactical Rules for if…else Statements

14 FIGURE 5-8 A Simple if...else Statement

15 FIGURE 5-9 Compound Statements in an if...else

16 FIGURE 5-10 Complemented if...else Statements

17 FIGURE 5-11 A Null else Statement

18 FIGURE 5-12 A Null if Statement

19 PROGRAM 5-3Two-way Selection

20 FIGURE 5-13 Nested if Statements

21 PROGRAM 5-4Nested if Statements

22 FIGURE 5-14 Dangling else else is always paired with the most recent unpaired if. Note

23 FIGURE 5-15 Dangling else Solution

24 Table 5-3Simplifying the Condition

25 FIGURE 5-16 Conditional Expression

26 Table 5-4Examples of Marginal Tax Rates

27 FIGURE 5-17 Design for Calculate Taxes

28 FIGURE 5-18 Design for Program 5-5 (Part I)

29 FIGURE 5-18 Design for Program 5-5 (Part II)

30 FIGURE 5-18 Design for Program 5-5 (Part III)

31 PROGRAM 5-5 Calculate Taxes

32 所得總額 所得淨額 ( 可扣稅所得 ) 預扣稅額 ( 已繳稅額 ) 應繳、應退稅額 各級稅率 基本扣除額

33

Multiway Selection In addition to two-way selection, most programming languages provide another selection concept known as multiway selection. Multiway selection chooses among several alternatives. C has two different ways to implement multiway selection: the switch statement and else-if construct. The switch Statement The else-if Topics discussed in this section:

35 FIGURE 5-19 switch Decision Logic

36 FIGURE 5-20 switch Statement Syntax

37 FIGURE 5-21 switch Flow

38 PROGRAM 5-6Demonstrate the switch Statement

39 FIGURE 5-22 switch Results

40 FIGURE 5-23 A switch with break Statements

41 PROGRAM 5-7Multivalued case Statements

42 PROGRAM 5-8 Student Grading

43 FIGURE 5-24 The else-if Logic Design for Program 5-9

44 The else-if is an artificial C construct that is only used when 1. The selection variable is not an integral, and 2. The same variable is being tested in the expressions. Note

45 PROGRAM 5-9 Convert Score to Grade 2015/04/02

More Standard Functions One of the assets of the C language is its rich set of standard functions that make programming much easier. For example, C99 has two parallel but separate header files for manipulating characters: ctype.h and wctype.h. Standard Characters Functions A Classification Program Handling Major Errors Topics discussed in this section:

47 FIGURE 5-25 Classifications of the Character Type

48 Table 5-6Classifying Functions continued

49 Table 5-6Classifying Functions (continued) Conversion Functions Table 5-7

50 PROGRAM 5-10 Demonstrate Classification Functions