Bill Tucker Austin Community College COSC 1315

Slides:



Advertisements
Similar presentations
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 2: Introduction to C++.
Basic Elements of C++ Chapter 2.
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.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Bill Tucker Austin Community College COSC 1315
Chapter # 2 Part 2 Programs And data
The Ohio State University
Chapter 1.2 Introduction to C++ Programming
Repetitive Structures
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 3 Control Statements
Chapter 1.2 Introduction to C++ Programming
Computing and Statistical Data Analysis Lecture 2
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
生查子 ~ 歐陽修 去年元夜時,花市燈如晝, 月上柳梢頭,人約黃昏後; 今年元夜時,月與燈依舊, 不見去年人,淚濕春衫袖。
Computing and Statistical Data Analysis Lecture 2
Selection (also known as Branching) Jumail Bin Taliba by
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
Computing Fundamentals
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
EGR 2261 Unit 4 Control Structures I: Selection
Variables A piece of memory set aside to store data
The Selection Structure
Introduction to C++ October 2, 2017.
Chapter 4: Making Decisions.
Bools & Ifs.
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
2.1 Parts of a C++ Program.
Additional Control Structures
Bools & Ifs.
Chapter 7 Conditional Statements
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 4: Control Structures I (Selection)
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Life is Full of Alternatives
Life is Full of Alternatives
Life is Full of Alternatives
Subject:Object oriented programming
Presentation transcript:

Bill Tucker Austin Community College COSC 1315 Chapter 4 Lecture Notes Bill Tucker Austin Community College COSC 1315 Copyright © 2002 W. A. Tucker Copyright © 2002 W. A. Tucker

Scope of Names All names have a scope that is either Global – the value stored in the memory location referred to by a global name is accessible for ALL of the program Local – the value stored in the memory location referred to by a local name is accessible ONLY for the function where the value is declared Copyright © 2002 W. A. Tucker

Example of Scope #include <iostream> using namespace std; double calcDistance (double); // prototype const double VELOCITY = 55; // global constant double height; // global variable int main() { double distance, time; time = 10; distance = calcDistance (time); cout << distance << endl; return 0; } double calcDistance (double time) double distance = 0; distance = VELOCITY * time; return distance; global constant OK global variable NONO local variables local variable NOTE: time and distance are local Copyright © 2002 W. A. Tucker

Tracing the values Values when the program is loaded GLOBAL main calcDistance VELOCITY 55.0 distance ??? Not loaded yet time ???t Both main and calcDistance have access to the global constant Copyright © 2002 W. A. Tucker

Tracing the values Values after execution of instruction “time = 10;” GLOBAL main calcDistance VELOCITY 55.0 distance ??? Not loaded yet time 10.0 Both main and calcDistance have access to the global constant Copyright © 2002 W. A. Tucker

Tracing the values Values after calling calcDistance (time); GLOBAL main calcDistance VELOCITY 55.0 distance ??? distance 0.0 time 10.0 time 10.0 Both main and calcDistance have access to the global constant Copyright © 2002 W. A. Tucker

Tracing the values Values after execution of instruction in calcDistance “distance = VELOCITY * time;” GLOBAL main calcDistance VELOCITY 55.0 distance ??? distance 550.0 time 10.0 time 10.0 Both main and calcDistance have access to the global constant Copyright © 2002 W. A. Tucker

Tracing the values Values after returning from calcDistance GLOBAL main calcDistance VELOCITY 55.0 distance 550.0 no longer loaded time 10.0 Both main and calcDistance have access to the global constant Copyright © 2002 W. A. Tucker

Style to Document Functions Since communication to and from a function is key, documentation is important Document the “contract” between the function and the calling function // PRE: preconditions – what is true when the function is called // POST: postconditions – what is true when the function finishes Copyright © 2002 W. A. Tucker

