Selection—Making Decisions

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
true (any other value but zero) false (zero) expression Statement 2
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
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
Decision Structures and Boolean Logic
© 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.
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
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 2. Program Construction in Java. 2.4 Selection (decisions)
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.
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.
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 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.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Computer Science: A Structured Programming Approach Using C1 5-5 Incremental Development Part II In Chapter 4, we introduced the concept of incremental.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
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.
If/Else Statements.
Chapter 4: Control Structures I
CNG 140 C Programming (Lecture set 3)
‘C’ Programming Khalid Jamal.
Chapter 3 Control Statements
More on the Selection Structure
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 3: Decisions and Loops
Chapter 4: Control Structures I
Chapter 6 Conditionals.
Introduction to C++ Programming Language
Decisions Chapter 4.
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.
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
An Introduction to Programming with C++ Fifth Edition
Control Structures.
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Chapter 4: Control Structures I
Structure of a C Program
Topics discussed in this section:
Topics discussed in this section:
Topics discussed in this section:
Control Structures: Selection Statement
Topics discussed in this section:
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Week 3 – Program Control Structure
Topics discussed in this section:
Section 3.7 Switching Circuits
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Computer Programming Basics
Selection—Making Decisions
Topics discussed in this section:
Control Structures: Selection Statement
Control Structure.
ICS103: Programming in C 4: Selection Structures
Structural Program Development: If, If-Else
Chapter 6 Conditionals.
Presentation transcript:

Selection—Making Decisions Chapter 4 Selection—Making Decisions 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 Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 4-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. Topics discussed in this section: Logical Data in C Logical Operators Evaluating Logical Expressions Comparative Operators Computer Science: A Structured Programming Approach Using C

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

FIGURE 4-2 Logical Operators Truth Table Computer Science: A Structured Programming Approach Using C

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

PROGRAM 4-1 Logical Expressions Computer Science: A Structured Programming Approach Using C

PROGRAM 4-1 Logical Expressions Computer Science: A Structured Programming Approach Using C

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

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

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

Comparative Operators PROGRAM 4-2 Comparative Operators Computer Science: A Structured Programming Approach Using C

Comparative Operators PROGRAM 4-2 Comparative Operators Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 4-2 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. Topics discussed in this section: if…else and Null else Statement Nested if Statements and Dangling else Problem Simplifying if Statements Conditional Expressions Computer Science: A Structured Programming Approach Using C

FIGURE 4-6 Two-way Decision Logic Computer Science: A Structured Programming Approach Using C

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

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

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

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

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

FIGURE 4-11 A Null else Statement Computer Science: A Structured Programming Approach Using C

FIGURE 4-12 A Null if Statement Computer Science: A Structured Programming Approach Using C

PROGRAM 4-3 Two-way Selection Computer Science: A Structured Programming Approach Using C

PROGRAM 4-3 Two-way Selection Computer Science: A Structured Programming Approach Using C

FIGURE 4-13 Nested if Statements Computer Science: A Structured Programming Approach Using C

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

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

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

FIGURE 4-14 Dangling else Computer Science: A Structured Programming Approach Using C

FIGURE 4-15 Dangling else Solution Computer Science: A Structured Programming Approach Using C

Simplifying the Condition Table 4-3 Simplifying the Condition Computer Science: A Structured Programming Approach Using C

FIGURE 4-16 Conditional Expression Computer Science: A Structured Programming Approach Using C

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

FIGURE 4-17 Design for Calculate Taxes Computer Science: A Structured Programming Approach Using C

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

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

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

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

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

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

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

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

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

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

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

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

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

Topics discussed in this section: 4-3 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. Topics discussed in this section: The switch Statement The else-if Computer Science: A Structured Programming Approach Using C

FIGURE 4-19 switch Decision Logic Computer Science: A Structured Programming Approach Using C

FIGURE 4-20 switch Statement Syntax Computer Science: A Structured Programming Approach Using C

FIGURE 4-21 switch Flow Computer Science: A Structured Programming Approach Using C

Demonstrate the switch Statement PROGRAM 4-6 Demonstrate the switch Statement Computer Science: A Structured Programming Approach Using C

FIGURE 4-22 switch Results Computer Science: A Structured Programming Approach Using C

FIGURE 4-23 A switch with break Statements Computer Science: A Structured Programming Approach Using C

Multivalued case Statements PROGRAM 4-7 Multivalued case Statements Computer Science: A Structured Programming Approach Using C

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

PROGRAM 4-8 Student Grading Computer Science: A Structured Programming Approach Using C

PROGRAM 4-8 Student Grading Computer Science: A Structured Programming Approach Using C

PROGRAM 4-8 Student Grading Computer Science: A Structured Programming Approach Using C

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

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

PROGRAM 4-9 Convert Score to Grade Computer Science: A Structured Programming Approach Using C

PROGRAM 4-9 Convert Score to Grade Computer Science: A Structured Programming Approach Using C

PROGRAM 4-9 Convert Score to Grade Computer Science: A Structured Programming Approach Using C