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

Similar documents
Variables. Data Types.

Introduction to Programming

Basic Types, Variables, Literals, Constants

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

6.096 Introduction to C++ January (IAP) 2009

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Introduction to C++ Systems Programming

Programming. C++ Basics

Programming Fundamentals and Methodology. COMP104: C++ Basics

Programming with C++ Language

Tokens, Expressions and Control Structures

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

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

EEE145 Computer Programming

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Basics of C++ // my first program in C++ Hello World! #include <iostream> using namespace std; int main () { cout << "Hello World!

CSCI 123 Introduction to Programming Concepts in C++

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Chapter 15 - C++ As A "Better C"

Chapter 2 - Control Structures

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Compiler Construction. Lecture 10

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

IS 0020 Program Design and Software Tools

CS2141 Software Development using C/C++ C++ Basics

Fundamentals of Programming

Programming in C++ 4. The lexical basis of C++

C: How to Program. Week /Mar/05

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.

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Chapter 2 - Introduction to C Programming

EP241 Computer 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.

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Fundamental of Programming (C)

A First Program - Greeting.cpp

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

The C++ Language. Arizona State University 1

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

Introduction to C# Applications

Short Notes of CS201

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Chapter 2. Procedural Programming

CS201 - Introduction to Programming Glossary By

Programming and Data Structures

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

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

BITG 1233: Introduction to C++

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Creating a C++ Program

UEE1302 (1102) F10: Introduction to Computers and Programming

1. In C++, reserved words are the same as predefined identifiers. a. True

Chapter 1 & 2 Introduction to C Language

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

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

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Review of Important Topics in CS1600. Functions Arrays C-strings

A Fast Review of C Essentials Part I

Homework #3 CS2255 Fall 2012

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

CSc Introduction to Computing

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

Chapter 2 - Control Structures

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

CS3157: Advanced Programming. Outline

Introduction to Programming EC-105. Lecture 2

Interview Questions of C++

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Lecture 2 Tao Wang 1

Numbers. John Perry. Spring 2017

Declaration and Memory

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Absolute C++ Walter Savitch

Unit 3. Constants and Expressions

Fast Introduction to Object Oriented Programming and C++

Chapter 2 - Control Structures

C++_ MARKS 40 MIN

3. Java - Language Constructs I

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Fundamentals of Programming CS-110. Lecture 2

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

C++ INDEX. Introduction: Instructions for use. Basics of C++: Structure of a program Variables. Data Types. Constants Operators Basic Input/Output

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

Basics of Java Programming

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Presented By : Gaurav Juneja

Data types, variables, constants

EP578 Computing for Physicists

CHAPTER 3 Expressions, Functions, Output

.Net Technologies. Components of.net Framework

CSE 431S Type Checking. Washington University Spring 2013

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Chapter 1 INTRODUCTION

Transcription:

, ean, arithmetic s s on acters Comp Sci 1570 Introduction to C++

Outline s s on acters 1 2 3 4 s s on acters

Outline s s on acters 1 2 3 4 s s on acters

ASCII s s on acters

ASCII s s on acters

Type: acter s s on acters Allocation: 1 byte Range of values: a type variable can take on the values of the ASCII acter set. There are 256 acters in the set, including the digits, uppercase alphas, lower case alphas, punctuation, and many other special acters. Example declaration: c o n t i n u e R e s p o n s e = y ; See demo code.

Escape acters s s on acters

Outline s s on acters 1 2 3 4 s s on acters

Type: s s on acters Allocation: varies Example declaration: s t r i n g g r e e t i n g = h e l l o. s t r i n g name ; c i n >> name ; // suppose you type Bob Smith cout << name ; // o u t p u t s Bob ( not Smith ) Use the double quotes for variable initializations, while using the single quotes (or ticks) for acters. Though you can initialize a variable to any, even one that includes spaces, reading into a variable using a cin statement is different. If you use a cin statement to read a value into a variable, it will only read up to the first space and then stop. We ll show a solution later. See demo code.

Outline s s on acters 1 2 3 4 s s on acters

Type: Boolean s s on acters Allocation: 1 byte Possible values: true or false Example declaration: q u i t = f a l s e ; passed = true ; true and false are reserved, which means is that they (and many others) have special meanings to the compiler and cannot be used as identifiers such as variable names. variable can be used as a primitive if statement. See demo code.

Outline s s on acters 1 2 3 4 s s on acters

Constants s s on acters Declaring a variable as ant will tell the compiler not to allow it to be changed. A ant may NOT be on the left hand side of an assignment statement. f l o a t PI = 3. 1 4 1 5 9 ; double DISTORTION COEF = 5. 6 6 2 3 9 8 ; f l o a t TAX RATE =. 0 2 3 ; PI = 4 ; // NO, w i l l not c o m p i l e Benefits of CONSTANTS variables can be protected from inadvertent corruption. In addition, the variable name identifies them, taking the mystery out of an unidentifiable number. Perhaps a value in your program needs to be changed at some point in time. It is easy to change the value of a ant in one location rather than have to search perhaps thousands of lines of code to change all instances.

