Add Subtract Multiply Divide

Similar documents
Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

C/C++ Programming Lecture 7 Name:

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Lecture 3 Tao Wang 1

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Chapter 4: Control Structures I (Selection)

Looping. Arizona State University 1

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Homework #3 CS2255 Fall 2012

Operators. Java operators are classified into three categories:

Unit 3. Operators. School of Science and Technology INTRODUCTION

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

LECTURE 3 C++ Basics Part 2

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

UNIT- 3 Introduction to C++

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

LAB 4.1 Relational Operators and the if Statement

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Computer Programming : C++

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Chapter 3 Structure of a C Program

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Week 4 Lecture 1. Expressions and Functions

C: How to Program. Week /Mar/05

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Introduction to Programming

Objectives. In this chapter, you will:

Object Oriented Programming with Java

Chapter 2. Outline. Simple C++ Programs

C++ Programming: From Problem Analysis to Program Design, Third Edition

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Programming. C++ Basics

Chapter 2 - Control Structures

From Pseudcode Algorithms directly to C++ programs

A First Program - Greeting.cpp

Increment and the While. Class 15

Chapter 2 - Introduction to C Programming

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Introduction to Programming using C++

CHAPTER 3 Expressions, Functions, Output

Operators & Expressions

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

REVIEW. The C++ Programming Language. CS 151 Review #2

by Pearson Education, Inc. All Rights Reserved. 2

Structured Program Development

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

Fundamental of Programming (C)

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

CS2255 HOMEWORK #1 Fall 2012

Theory of control structures

Engineering Problem Solving with C++, Etter/Ingber

Computer Science II Lecture 1 Introduction and Background

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Basics of Java Programming

C++ PROGRAMMING SKILLS Part 2 Programming Structures

In this chapter you will learn:

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

REPETITION CONTROL STRUCTURE LOGO

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

The C++ Language. Arizona State University 1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 5: Making Decisions

CSCI Wednesdays: 1:25-2:15 Keller Thursdays: 4:00-4:50 Akerman 211

Why Is Repetition Needed?

QUIZ: What value is stored in a after this

More Complex Versions of the if Statement. Class 13

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Chapter 1 Introduction to Computers and C++ Programming

Lecture 3 Operators MIT AITI

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

VARIABLES & ASSIGNMENTS

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Lab 2: Structured Program Development in C

Software Design & Programming I

Program Organization and Comments

Transcription:

ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient % Mod = gives the remainder for integer division 1

