C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Slides:



Advertisements
Similar presentations
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Advertisements

Overview creating your own functions calling your own functions.
Computer Science A 3: 10/2. Logical Expressions Expressions that has a value which is either true or false. Logical expressions are essential in a program.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Logical Operators and Conditional statements
Chapter 2: Introduction to C++.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
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.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher Fundamental Data Types 09/09/13.
Computer Programming Fundamental Data Types Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons.
1 Chapter 9 Scope, Lifetime, and More on Functions.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
CPS120: Introduction to Computer Science
Chapter 02 (Part III) Introduction to C++ Programming.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
C Hints and Tips The preprocessor and other fun toys.
Fundamental Programming: Fundamental Programming Introduction to C++
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Decisions Bush decision making.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Bill Tucker Austin Community College COSC 1315
Decisions.
Data Types and Expressions
Working with Java.
Lecture 3: Operators, Expressions and Type Conversion
Conditionals & Boolean Expressions
Wednesday 09/23/13.
Elementary Programming (C++)
Anatomy of a Java Program
Fundamental Programming
Subject:Object oriented programming
Use a variable to store a value that you want to use at a later time
Lexical Elements & Operators
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13

C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved Get Back Exams Get back corrected exams. Other note: Program 4 input is m/s

C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved Objectives Indent to make if’s easier to read. Comparing floating point values. – Prevent round off error Comparing strings.

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Style guidelines Meaningful names Separate words with underscore or capital letters. feet_input, milesDriven Constants Give constants meaningful names ALL CAPS EARTH_MASS, SPEED_OF_LIGHT No “magic numbers” Not, e = m * pow( ,2.0)

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Style guidelines Documentation Use comments to explain your program. Not necessary for every line. Spacing At most, one statement per line. Blank lines included for clarity. Indentation

Block-structured code has the property that nested statements are indented by one or more levels. int main() { int floor;... if (floor > 13) { floor--; }... return 0; } Indentation level The if Statement – Indent when Nesting C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Using the tab key is a way to get this indentation but … not all tabs are the same width! Luckily most development environments have settings to automatically convert all tabs to spaces. The if Statement – Indent when Nesting C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Lexicographical Ordering of Strings Comparing strings uses “lexicographical” order to decide which is larger or smaller or if two strings are equal. “Dictionary order” is another way to think about “lexicographical” (and it’s a little bit easier to pronounce). string name = " Tom " ; if (name < " Dick " )... The test is false because “Dick” C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Lexicographical Ordering of Strings Comparing strings uses “lexicographical” order to decide which is larger or smaller or if two strings are equal. “Dictionary order” is another way to think about “lexicographical” (and it’s a little bit easier to pronounce). string name = " Tom " ; if (name < " Dick " )... The test is false because “Dick” would come before “Tom” if they were words in a dictionary. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Lexicographical Ordering of Strings All uppercase letters come before the lowercase letters. For example, "Z" comes before "a". The space character comes before all printable characters. Numbers come before letters. The punctuation marks are ordered but we won’t go into that now. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Lexicographical Ordering of Strings When comparing two strings, you compare the first letters of each word, then the second letters, and so on, until: –one of the strings ends –you find the first letter pair that doesn’t match. If one of the strings ends, the longer string is considered the “larger” one. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Lexicographical Ordering of Strings For example, compare "car" with "cart". c a r c a r t The first three letters match, and we reach the end of the first string – making it less than the second. Therefore "car" comes before "cart" in lexicographic ordering. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Lexicographical Ordering of Strings When you reach a mismatch, the string containing the “larger” character is considered “larger”. For example, let’s compare "cat" with "cart". c a t c a r t The first two letters match. Since t comes after r, the string "cat" comes after "cart“ in the lexicographic ordering. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Kinds of Error Messages There are two kinds of errors: Warnings Errors C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Kinds of Error Messages Error messages are fatal. –The compiler will not translate a program with one or more errors. Warning messages are advisory. –The compiler will translate the program, but there is a good chance that the program will not do what you expect it to do. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Exact Comparison of Floating-Point Numbers Roundoff errors Does== 2 ? Let’s see (by writing code, of course) … C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Exact Comparison of Floating-Point Numbers double r = sqrt(2.0); if (r * r == 2) { cout << "sqrt(2) squared is 2" << endl; } else { cout << "sqrt(2) squared is not 2 but " << setprecision(18) << r * r << endl; } This program displays: sqrt(2) squared is not 2 but roundoff error C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Exact Comparison of Floating-Point Numbers Roundoff errors – a solution Close enough will do. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Exact Comparison of Floating-Point Numbers It is common to set ε to 10 –14 when comparing double numbers: const double EPSILON = 1E-14; double r = sqrt(2.0); if (fabs(r * r - 2) < EPSILON) { cout << "sqrt(2) squared is approximately "; cout << r * r << endl; } Include the header to use sqrt and the fabs function which gives the absolute value. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved Write a Program Write a program to classify an input angle measure. Output either “right”, “obtuse” or “acute”. Consider round-off error might make a right angle slight more or less than 90. Set an epsilon value for the error tolerance. Use that when checking for equal to 90 degrees.