Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.

Slides:



Advertisements
Similar presentations
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Advertisements

Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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)
true (any other value but zero) false (zero) expression Statement 2
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
© 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 +,
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
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
© 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: 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 (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 4-6 Scope Scope determines the region of the program in which a defined object is visible.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
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.
Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide.
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
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.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
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.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
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.
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++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
AND.
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.
Functions Review.
Selection—Making Decisions
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Programming Fundamentals
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Structure of a C Program
Topics discussed in this section:
Topics discussed in this section:
Topics discussed in this section:
Topics discussed in this section:
Topics discussed in this section:
Topics discussed in this section:
Computer Programming Basics
Selection—Making Decisions
Topics discussed in this section:
Structural Program Development: If, If-Else
Presentation transcript:

Computer Science: A Structured Programming Approach Using C1 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

Computer Science: A Structured Programming Approach Using C2 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:

Computer Science: A Structured Programming Approach Using C3 FIGURE 5-1 true and false on the Arithmetic Scale

Computer Science: A Structured Programming Approach Using C4 FIGURE 5-2 Logical Operators Truth Table

Computer Science: A Structured Programming Approach Using C5 FIGURE 5-3 Short-circuit Methods for and /or

Computer Science: A Structured Programming Approach Using C6 PROGRAM 5-1Logical Expressions

Computer Science: A Structured Programming Approach Using C7 PROGRAM 5-1Logical Expressions

Computer Science: A Structured Programming Approach Using C8 FIGURE 5-4 Relational Operators

Computer Science: A Structured Programming Approach Using C9 FIGURE 5-5 Comparative Operator Complements

Computer Science: A Structured Programming Approach Using C10 Table 5-1Examples of Simplifying Operator Complements

Computer Science: A Structured Programming Approach Using C11 PROGRAM 5-2Comparative Operators

Computer Science: A Structured Programming Approach Using C12 PROGRAM 5-2Comparative Operators

Computer Science: A Structured Programming Approach Using C 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:

Computer Science: A Structured Programming Approach Using C14 FIGURE 5-6 Two-way Decision Logic

Computer Science: A Structured Programming Approach Using C15 FIGURE 5-7 if...else Logic Flow

Computer Science: A Structured Programming Approach Using C16 Table 5-2Syntactical Rules for if…else Statements

Computer Science: A Structured Programming Approach Using C17 FIGURE 5-8 A Simple if...else Statement

Computer Science: A Structured Programming Approach Using C18 FIGURE 5-9 Compound Statements in an if...else

Computer Science: A Structured Programming Approach Using C19 FIGURE 5-10 Complemented if...else Statements

Computer Science: A Structured Programming Approach Using C20 FIGURE 5-11 A Null else Statement

Computer Science: A Structured Programming Approach Using C21 FIGURE 5-12 A Null if Statement

Computer Science: A Structured Programming Approach Using C22 PROGRAM 5-3Two-way Selection

Computer Science: A Structured Programming Approach Using C23 PROGRAM 5-3Two-way Selection

Computer Science: A Structured Programming Approach Using C24 FIGURE 5-13 Nested if Statements

Computer Science: A Structured Programming Approach Using C25 PROGRAM 5-4Nested if Statements

Computer Science: A Structured Programming Approach Using C26 PROGRAM 5-4Nested if Statements

Computer Science: A Structured Programming Approach Using C27 else is always paired with the most recent unpaired if. Note

Computer Science: A Structured Programming Approach Using C28 FIGURE 5-14 Dangling else

Computer Science: A Structured Programming Approach Using C29 FIGURE 5-15 Dangling else Solution

Computer Science: A Structured Programming Approach Using C30 Table 5-3Simplifying the Condition

Computer Science: A Structured Programming Approach Using C31 FIGURE 5-16 Conditional Expression

Computer Science: A Structured Programming Approach Using C32 Table 5-4Examples of Marginal Tax Rates

Computer Science: A Structured Programming Approach Using C33 FIGURE 5-17 Design for Calculate Taxes

Computer Science: A Structured Programming Approach Using C34 FIGURE 5-18 Design for Program 5-5 (Part I)

Computer Science: A Structured Programming Approach Using C35 FIGURE 5-18 Design for Program 5-5 (Part II)

Computer Science: A Structured Programming Approach Using C36 FIGURE 5-18 Design for Program 5-5 (Part III)

Computer Science: A Structured Programming Approach Using C37 PROGRAM 5-5Calculate Taxes

Computer Science: A Structured Programming Approach Using C38 PROGRAM 5-5Calculate Taxes

Computer Science: A Structured Programming Approach Using C39 PROGRAM 5-5Calculate Taxes

Computer Science: A Structured Programming Approach Using C40 PROGRAM 5-5Calculate Taxes

Computer Science: A Structured Programming Approach Using C41 PROGRAM 5-5Calculate Taxes

Computer Science: A Structured Programming Approach Using C42 PROGRAM 5-5Calculate Taxes

Computer Science: A Structured Programming Approach Using C43 PROGRAM 5-5Calculate Taxes

Computer Science: A Structured Programming Approach Using C44 PROGRAM 5-5Calculate Taxes

Computer Science: A Structured Programming Approach Using C45 PROGRAM 5-5Calculate Taxes

Computer Science: A Structured Programming Approach Using C46 PROGRAM 5-5Calculate Taxes

Computer Science: A Structured Programming Approach Using C 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:

Computer Science: A Structured Programming Approach Using C48 FIGURE 5-19 switch Decision Logic

Computer Science: A Structured Programming Approach Using C49 FIGURE 5-20 switch Statement Syntax

Computer Science: A Structured Programming Approach Using C50 FIGURE 5-21 switch Flow

Computer Science: A Structured Programming Approach Using C51 PROGRAM 5-6Demonstrate the switch Statement

Computer Science: A Structured Programming Approach Using C52 FIGURE 5-22 switch Results

Computer Science: A Structured Programming Approach Using C53 FIGURE 5-23 A switch with break Statements

Computer Science: A Structured Programming Approach Using C54 PROGRAM 5-7Multivalued case Statements

Computer Science: A Structured Programming Approach Using C55 Table 5-5Summary of switch Statement Rules

Computer Science: A Structured Programming Approach Using C56 PROGRAM 5-8Student Grading

Computer Science: A Structured Programming Approach Using C57 PROGRAM 5-8Student Grading

Computer Science: A Structured Programming Approach Using C58 PROGRAM 5-8Student Grading

Computer Science: A Structured Programming Approach Using C59 FIGURE 5-24 The else-if Logic Design for Program 5-9

Computer Science: A Structured Programming Approach Using C60 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

Computer Science: A Structured Programming Approach Using C61 PROGRAM 5-9Convert Score to Grade

Computer Science: A Structured Programming Approach Using C62 PROGRAM 5-9Convert Score to Grade

Computer Science: A Structured Programming Approach Using C63 PROGRAM 5-9Convert Score to Grade

Computer Science: A Structured Programming Approach Using C 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:

Computer Science: A Structured Programming Approach Using C65 FIGURE 5-25 Classifications of the Character Type

Computer Science: A Structured Programming Approach Using C66 Table 5-6Classifying Functions continued

Computer Science: A Structured Programming Approach Using C67 Table 5-6Classifying Functions (continued)

Computer Science: A Structured Programming Approach Using C68 Table 5-7Conversion Functions

Computer Science: A Structured Programming Approach Using C69 PROGRAM 5-10Demonstrate Classification Functions

Computer Science: A Structured Programming Approach Using C70 PROGRAM 5-10Demonstrate Classification Functions

Computer Science: A Structured Programming Approach Using C Incremental Development Part II In Chapter 4, we introduced the concept of incremental development with a simple calculator program. We continue the discussion by adding a menu and calculator subfunctions. Calculator Design Calculator Incremental Design Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C72 FIGURE 5-26 Design for Menu-driven Calculator

Computer Science: A Structured Programming Approach Using C73 PROGRAM 5-11Menu-driven Calculator—First Increment

Computer Science: A Structured Programming Approach Using C74 PROGRAM 5-11Menu-driven Calculator—First Increment

Computer Science: A Structured Programming Approach Using C75 PROGRAM 5-11Menu-driven Calculator—First Increment

Computer Science: A Structured Programming Approach Using C76 PROGRAM 5-11Menu-driven Calculator—First Increment

Computer Science: A Structured Programming Approach Using C77 PROGRAM 5-12Menu-driven Calculator—Third Increment

Computer Science: A Structured Programming Approach Using C78 PROGRAM 5-12Menu-driven Calculator—Third Increment

Computer Science: A Structured Programming Approach Using C79 PROGRAM 5-12Menu-driven Calculator—Third Increment

Computer Science: A Structured Programming Approach Using C80 PROGRAM 5-12Menu-driven Calculator—Third Increment

Computer Science: A Structured Programming Approach Using C81 PROGRAM 5-12Menu-driven Calculator—Third Increment

Computer Science: A Structured Programming Approach Using C82 PROGRAM 5-12Menu-driven Calculator—Third Increment

Computer Science: A Structured Programming Approach Using C83 PROGRAM 5-12Menu-driven Calculator—Third Increment

Computer Science: A Structured Programming Approach Using C84 PROGRAM 5-13Menu-driven Calculator—Fifth Increment

Computer Science: A Structured Programming Approach Using C85 PROGRAM 5-13Menu-driven Calculator—Fifth Increment

Computer Science: A Structured Programming Approach Using C86 PROGRAM 5-13Menu-driven Calculator—Fifth Increment

Computer Science: A Structured Programming Approach Using C87 PROGRAM 5-13Menu-driven Calculator—Fifth Increment

Computer Science: A Structured Programming Approach Using C88 PROGRAM 5-13Menu-driven Calculator—Fifth Increment

Computer Science: A Structured Programming Approach Using C89 PROGRAM 5-13Menu-driven Calculator—Fifth Increment

Computer Science: A Structured Programming Approach Using C90 PROGRAM 5-13Menu-driven Calculator—Fifth Increment

Computer Science: A Structured Programming Approach Using C91 PROGRAM 5-13Menu-driven Calculator—Fifth Increment

Computer Science: A Structured Programming Approach Using C92 PROGRAM 5-13Menu-driven Calculator—Fifth Increment

Computer Science: A Structured Programming Approach Using C Software Engineering In this section, we discuss some software engineering issues related to decisions. Dependent Statements Negative Logic Rules for Selection Statements Selection in Structure Charts Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C94 PROGRAM 5-14Examples of Poor and Good Nesting Styles

Computer Science: A Structured Programming Approach Using C95 Table 5-8Indentation Rules

Computer Science: A Structured Programming Approach Using C96 Avoid compound negative statements! Note

Computer Science: A Structured Programming Approach Using C97 Table 5-9Complementing Expressions

Computer Science: A Structured Programming Approach Using C98 Table 5-10Selection Rules

Computer Science: A Structured Programming Approach Using C99 FIGURE 5-27 Structure Chart Symbols for Selection

Computer Science: A Structured Programming Approach Using C100 FIGURE 5-28 Multiway Selection in a Structure Chart