When to use ants s s on acters short TWO = 2 ; f l o a t val1, val2, a v e r a g e ; a v e r a g e = ( v a l 1 + v a l 2 ) / TWO; short TWO = 3 ; f l o a t val1, val2, val3, a v e r a g e ; a v e r a g e = ( v a l 1 + v a l 2 + v a l 3 )/TWO; short DIVISOR = 2 ; See demo code.

Outline s s on acters 1 2 3 4 s s on acters

Names not to use s s on acters There are that are used in the C++ language that should not use in any other way. Thus, you should be tempted to name a variable new, since that is a reserved word. Do not use any of the following for your own named objects: auto double float int short struct unsigned break continue else for long signed switch void case default enum goto register sizeof typedef volatile do extern if return static union while asm dynamic cast namespace reinterpret cast explicit new static cast typeid catch false try operator template typename class friend private this using cast inline public throw virtual delete mutable protected true w t and bitand compl not eq or eq xor eq and eq bitor not or xor cin endl INT MIN iomanip main npos std cout include INT MAX iostream MAX RAND NULL

Outline s s on acters 1 2 3 4 s s on acters

Outline s s on acters 1 2 3 4 s s on acters

s s on acters Symbol used for assignment of a value to a variable is = tax = income RATE; // RATE i s c o n s t RATE =. 0 5 ; // not a l l o w e d! 4 = income + 6 4 ; // not a l l o w e d! The compiler goes through these steps to execute: 1 Value of the rhs (right hand side) is determined. In this case, memory locations for income and RATE have to be read, and then those values multiplied. 2 Types of the rhs and lhs are determined. If the type of the lhs can be converted to the type of the rhs without losing information, then it is done. If information would be lost in doing so, a compiler warning is issued. 3 Value of the rhs is copied into the memory location of the lhs. Thus, the lhs MUST BE A MODIFIABLE MEMORY LOCATION. That is, lhs cannot be a ant.

Outline s s on acters 1 2 3 4 s s on acters

s s on acters +,,, /, % ( modular a r i t h m e t i c ) somevalue = num1 + num2 ; // a d d i t i o n somevalue = num1 num2 ; // s u b t r a c t i o n somevalue = num1 num2 ; // m u l t i p l i c a t i o n somevalue = num1 / num2 ; // d i v i s i o n somevalue = num1 % num2 ; // modulus

A=10; B=20; s s on acters

Division s s on acters i n t hour, minute ; hour = 1 1 ; minute = 5 9 ; cout << Number o f minutes s i n c e midnight : ; cout << hour 60 + minute << e n d l ; cout << F r a c t i o n o f the hour passed : ; cout << minute /60 << e n d l ; See code demo. What does this output?? More to come next class

Outline s s on acters 1 2 3 4 s s on acters

s s on acters Multiplication and division happen before addition and subtraction. So 2*3-1 yields 5, not 4, and 2/3-1 yields -1, not 1 (remember that in integer division 2/3 is 0). If the have the same they are evaluated from left to right. So in the expression minute*100/60, the multiplication happens first, yielding 5900/60, which in turn yields 98. If the operations had gone from right to left, the result would be 59*1 which is 59, which is wrong. Any time you want to override the rules of (or you are not sure what they are) you can use parentheses. Expressions in parentheses are evaluated first, so 2 * (3-1) is 4. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60, even though it doesn t change the result.

Associativity s s on acters To see how associativity works, consider the expression 2-3 - 4 The two are the same, so they have equal. Should the first subtraction operator be applied before the second, as in (2-3) - 4 (that is,-5), or rather is 2 - (3-4) (that is, 3) the correct interpretation? The former (-5) is the correct interpretation. We say that the subtraction operator is left associative, and the evaluation is left to right. This interpretation agrees with standard arithmetic rules. All binary except assignment are left associative.

Associativity s s on acters Assignment is an exception; it is right associative. To see why associativity is an issue with assignment, consider the statement w = x = y = z; This is legal C++ and is called chained assignment. Assignment can be used as both a statement and an expression. The statement x = 2; assigns the value 2 to the variable x. The expression x = 2 assigns the value 2 to the variable x and evaluates to the value that was assigned; that is, 2. Since assignment is right associative, the chained assignment example should be interpreted as w = (x = (y = z)); which behaves as follows: The expression y = z is evaluated first. z s value is assigned to y, and the value of the expression y = z is z s value.

s s on acters

Outline s s on acters 1 2 3 4 s s on acters

s on acters? s s on acters #i n clude <i o s t r e a m > #i n clude <s t r i n g > using namespace s t d ; i n t main ( ) { l e t t e r ; l e t t e r = a + 1 ; cout << l e t t e r << e n d l ; //?? cout << l e t t e r + b << e n d l ; //?? l e t t e r = a + b ; cout << l e t t e r << e n d l ; //?? } s t r i n g word ; word = h e l l o ; cout << word + world << e n d l ; //?? cout << word world << e n d l ; //?? cout << word world << e n d l ; //?? cout << word / world << e n d l ; //??