Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Chapter 2: Introduction to C++.
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Basic Elements of C++ Chapter 2.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA.
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.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Bill Tucker Austin Community College COSC 1315
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Data Types and Expressions
Data Types and Expressions
Chapter 2: Introduction to C++
Data Types, Variables & Arithmetic
Computing and Statistical Data Analysis Lecture 2
Computing Fundamentals
Basic Elements of C++.
Assignment and Arithmetic expressions
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Multiple Files Revisited
Chapter 2 Elementary Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2 Edited by JJ Shepherd
C++ Data Types Data Type
Expressions and Assignment
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 3 Operators and Expressions
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Primitive Types and Expressions
Chapter 2 Variables.
Chapter 2: Overview of C++
Presentation transcript:

Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what identifiers are legal? what are C-style, Camel Back-style identifiers? what is variable type? what types have we studied? what is variable declaration? where (in the program) is a variable declaration placed? what is an assignment? what is a stream? input stream? output stream? cout? cin? what is an extraction/insertion operator? what is an escape sequence? what is an input token?

Types, Expressions, More on Assignment

int, double and Others type is the kind of data that is stored in variables int - whole numbers double – numbers with fractions (called floating point) since the storage is limited, fraction can contain only a limited number of digits (usually up to 14) two ways to write a double number in C++ regular: 2.0 -3.23 +0.0456 .45 dot needs to be there scientific or floating point (explicitly states mantissa and exponent): 5.89e5 .045E-4 5e+5 dot does not need to be there mantissa is limited in size. the largest allowable number differs for every architecture. Usually: int - up to 32767 double - up to 10308 int or double (or any other type in C++) cannot contain a comma other possible types are short, float and long double in this course, use int

Character Type // asks for initials and outputs greeting a variable stores a single character, e.g. ’a’, ’A’, ’%’, ’1’ declared char varName; note that ’1’is also a character, not the same thing as numeric type 1 note also that ”1” is a string, not a character // asks for initials and outputs greeting #include <iostream> main (){ char first, second; cout << ”Enter your initials: ”; cin >> first >> second; cout << ”Hello ” << first << second << endl; cout << ”pleased to meet you\n”; }

bool Type bool (short for boolean) is used for branching and looping statements a boolean variable can have only two values true or false bool result; result = true; true and false are keywords and cannot be used as identifiers

Literal Constants an explicitly stated value is a literal constant examples: 23 34.4 ’a’ true a literal constant has value and type what are the types of the above constants?

Named Constants there are problems with using literal constants 9.8 does not give an idea as to what it means in the program hard to modify if used in multiple places in program named constant gives a name to a value; have to be declared: const int windowCount = 5; const double taxRate = 9.8; style: use named constants rather than literal constants use constants rather than variables

Type Compatibility as a rule you cannot store a value of one type in a variable of another type trying to do it leads to type mismatch int intvar; intvar = 2.99; compiler prints this: warning: assignment to ’int' from ’double' but still compiles the program discarding the fractional part it is usually a bad idea (but some programmers do it) to store char values in variables of type int, bools can also be in int. Even though you compiler allows it, it obscures the meaning of the variables and should be avoided

Expressions expression is a mechanism of calculating new values expression (similar to variable and constant) has type and value simplest expression literal constant named constant complex expression consists of operands joined by operators operand – (sub) expression operator – computes new values based on operands arity - number of operands the operator uses binary operator – two operands unary operator – one operand

Binary Integer Operators Operator Example addition + 2+3 a+4 b+1 subtraction - count-2 4-7 multiplication * 5*6 width*height division / 12/3 4/5 remainder % 10%3 23%4 for positive integers: if the integer division is not even, then the fractional part of the result is discarded: 12/5 produces 2 note that the fractional part is discarded and the result is never rounded up: 11/3 which should be (3.6666…) produces 3 not 4 the remainder can be used to “catch” the “missing” fraction 12%5 produces 2

Binary Double/Mixed Operators Operator Example addition + 2.3 + 3.4 subtraction - 2.45 - 1.3 multiplication * 5.4*2.3 division / 12.4 / 5.0 there is no remainder operator with floating point if there are integer and floating-point operands then the integers are first converted (by compiler) to floating-point operands and then the expression is evaluated: 45.34 * 2 is converted to 45.34 * 2.0

Unary Operators, Precedence precedence (order of execution) follows mathematical conventions: 1. Unary +, - 2. Binary *, /, % 3. Binary - and + you can use () to change precedence: (2+3)*2 changes default precedence 2 / 3 + 5 is equivalent to (2 / 3) + 5 -8 * 4 (-8) * 4 8 + 7 % 4 8 + (7 % 4)

Whole Numbers in Division when you divide int by int the result is int. It may be problematic in expression and the problem is hard to spot the compiler would not complain this program converts feet into miles. Is there anything wrong with it? double totalPrice; int feet; cin >> feet; totalPrice = 5000 * (feet/5280);

Assignment Conversions if double expression is assigned to an integer variable, its fractional part is dropped if int expression is assigned to a double variable, the expression is converted to double with zero fractional part consider double y = 2.7; int i = 15; int j = 10; i = y; // i is now 2 cout << i << endl; y = j; // y is now 10.0 cout << y << endl;

Compound Assignment compound assignment - joins assignment with another operator syntax: variable operator = expression; operator takes variable and expression as operands computes the value, assigns it to variable can be used for different types: int and double shorthand for regular assignment examples: int i = 3; i += 4; // equivalent to i=i+4; cout << i << endl; double a = 3.2; a *= 2.0; // equivalent to a=a*2.0; cout << a << endl; examples of other compound assignments: time /= rush_factor; change %=100; amount *= cnt1 + cnt2; // what is this equivalent to?

Increment and Decrement increment – increase value by one, unary operator: ++ decrement – decrease value by one, unary operator: -- can be used as a standalone statement ++k; or --k; in an expression a = ++k + 5; or a = --k + 5; two forms: prefix – preceding operand, and postfix/suffix – following operand both forms can be used in standalone statements and expressions no difference in standalone statements (stylistic preference) in expressions, in prefix form, the operation applies before value used in expression int k=5; a = ++k+5; // a is 11, k is 6 in suffix form, the operation applies after value is used in expression a = k++ +5; // a is 10, k is 6 modern style: avoid postfix form (may be inefficient for complex types)

Increment and Decrement Example int k; k=4; ++k; // k is 5 k++; // k is 6 cout << k << endl; int i; i= k++; // i is 6, k is 7 cout << i << " " << k << endl; int j; j= ++k; // j is 8, k is 8 cout << j << " " << k << endl;

Initialization any declared variable contains a value what is wrong with this code? int desiredNumber; desiredNumber=desiredNumber+5; initialization – assigning a value to the variable at its declaration two forms: primary int count=0, limit=10; double distance=5.723, pi=3.14; double step=limit/2.0; alternative int count(0), limit(10); double distance(5.723), pi(3.14); double step(limit/2.0);