Example Documentation double calcDistance (double time) { // PRE: time contains a valid value // POST: returns a double value equal to the product of // time and VELOCITY double distance = 0; distance = VELOCITY * time; return distance; } // end calcDistance NOTE: The pre-conditions are the responsibility of the calling function The post-conditions are the responsibility of the new function Copyright © 2002 W. A. Tucker

Programming Structures Recall that we have only talked about SEQUENTIAL programming structures where each instruction is executed in sequence, each and every time the program is executed But what if we want to execute different instructions based upon the value of some variable that may change during execution? Copyright © 2002 W. A. Tucker

Selection Logic The SELECTION programming structure is used to alter the “flow of control” of instructions on a “real time” basis while the program is executing These decisions will be based upon the evaluation of a logical expression as either TRUE or FALSE Logical expressions can contain RELATIONAL or EQUALITY operators Copyright © 2002 W. A. Tucker

Relational Operators < Less than > Greater than <= Less than or equal to >= Greater than or equal to Which ones are compliments of each other ?? Copyright © 2002 W. A. Tucker

Equality Operators == Equal to != Not equal to Note these are compliments of each other Number one C++ programming error is to confuse equality operator (==) with assignment operator (=) Copyright © 2002 W. A. Tucker

Logical Expressions The data type of values that are compared should be the SAME or the compiler will make assumptions that you may not like! What is the value of the following logical expressions? Given: a = 24, b = 36, a and b are integers Expressions: a > b a == b a < b a != b a > = b a < = b Copyright © 2002 W. A. Tucker

Logical Operators There are three logical operators in C++ that are used to combine or modify logical expressions AND && OR || NOT ! Copyright © 2002 W. A. Tucker

AND Logical Operator expression1 && expression2 both expressions must be true for the combined expression to be true expression1 expression2 result true true true true false false false true false false false false Copyright © 2002 W. A. Tucker

OR Logical Operator expression1 || expression2 either expression must be true for the combined expression to be true expression1 expression2 result true true true true false true false true true false false false Copyright © 2002 W. A. Tucker

! (NOT) Operator ! expression after evaluating the “expression” the value of true or false is FLIPPED ! (a == b) is the same as (a != b) ! (condition1 && condition2) is the same as (condition1 || condition2) Copyright © 2002 W. A. Tucker

Logical Expression Examples What is the value (result) of the following logical expressions? Given: letter1 = ‘A’ letter2 = ‘a’ string1 = “Testing” string2 = “TESTING” value1 = 24.325 value2 = 34.555 bool raining = true letter1 == letter2 letter1 > letter2 string1 == string2 string1 > string2 value1 == value2 value1 > value2 raining ! raining Copyright © 2002 W. A. Tucker

More Examples What is the value (result) of the following logical expressions? Given: letter1 = ‘A’ letter2 = ‘a’ string1 = “Testing” string2 = “TESTING” value1 = 24.325 value2 = 34.555 bool raining = true (letter1 < letter2) && raining (string1 == string2) || ( value1 < value2) ((value1 == value2) || raining) && (value1 > value2) Copyright © 2002 W. A. Tucker

Selection Structure One of two possible alternatives is taken, depending upon a condition Condition? False True Alternative A Alternative B Copyright © 2002 W. A. Tucker

Selection Structure A selection structure may have only a single alternative - the true alternative Condition? True Alternative False Copyright © 2002 W. A. Tucker

if / else Statement The two alternative selection in C++ is coded by using an if / else statement false true if (a > b) largest = a; else largest = b; a > b ? largest = b largest = a NOTE: if and else are reserved words Copyright © 2002 W. A. Tucker

if Statement A single alternative selection is coded in C++ using the if statement largest = b largest = b; if (a > b) largest = a; a > b ? True largest = a False NOTE: if is a reserved word Copyright © 2002 W. A. Tucker

More on the if statement The if statement has two generic forms if (condition) a single statement or block of code; else Copyright © 2002 W. A. Tucker

Block of code A “block of code” is like a paragraph and is written inside a set of curly braces { } if (a > b) { largest = a; smallest = b; } // end if else largest = b; smallest = a; } // end else Copyright © 2002 W. A. Tucker

Nested if statements More than two alternatives may be coded by using “nested if statements” true Display “a is largest” a > b? if (a > b) cout << “a is largest”; else if (a < b) cout << “b is largest”; cout << “a = b”; false true false a < b? Display “b is largest” Display “a = b” Copyright © 2002 W. A. Tucker

Nested if statements More than two alternatives may be coded by using “nested if statements” false true a >= b? if (a >= b) if (a > b) cout << “a is largest”; else cout << “a = b”; cout << “b is largest”; Display “b is largest” false true a > b? Display “a = b” Display “a is largest” Copyright © 2002 W. A. Tucker

Common Errors An else always refers to the last if statement What is the output of the following code? (Try temp = 40, 50 and 60) if (temp < 60) if (temp <= 40) cout << “wear a jacket”; else cout << “no jacket required”; Does this make sense? What is wrong? Copyright © 2002 W. A. Tucker

Common Errors The following program fragment is supposed to print out the values 25, 60 and 8 in that order. Instead, it prints out 50, 60 and 4, why? length = 25; width = 60; if (length = 50) height = 4; else height = 8; cout << length << ‘ ‘ << width << ‘ ‘ << height << endl; Copyright © 2002 W. A. Tucker

In Class Exercise Write a function that will return a letter grade given an integer grade as input char convertGrade (int grade) { //PRE: grade is a positive value <= 100 //POST: returns a letter grade as follows: // 90 – 100 = A; 80 – 89 = B // 70 – 89 = C; 60 – 69 = D: <59 = F char letter; // PUT SELECTION LOGIC HERE return letter; } // convertGrade Copyright © 2002 W. A. Tucker

Common Algorithms Minimum (smallest) Maximum (largest) Swap Odd Even Average (will cover later) Copyright © 2002 W. A. Tucker