Module 3 Selection Structures 12/7/2019 CSE 1321 Module 3.

Slides:



Advertisements
Similar presentations
The Software Lifecycle. Example Problem: Update a Checkbook Write a program that allows the user to enter a starting balance, a transaction type, D or.
Advertisements

Object-Oriented Programming and Classes. OOP / Slide 2 Basic, built-in, pre-defined types : char, int, double, … Variables + operations on them int a,
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Software Engineering 1 (Chap. 1) Object-Centered Design.
The switch Statement Selection Revisited. Problem Using OCD, design and implement a program that allows the user to perform an arbitrary temperature conversion.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
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.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Previously Repetition Structures While, Do-While, For.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Branches and Program Design
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Chapter 4 Selection: the if-else and switch-case instructions.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Control Structures RepetitionorIterationorLooping Part I.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
Introduction to Computer Programming
Chapter 3 Selection Statements
Repetitive Structures
while Repetition Structure
Compound Condition Break , Continue Switch Statements in Java
Decisions Given hours worked and pay rate, calculate total pay
TEMPERATURE CONVERSION
Temperature: Comparing degrees Celsius (C) and degrees Fahrenheit (F)
C++ Arrays.
Decisions Given hours worked and pay rate, calculate total pay
Flow of Control October 16, 2017.
The switch statement: an alternative to writing a lot of conditionals
Selection Control Structure: Switch Case Statement
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Module 3 Selection Structures 2/19/2019 CSE 1321 Module 3.
Let’s all Repeat Together
Module 3 Selection Structures 4/3/2019 CSE 1321 Module 3.
Module 3 Selection Structures 4/4/2019 CSE 1321 Module 3.
Module 4 Loops and Repetition 4/4/2019 CSE 1321 Module 4.
Life is Full of Alternatives
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Objectives You should be able to describe: The while Statement
Module 4 Loops and Repetition 4/15/2019 CSE 1321 Module 4.
Fundamental Programming
Life is Full of Alternatives
Clear and Unclear Windows
Welcome back to Software Development!
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
CSE Module 1 A Programming Primer
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
CSE Module 1 A Programming Primer
Presentation transcript:

Module 3 Selection Structures 12/7/2019 CSE 1321 Module 3

Pseudocode - IF Statement BEGIN MAIN CREATE Fahrenheit ← 0, Celsius ← 0 PRINT “Enter Celsius temperature: ” READ user input Celsius ← user input Fahrenheit ← 9.0 / 5.0 * Celsius + 32 PRINT Fahrenheit IF (Fahrenheit >= 90) THEN PRINT “heat warning” ENDIF END MAIN Ps 12/7/2019 CSE 1321 Module 3

Example - if Statement double celsius = 0.0, fahrenheit = 0.0; cout << " What is the Celsius temperature ? "; cin >> celsius; fahrenheit = 9.0 / 5.0 * celsius + 32; cout << "The temperature is " << fahrenheit << " degrees Fahrenheit"; if (fahrenheit >= 90) { cout << "It is really hot out there!"; } 12/7/2019 CSE 1321 Module 3

Pseudocode – IF-ELSE Statement BEGIN MAIN CREATE Fahrenheit ← 0, Celsius ← 0 PRINT “Enter Celsius temperature: ” READ user input Celsius ← user input Fahrenheit ← 9.0 / 5.0 * Celsius + 32 PRINT Fahrenheit IF (Fahrenheit >= 90) THEN PRINT “heat warning” ELSE PRINT “there is no extreme heat” ENDIF END MAIN Ps 12/7/2019 CSE 1321 Module 3

Example – if-else (Code Snippet) double celsius = 0.0, fahrenheit = 0.0; cout << " What is the Celsius temperature ? "; cin >> celsius; fahrenheit = 9.0 / 5.0 * celsius + 32; cout << "The temperature is " << fahrenheit << " degrees Fahrenheit"; if (fahrenheit >= 90) { cout << "It is really hot out there!"; } else cout << "There is no extreme heat today."; 12/7/2019 CSE 1321 Module 3

Pseudocode – IF-ELSE-IF BEGIN MAIN CREATE Fahrenheit ← 0, Celsius ← 0 PRINT “Enter Celsius temperature: ” READ user input Celsius ← user input Fahrenheit ← 9.0 / 5.0 * Celsius + 32 PRINT Fahrenheit IF (Fahrenheit >= 90) THEN PRINT “heat warning” ELSE IF (Fahrenheit >= 80) THEN PRINT “it is warm, but there is no extreme heat” ELSE IF (Fahrenheit >= 70) THEN PRINT “the temperature is pleasant and suggest a picnic” ELSE PRINT “a suggestion to take a jacket” END IF END MAIN Ps 12/7/2019 CSE 1321 Module 3

Example – if-else-if double celsius = 0.0, fahrenheit = 0.0; cout << " What is the Celsius temperature ? "; cin >> celsius; fahrenheit = 9.0 / 5.0 * celsius + 32; cout << "The temperature is " << fahrenheit << " degrees Fahrenheit"; if (fahrenheit >= 90) { cout << "It is really hot out there!"; } else if (fahrenheit >= 80) cout << "It is very warm!"; else if (fahrenheit >= 70) { cout << "It is very pleasant!"; else { cout << "It is cool today"; 12/7/2019 CSE 1321 Module 3

Pseudocode – switch Statement // Read user input like before conditions ← compute using Fahrenheit variable / 10 PRINTLINE("It is in the " + conditions + "0's.") SWITCH (conditions) CASE 10: PRINT “stay inside”, BREAK CASE 9 : PRINT “be careful due to heat”, BREAK CASE 8 : PRINT “it is hot, but not extreme”, BREAK CASE 7 : PRINT “pack a picnic”, BREAK DEFAULT : PRINT “take a jacket”, BREAK ENDCASE Ps 12/7/2019 CSE 1321 Module 3

switch Example int fahrenheit = 90; int conditions = (int)fahrenheit / 10; cout << "It is in the " << conditions << "0's."; switch (conditions) { case 10: cout << "It’s hot! Stay inside!"; break; case 9: cout << "It is really hot out there!"; case 8: cout << "It is warm, but no extreme heat!"; case 7: cout << "It is very pleasant today!"; default: cout << "Take a jacket!"; } 12/7/2019 CSE 1321 Module 3