Division Integer Division 6 r 3 4 ) 27 ` Floating Point Division 6.75 4 ) 27 int Q1 = 27 / 4; // quotient = 6 int R1 = 27 % 4; // remainder = 3 double Q2 = 27.0 / 4.0; // quotient = 6.75 Data Type Mismatch int Q1 = 27 / 4; // int quotient = 6 int R1 = 27 % 4; // int remainder = 3 double Q2 = 27.0 / 4.0; // double quotient = 6.75 Watch out for an integer division being assigned to a double. You lose the digits past the decimal. double Q2 = 27 / 4; // double quotient = 6.0 // because 27/4 is an // integer division equal 6 2

Data Type Mismatch Watch out for an integer division being assigned to a double. You lose the digits past the decimal. double Q2 = 27 / 4; // double quotient = 6.0 // because 27/4 is an // integer division equal 6 If either the 27 is a double (27.0) or the 4 is a double (4.0), or both, the result would be 67.5 double Q2 = 27 / 4.0; // double quotient = 6.75 Data Type Mismatch Compilers will promote an integer to a double, but will either issue a Loss of Precision Warning or an Error when converting from a double to an integer. If only a warning is issued, the program will still compile and run, but you may not get the results you were expecting. int Q1 = 27.0 / 4; // double quotient = 6.75 is // converted to an integer 6 by // the assignment = statement 3

Standard Calculator Left to Right 2 * 3 + 4 * 5 2*3=6 6+4=10 10*5=50 Order of Operation Scientific Calculator Multiply & Divide before Add & Subtract 2 * 3 + 4 * 5 6 + 20 26 Order of Operation Multiply and Divide before Add and Subtract 2 * 3 + 4 * 5 6 + 20 26 Precedence ( ) Parentheses ( ) * / % Multiply, Divide, Mod + Add, Subtract = Assignment You can change the order of computation by using parentheses int x = 2 * ( 3 + 4 ) * 5 ; // answer is 70 // 2 * ( 3 + 4 ) * 5 2 * 7 * 5 70 int x = ( 2 * 3 + 4 ) * 5 ; // answer is 50 // ( 2 * 3 + 4 ) * 5 (6+4) * 5 20 * 5 50 4

Arithmetic Assignment Operators int a = 10; int b = 20; a = a + b; // now a = 30 int a = 10; int b = 20; a += b; // now a = 30 a += b; is the same as a = a + b; a = b; is the same as a = a b; a *= b; is the same as a = a * b; Arithmetic Assignment Operators a += b; is the same as a = a + b; a = b; is the same as a = a b; a *= b; is the same as a = a * b; a /= b; is the same as a = a / b; a %= b; is the same as a = a % b; Other arithmetic and logical operators work the same. 5

Auto Increment Auto Decrement The C language introduced the auto-increment and auto-decrement. The auto-increment adds one to a numeric variable. For example: int a = 5; a++; // after the statement, a equals 6 int b = 10; b--; // after the statement, b equals 9 Pre-Increment Post-Increment The increment (or decrement) can be applied either at the beginning of the statement or after everything else in the statement has executed int a = 5; int x = ++a + 10; // a = 6, x = 16; int b = 5; int x = b++ + 10; // b = 6, x = 15 The variable b does not get incremented to a 6 until the rest of the statement has executed. The value of b was still a 5 when 10 was added to get a 15. The 15 was stored into x before b was incremented. 6

The if Statement if / else A simple if statement if (score >= 90) cout << "Great work!" << endl; A simple if/else statement if (score >= 90) cout << "Great work!" << endl; else cout << "Work harder." << endl; 7

if / else A simple if statement if (score >= 90) cout << "Great work!" << endl; A simple if/else statement if (score >= 90) cout << "Great work!" << endl; else cout << "Work harder." << endl; if / else if / else if / else if (score >= 90) cout << "Grade is A" << endl; else if (score >= 80) cout << "Grade is B" << endl; else if (score >= 70) cout << "Grade is C" << endl; else if (score >= 60) cout << "Grade is D" << endl; else cout << "Grade is F" << endl; 8

if / else if / else if / else if (score >= 90) cout << "Grade is A" << endl; else if (score >= 80) cout << "Grade is B" << endl; else if (score >= 70) cout << "Grade is B" << endl; else if (score >= 60) cout << "Grade is D" << endl; else cout << "Grade is F" << endl; if / else if / else if / else if (score >= 90) cout << "Grade is A" << endl; else if (score >= 80) cout << "Grade is B" << endl; else if (score >= 70) cout << "Grade is B" << endl; else if (score >= 60) cout << "Grade is D" << endl; else cout << "Grade is F" << endl; 9

Compound if A compound if statement can have a block of code attached to the if statement by using the open and close curly braces. if (score >= 90) cout << "Great work!" << endl; cout << "Your grade is an A" << endl; The curly braces can even be used if there is only one statement attached to the if. Compound if A compound if statement can have a block of code attached to the if statement by using the open and close curly braces. if (score >= 90) cout << "Great work!" << endl; cout << "Your grade is an A" << endl; The curly braces can even be used if there is only one statement attached to the if. 10

Compound if / else if (score >= 70) cout << "Good work." << endl; cout << "You passed." << endl; else cout << "Try harder." << endl; cout << "You can do it." << endl; Programming Style The opening curly brace can be on the same line as the if if (time >= 2) cout << "Takes too long." << endl; cout << "Get some help." << endl; -------- or ----------- if (time >= 2) cout << "Takes too long." << endl; cout << "Get some help." << endl; 11

Simple Loops int i = 0; while ( i < 5 ) cout << i << endl; cout << "Done << endl; 0 1 2 3 4 Done int i = 0; do cout << i << endl; while ( i < 5 ) ; cout << "Done << endl; 0 1 2 3 4 Done Repetition Without Loops // Input and add three numbers int total = 0; int number; cout << "Enter a number: "; cin >> number; total += number; cout << "Enter a number: "; cin >> number; total += number; cout << "Enter a number: "; cin >> number; total += number; 12

Repetition With at Loop // Input and add three numbers int total = 0; int number; int = 0; while (i < 3) cout << "Enter a number: "; total += number; Simple Loops int i = 0; while ( i < 5 ) cout << i << endl; cout << "Done << endl; 0 1 2 3 4 Done int i = 0; do cout << i << endl; while ( i < 5 ) ; cout << "Done << endl; 0 1 2 3 4 Done The test for looping is done at the top of the loop. Code may not be executed if the loop condition fails. The test for looping is done at the bottom of the loop. Code will be executed at least one time. 13

Simple Loops int i = 10; while ( i < 5 ) cout << i << endl; cout << "Done << endl; Done int i = 10; do cout << i << endl; while ( i < 5 ) ; cout << "Done << endl; 10 Done The test for looping is done at the top of the loop. Code may not be executed if the loop condition fails. The test for looping is done at the bottom of the loop. Code will be executed at least one time. The Sum of Input Values int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter 14

Be Careful of the Semi-colon int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 2 3 4 5 5 times in the loop Done 15

Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 (at start of loop) value = 12 (from the user) total = 12 i++ = 1 (at end of loop) i<5 (y/n) y Loop? (y/n) y Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 value = 12 23 total = 12 35 i++ = 1 2 i<5 (y/n) y y Loop? (y/n) y y 16

Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 2 value = 12 23 9 total = 12 35 44 i++ = 1 2 3 i<5 (y/n) y y y Loop? (y/n) y y y Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 2 3 value = 12 23 9-6 total = 12 35 44 38 i++ = 1 2 3 4 i<5 (y/n) y y y y Loop? (y/n) y y y y 17

Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 2 3 4 value = 12 23 9-6 37 total = 12 35 44 38 75 i++ = 1 2 3 4 5 i<5 (y/n) y y y y n Loop? (y/n) y y y y n Sample Execution int value = 0; // value from keyboard int total = 0; // initialize the total int i = 0; // initialize loop counter while (i < 5) // get 5 values cout << "Enter a number: "; // prompt cin >> value; // input from keyboard total += value; // total = total + value; // increment loop counter i = 0 1 2 3 4 5 value = 12 23 9-6 37 total = 12 35 44 38 75 75 i++ = 1 2 3 4 5 i<5 (y/n) y y y y n Loop? (y/n) y y y y n 18

Counters Start from Zero It is most common to start counting from zero because the first index (position) in an array starts with zero. = 0 This may be confusing for most Americans who count the ground floor of a building as the first floor, but the rest of the world counts the first floor as the floor above ground. The End The End The do while loop will be covered later 19