Variables and numeric types

Similar documents
Exercise: Inventing Language

Variables. Data Types.

Chapter-8 DATA TYPES. Introduction. Variable:

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

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Chapter 2 Basic Elements of C++

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CSc Introduction to Computing

Computer Programming : C++

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

CS201 Some Important Definitions

Full file at

The C++ Language. Arizona State University 1

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

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

Lesson 2 Variables and I/O

Objectives. In this chapter, you will:

1/29/2018. Starting a Program Executes its main Function. ECE 220: Computer Systems & Programming. The Function main Divides into Two Parts

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

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

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

377 Student Guide to C++

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Lab # 02. Basic Elements of C++ _ Part1

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

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

printf( Please enter another number: ); scanf( %d, &num2);

BTE2313. Chapter 2: Introduction to C++ Programming

Lecture 3 Tao Wang 1

Lecture 2 Tao Wang 1

Types, Values, Variables & Assignment. EECS 211 Winter 2018

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

3. Simple Types, Variables, and Constants

UNIT- 3 Introduction to C++

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

Reserved Words and Identifiers

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

ACS-1903 Basic program structure Feb 16, 2017

Computer Components. Software{ User Programs. Operating System. Hardware

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

Chapter 12 Variables and Operators

CS 231 Data Structures and Algorithms, Fall 2016

6.096 Introduction to C++

Unit 3. Constants and Expressions

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Getting started with C++ (Part 2)

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Maciej Sobieraj. Lecture 1

Week 3 Lecture 2. Types Constants and Variables

Chapter 12 Variables and Operators

CA31-1K DIS. Pointers. TA: You Lu

Should you know scanf and printf?

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Creating a C++ Program

CS242 COMPUTER PROGRAMMING

How to approach a computational problem

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CS201 - Introduction to Programming Glossary By

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Syntax and Variables

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

10. Functions (Part 2)

Chapter 2 C++ Fundamentals

Pointers, Dynamic Data, and Reference Types

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

COMP-202: Foundations of Programming

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

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

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Memory Addressing, Binary, and Hexadecimal Review

Lecture Set 2: Starting Java

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Ex: If you use a program to record sales, you will want to remember data:

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Lecture Set 2: Starting Java

The PCAT Programming Language Reference Manual

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Bits and Bytes. Bit Operators in C/C++

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

6.096 Introduction to C++ January (IAP) 2009

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

More about Binary 9/6/2016

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

IT 1033: Fundamentals of Programming Data types & variables

12. Pointers Address-of operator (&)

Fundamentals of Programming CS-110. Lecture 2

Short Notes of CS201

Computer Skills (2) for Science and Engineering Students

An overview about DroidBasic For Android

Transcription:

s s and numeric types Comp Sci 1570 to C++ types

Outline s types 1 2 s 3 4 types 5 6

Outline s types 1 2 s 3 4 types 5 6

s types Most programs need to manipulate data: input values, output values, store values, modify values. You accomplish that in large part using variables. is a modifiable memory address with a name, alias, or identifier. Similar to variables in math to represent values that can change, but not entirely the same a = 5; b = 2; a = a + 1; result = a - b;

Outline s types 1 2 s 3 4 types 5 6

s s types Provides named storage that programs can manipulate Each variable in C++ has a specific type, which determines: size and layout of the variable s memory range of values that can be stored within that memory set of operations that can be applied to the variable

Outline s types 1 2 s 3 4 types 5 6

s types C++ programs create, access, manipulate, and destroy objects Object is a segment of memory that can be used to store values. can store information for later retrieval and manipulation When an object is defined, memory is set aside for the object. Many objects in C++ come in the form of variables.

Outline s types 1 2 s 3 4 types 5 6

Making new variables s types Statement such as x = 5; We are assigning the value of 5 to x. But what exactly is x? x is a variable. In order to create a variable, we generally use a special kind of declaration statement called a (more later) Here s an example of defining variable x as an eger variable (one that can hold eger values): type variable list; i n t x ; Integers can be written without a fractional component, such as -12, -1, 0, 4, or 27.

Making new variables s types Upon memory from RAM will be set aside (called instantiation). E.g., variable x is assigned memory location 140. One of the most common operations done with variables is. Use the operator, more commonly known as the = symbol. Later in our program, we could pr that value to the screen using std::cout: x = 5 ; // p r i n t s the v a l u e o f x ( memory l o c a t i o n 140) s t d : : cout << x ; Define variables before you use them, or get a compiler error

Outline s types 1 2 s 3 4 types 5 6

s types In C++, you must define the variables you are going to use before you use them. A declaration is a statement that announces an identifier (variable or function name) and its type. A actually implements or instantiates (causes memory to be allocated for) the identifier. Don t worry about this too much until we hit functions

Outline s types 1 2 s 3 4 types 5 6

s types C++ does not initialize most variables to a given value (such as zero) automatically upon When variables are defined, they have an undetermined value until they are first assigned a value variables without or are called an uninitialized variables After a variable is defined, a value may be assigned to it via the operator (the = sign): C++ will let you both define a variable AND give it an initial value in the same step. This is called. i n t x ; // t h i s i s a v a r i a b l e d e f i n i t i o n x = 5 ; // a s s i g n the v a l u e 5 to v a r i a b l e x i n t x = 5 ; // i n i t i a l i z e v a r i a b l e x with 5

Uninitialized variables s types #i n clude <i o s t r e a m > i n t main ( ) { // d e f i n e an i n t e g e r v a r i a b l e named x // t h i s v a r i a b l e i s u n i n i t i a l i z e d i n t x ; // p r i n t the v a l u e o f x to the s c r e e n // dangerous, because x i s u n i n i t i a l i z e d s t d : : cout << x ; } return 0 ; Execute

Uninitialized variables s types The computer will assign some unused memory to x. It will then send the value residing in that memory location to std::cout, which will pr the value. But what value will it pr? The answer is who knows!, and the answer may change every time you run the program. When a variable is assigned a memory location by the compiler, the default value of that variable is whatever (garbage) value happens to already be in that memory location!

Initialize your variables where possible s types A good rule of thumb is to initialize your variables at. This ensures that your variable will always have a consistent value, making it easier to debug if something goes wrong somewhere else.

Several types of s types C-like (because it is inherited from the C language), consists of appending an equal sign followed by the value to which the variable is initialized: type identifier = initial value; For example, declare a variable of type called x and initialize it to a value of zero from the same moment it is declared, we can write: i n t x = 0 ; Constructor (roduced by the C++ language), encloses the initial value between parentheses (()): typeidentifier(initial value); i n t x ( 0 ) ;

Initialization of variables s types #i n clude <i o s t r e a m > using namespace s t d ; i n t main ( ) { i n t a=5; // i n i t i a l v a l u e : 5 i n t b ( 3 ) ; // i n i t i a l v a l u e : 3 i n t c {2}; // i n i t i a l v a l u e : 2. C++ 11 o n l y! i n t r e s u l t ; // i n i t i a l v a l u e undetermined a = a + b ; r e s u l t = a c ; cout << r e s u l t ; } return 0 ;

Initialization of variables s types #i n clude <i o s t r e a m > i n t main ( ) { // d e c l a r i n g v a r i a b l e s : i n t a, b, c ; i n t r e s u l t = 4, u s e l e s s P r o g r a m =2; // Assignment a = 5 ; b = 2 ; a = b + u s e l e s s P r o g r a m ; s t d : : cout << a << and << r e s u l t ; } return 0 ;

Outline s types 1 2 s 3 4 types 5 6

Bits and bytes: how many combinations? s types

Binary and decimal s types

Bits and bytes s types 1 bit is the smallest memory storage unit. It can be either 1 or 0. 8 bits is one byte. 2 bytes is 16 bits. Assuming only non-negative numbers, 2 bytes will store [0 to 2 16 1] (the -1 is because we have to store 0) However, we also want to have negative numbers, so the range of a 2-byte value is what?

Outline s types 1 2 s 3 4 types 5 6

types s types What is different about storing a simple eger vs. large ing-po number? Each variable declaration must be given a datatype, on which the memory assigned to the variable depends. Values of variables are stored somewhere in an unspecified location in the computer memory as zeros and ones. C++ is a strongly-typed language, and requires every variable to be declared with its type before its first use. This informs the compiler the size to reserve in memory for the variable and how to erpret its value. Syntax for variable type is: type variablename (i.e., its identifier)

Outline s types 1 2 s 3 4 types 5 6

Overview of types s types

Type syntax abbreviations s types

Outline s types 1 2 s 3 4 types 5 6

signed s types Memory allocation: 2 Bytes Range of values: 2 15 to +2 15 1 or about -32 K to 32 K s h o e S i z e ; s h o e S i z e = 5 ; Note: Since s can vary from 2 to 4 bytes, it s best not to use them. However, they are indeed very popular. But, suppose you declare a variable as an, Then transfer that program over to another system using a different allocation. Your program may indeed crash!

signed s types Memory allocation: varies. some systems give 2 bytes, some give 4. The system at S&T allocates 4 bytes. Range of values: 2 31 to +2 31 1 or about -2 billion to 2 billion i n t numcarsonhwy ;

s types type: ing po allocation: 4 bytes precision: you get 6 significant figures (decimal) type: double precision allocation: 8 bytes precision: you get 15 significant figures (decimal) type: long double allocation: non-standard precision: lots! f l o a t s h o e S i z e = 6. 5 ; double w e i g h t o f m o u n t a i n = 746538433.55; long double e l e c t r o n w t = 0.00000000000000432;

Outline s types 1 2 s 3 4 types 5 6

Type syntax abbreviations s types

Type ranges s types

Get the size of a type s types #i n clude <i o s t r e a m > using namespace s t d ; i n t main ( ) { cout << S i z e o f : ; cout << i n t= << s i z e o f ( i n t ) ; cout <<, s h o r t i n t= << s i z e o f ( i n t ) ; cout <<, l o n g i n t= << s i z e o f ( long i n t ) ; cout <<, f l o a t= << s i z e o f ( f l o a t ) ; cout <<, double= << s i z e o f ( double ) ; cout <<, l o n g l o n g= << s i z e o f ( long long ) ; return 0 ; }

Outline s types 1 2 s 3 4 types 5 6

s types A valid identifier is a sequence of one or more letters, digits, or underscore characters ( ). No spaces, punctuation marks, and symbols Identifiers usually begin with a letter, and never a numeric character Can begin with an ( ), but considered reserved for compiler-specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere. C++ case sensitive RESULT variable is not the same as the result variable or the Result variable. These are three different identifiers identifying three different variables.

Examples of variable names s types legal names: x, y, xyz Hello, bob all adhere to the rules illegal names: 8Hello, hi-there, Go! starts with numeric, special character, special character

Naming practice s types Name variables meaningfully for your own benefit (the compiler does not care) Instead of: i and j, name variables like: quantity age, tirepressure, lateral distortion, etc. These examples demonstrate readability; you know immediately what they represent. Several ways to include two words in one identifier.

Comments s types It is also nice to put comments in your variable declarations to give the reader some idea what the variables will be used for. For example... // count o f e x p e r i m e n t n u m P a r t i c i p a n t s ; // u s e r r e s p o n s e char answer ;

Outline s types 1 2 s 3 4 types 5 6

Put variables in main s types For now, it is easiest to declare variables at the top (inside) of the main function. For now, DO NOT declare variables outside of the main function. This has the effect of creating dangerous situations which I will discuss later.