An Introduction to Programming with C++1 The Selection Structure Tutorial 6.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Programming with Microsoft Visual Basic th Edition
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Chapter 2 Review Questions
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
1.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
JavaScript, Third Edition
C++ for Engineers and Scientists Third Edition
Guide To UNIX Using Linux Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
Chapter Four The Selection Structure
CIS162AD - C# Decision Statements 04_decisions.ppt.
CIS Computer Programming Logic
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Using the selection structure (Unit 7) Visual Basic for Applications.
Chapter 12: How Long Can This Go On?
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Introduction to Programming with RAPTOR
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Tutorial 4: The Selection Structure 1 Tutorial 4 The Selection Structure.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++1 Void Functions Tutorial 5.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Random Functions Selection Structure Comparison Operators Logical Operator
An Introduction to Programming with C++1 More on the Selection Structure Tutorial 7.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Exam 1 Review Jeff has 100 students in his MIS120 class. He is giving a 50 question exam worth 100 points. The following commands are available to you.
More on the Selection Structure
Value-Returning Functions
The Selection Structure
Chapter 4: The Selection Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005 BASICS
Making Decisions in a Program
WEB PROGRAMMING JavaScript.
Objectives After studying this chapter, you should be able to:
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Selection Structures: Making Decisions
An Introduction to Programming with C++ Fifth Edition
Chapter 3: Selection Structures: Making Decisions
Chapter 5: The Selection Structure
Presentation transcript:

An Introduction to Programming with C++1 The Selection Structure Tutorial 6

An Introduction to Programming with C++2 Objectives Use the selection structure in a program Write pseudocode for the selection structure Create a flowchart for the selection structure Code the if and if/else forms of the selection structure Write code that uses comparison operators and logical operators Change the contents of a String variable to uppercase or lowercase Compare strings using the CompareTo() method Return a floating-point number when dividing two integers

An Introduction to Programming with C++3 Concept Lesson

An Introduction to Programming with C++4 Using the Selection Structure Selection structure/decision structure – allows program to make a decision or comparison and then select one of two paths, depending on the result of the comparison Condition –Specifies the decision you are making –Phrased so that it results in either a true or false answer

An Introduction to Programming with C++5 Selection Structures You Might Use Today

An Introduction to Programming with C++6 Including the Selection Structure in Pseudocode True path - the instructions following the condition False path –When using else, false path includes instructions between else and end if –When else is not used, processing continues after the end if

An Introduction to Programming with C++7 Including the Selection Structure in Pseudocode

An Introduction to Programming with C++8 Drawing a Flowchart of a Selection Structure Flowcharts - use standardized symbols to show steps the computer must take to accomplish program’s goal Diamond symbol (selection/repetition symbol) – used to represent both selection and repetition

An Introduction to Programming with C++9 Drawing a Flowchart of a Selection Structure

An Introduction to Programming with C++10 Coding the Selection Structure Items in square brackets ([]) in the syntax are optional You create a statement block by enclosing the statements in a set of braces ({}) Although it is not required to do so, it is a good programming practice to use a comment, such as //end if, to mark the end of each if statement

An Introduction to Programming with C++11 Syntax of the C++ if statement

An Introduction to Programming with C++12 Coding the Selection Structure

An Introduction to Programming with C++13 Comparison Operators Comparison (relational) operators - used to make comparisons in a C++ program Precedence numbers - indicate the order in which the computer performs the comparisons in a C++ expression Parentheses - used to override the order of precedence

An Introduction to Programming with C++14 Comparison Operators

An Introduction to Programming with C++15 Comparison Operators

An Introduction to Programming with C++16 Comparison Operators

An Introduction to Programming with C++17 Comparison Operators

An Introduction to Programming with C++18 Swapping Values Study closely the four instructions that swap the two values: –int temp = 0; creates and initializes a local variable named temp –temp = first; assigns the value contained in the first variable to the temp variable –first = second; assigns the value contained in the second variable to the first variable –second = temp; assigns the value contained in the temp variable to the second variable

An Introduction to Programming with C++19 Illustration of the Swapping Concept

An Introduction to Programming with C++20 Logical Operators Logical/Boolean operators - And and Or –Allow you to combine two or more conditions into one compound condition And logical operator - all of the conditions must be true for the compound condition to be true Or logical operator - only one of the conditions must be true for the compound condition to be true

An Introduction to Programming with C++21 Truth Tables for the And and Or Logical Operators

An Introduction to Programming with C++22 Logical Operators

An Introduction to Programming with C++23 Logical Operators

An Introduction to Programming with C++24 Using Logical Operators in a Program Data validation - the process of verifying that the input data is within the expected range

An Introduction to Programming with C++25 Comparing Strings String comparisons are case-sensitive –“Yes” is not the same as the string “YES” or “yes” ToUpper() method - converts a string to uppercase ToLower() method - converts a string to lowercase Member access operator (->) - used to access the members of a class CompareTo() method – used to compare strings

An Introduction to Programming with C++26 Comparing Strings

An Introduction to Programming with C++27 Using the ToUpper() and CompareTo() Methods in a Program ToUpper() method – converts the contents of the variable to uppercase CompareTo() method - tells the computer to compare the contents of the variable to each of the three state IDs

An Introduction to Programming with C++28 Comparing Strings

An Introduction to Programming with C++29 Application Lesson

An Introduction to Programming with C++30 Analyzing, Planning, and Desk- checking

An Introduction to Programming with C++31 Desk-checking Data for first desk-check –Total calories: 150 –Grams of fat: 6 Data for second desk-check –Total calories: 105 –Grams of fat: 2 Data for third desk-check –Total calories: 100 –Grams of fat: -3

An Introduction to Programming with C++32 Completed Desk-check Tables

An Introduction to Programming with C++33 Coding the main() Function main() function - requires four memory locations to store the values of its input and output items Use the int data type for variables that store: –total calories, grams of fat, and fat calories (these variables need to store whole numbers only) Use the double data type for the variable that stores: –fat percentage

An Introduction to Programming with C++34 Coding the main() Function

An Introduction to Programming with C++35 Coding the calcFatInfo() Function

An Introduction to Programming with C++36 Data and Completed Desk-check Table for the Program

An Introduction to Programming with C++37 Completing the Health Club Program To open the partially completed C++ program: 1.Start Microsoft Visual Studio.NET. If necessary, close the Start Page window 1.Click File on the menu bar, and then click Open Solution. The Open Solution dialog box opens 1.Locate and then open the CppNet\Tut06 \T6App Solution folder 1.Click T6App Solution (T6App Solution.sln) in the list of filenames, and then click the Open button 1.If the T6App.cpp source file is not displayed, right-click T6App.cpp in the Solution Explorer window, and then click Open

An Introduction to Programming with C++38 Summary –Selection structure As pseudocode In a flowchart As an if and if/else statement –Use comparison operators and logical operators –String operations Uppercase to lowercase Compare strings using the CompareTo() method –Return a floating-point number when dividing two integers