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

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

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

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

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

Chapter Two: Fundamental Data Types

LECTURE 3 C++ Basics Part 2

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Add Subtract Multiply Divide

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Basics of Java Programming

Variables and Operators 2/20/01 Lecture #

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

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

Information Science 1

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Chapter 3: Operators, Expressions and Type Conversion

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Unit 3. Operators. School of Science and Technology INTRODUCTION

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Arithmetic Expressions in C

Computer Programming CS F111

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Section we will not cover section 2.11 feel free to read it on your own

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

Chapter 2. C++ Basics

CIS133J. Working with Numbers in Java

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

CS102: Variables and Expressions

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

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

Chapter 4: Basic C Operators

FUNDAMENTAL DATA TYPES

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++

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

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

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Lesson 2 Variables and I/O

Operators. Java operators are classified into three categories:

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Elementary Programming

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.

CSCE 110 PROGRAMMING FUNDAMENTALS

Information Science 1

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

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

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Chapter 2: Using Data

UNIT- 3 Introduction to C++

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

Learning the Language - V

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

Programming. C++ Basics

Arithmetic. 2.2.l Basic Arithmetic Operations. 2.2 Arithmetic 37

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Chapter 2: Data and Expressions

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Maciej Sobieraj. Lecture 1

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

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

The C++ Language. Arizona State University 1

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

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

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

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

3. Java - Language Constructs I

Chapter 2: Data and Expressions

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

CS Kangmei Yang. Page 1

Visual C# Instructor s Manual Table of Contents

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

Chapter 2: Introduction to C++

Chapter 2. Outline. Simple C++ Programs

Programming in C++ 5. Integral data types

by Pearson Education, Inc. All Rights Reserved.

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Declaration and Memory

Fundamental of Programming (C)

Chapter 2: Overview of C++

Operators and Expressions:

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

Engineering Problem Solving with C++, Etter/Ingber

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

The Three Rules. Program. What is a Computer Program? 5/30/2018. Interpreted. Your First Program QuickStart 1. Chapter 1

Transcription:

Lesson 3: Arithmetic & Casting Pic 10A Ricardo Salazar

(2.4) Constants Sometimes we want a 'variable' that does not vary!? (OK that does not make sense... but how about a 'house' whose guest is always the same) We call such 'houses' constants. We can declare them with the reserved word const. The value of such constant WILL NEVER change. (The compiler will not let you change the value) The convention is to CAPITALIZE their names Examples: const double PI = 3.1416; const int NUMBER_OF_LESSONS = 27;

(2.5) Basic arithmetic Symbol Operation Example Value* + Addition 1 + 5; 6 - Subtraction 1 5; 4 * Multiplication 3 * 5; 15 / Division 7 / 3; 2 % Mod (residue) 7 % 3; 1 ++ Increment one int i=7; i++; /* ++i; also works */ 7 8 * some values make no sense, we will explain this later.

Assignment, not equality! Unlike math where the symbol = denotes equality, in C++ = is used to assign values. More precisely: = assigns the value on the right to the variable on the left. Example int count = 4; double total = 0.03; total = count * 0.05 + total;

Example: How much soda? A fridge contains soda in 2-liter bottles and 12-oz cans. How much soda there is? 2.0 is the volume (in liters) of a bottle, 0.355 is the volume of a can. The formula is correct but the code is not very clear!

Shortcuts From the previous program: soda = soda + userinput * BOTTLE_VOLUME; soda = soda + userinput * CAN_VOLUME; Such expressions are very common. To write less use soda += userinput * BOTTLE_VOLUME; soda += userinput * CAN_VOLUME; Some other shortcuts: a = 4; is the same as a = a 4; a *= 2; is the same as a = a * 2; a /= 3; is the same as a = a / 3; It looks weird but it does save time!

The 'increment' operator Later we'll use particular int variables called counters. They are used to count and are often increased or decreased by one. int currentlesson = 3; currentlesson += 1; // sets currentlesson=4 This operation is so common, it has a special syntax: int currentlesson = 3; currentlesson++; // sets currentlesson=4 There are 4 increment/decrement operators: x++; ++x; x--; --x; Does the name C++ make sense now?

Order of operations Multiplication ( * ) and division ( / ) take precedence over addition ( + ) and subtraction ( ). If you want to enforce a different precedence use parentheses. int m = 2+3*4; // stores the number 2 + 12 = 14 int n = (2+3)*4; // stores the number 6 * 4= 24 You can improve readability by using blank spaces. x = ( 1+2*(3 6)*(5 2)+3*2)/(2+2*(5 3)); x = ( 1 + 2 * (3 6) * (5 2 ) + 3 * 2 ) / ( 2 + 2 * ( 5 3 ) ); some exceptions include: the unary minus sign: b vs b functions: sqrt(16) vs sqrt (16)

Division and data types The result of a division depends on what type of values are being divided. If at least one number is decimal (double), the result is decimal, otherwise the result is an integer. E.g: double x = 5 / 3; // sets x = 1 double y = 5.0 / 3; // sets y = 1.66667 In the first case the result of the division is an integer. The decimal part is discarded and it is stored (as a double). In the second case the result is a decimal. Remember to use the.0 for decimals.

Static casts Also be careful with 'raw' data cout << 5/4; // shows 1 cout << 5.0/4; // shows 1.25 cout << 5/4.0; // shows 1.25 The compiler does not give an error for mixing types... some of them only WARN you that some information might get lost. If you are prepared for the consequences you can turn off the warning message with a static cast. Casts are also used to temporarily change the type of a variable.

Casts (cont) For older compilers use: (newtype) expression for newer ones use: static_cast<newtype>(expression) E.g: int a = 3, b = 10; double c = 2.5; int d = a / (int) c; // sets d = 3/2 = 1 /* int d = a / static_cast<int>(c); also works */ double e = (double) a / b; // sets e = 3.0/10 = 0.3 double f = a / b ; // sets f = 3/10 = 0 The changes are temporary: c doe not store 2 and a does not store 3.0

Casts (applications ) How can we round off a double to the nearest integer? Add 0.5 and then discard the decimal part. double x = 3.1; int a = static_cast<int>(x + 0.5); /* sets x=3, discards the decimal 0.6 */ double y = 1.7; int b = static_cast<int>(y + 0.5); /* sets y=2, discards the decimal 0.2 */ If c and d are integers and the quotient (decimal) is needed: double e = static_cast<double>(c) / d; // works double e = static_cast<double>( c/d ); // doesn't double e = 1.0 * c / d; // also works (avoid it)!

The mod (residue) operator % In elementary school we are taught that: 7 divided by 3 is 2 and leaves 1 as a remainder. 15 divided by 4 is 3 and leaves 3 as a remainder. This remainder is also called a residue. The operator % is used to compute the residue. int a = 4 % 2; // sets a = 0 int b = 7 % 3; // sets b = 1 Problem: Convert 145 days to months and days. int d = 135; int months = d / 30; // sets months = 4 int days = d % 30; // sets days = 15