3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)

Slides:



Advertisements
Similar presentations
Types and Variables. Computer Programming 2 C++ in one page!
Advertisements

1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4) 10 September.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
Basic Elements of C++ Chapter 2.
Programming Introduction to C++.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Week 1 Algorithmization and Programming Languages.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CSC 107 – Programming For Science. Announcements.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
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
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter # 2 Part 2 Programs And data
Variables, Identifiers, Assignments, Input/Output
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Computing and Statistical Data Analysis Lecture 2
Documentation Need to have documentation in all programs
Computing Fundamentals
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Basic Elements of C++.
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
CS111 Computer Programming
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
Programming Introduction to C++.
What Actions Do We Have Part 1
C++ Programming Basics
Programming Fundamental-1
Presentation transcript:

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C Variables and Constants

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 3 A variable is actually a place to store information in a computer. It is a location (or series of locations) in the memory. The name of a variable can be considered as a label of that piece of memory Address Memory Variables char a int b short int c bool d 100A213A Variables and Memory One address for one byte.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 4 In memory, all data are groups of ‘1’ and ‘0’, byte by byte. Depending on how we interpret the data, different types of variables can be identified in the memory. Size of Variables Type bool unsigned short int short int unsigned long int long int unsigned int int char float double Size 1 byte 2 bytes 4 bytes 2, 4 bytes 1 byte 4 bytes 8 bytes Values true or false (1 or 0) 0 to 65, ,768 to 32,767 0 to 4,294,967,295 -2,147,483,648 to 2,147,483, character values (+/-)1.2e  38 to (+/-)3.4e38 (+/-)2.2e  308 to (+/-)1.8e308 p.8 p.9

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 5 #include using namespace std; int main() { cout << "The size of an int is:\t\t" << sizeof(int) << " bytes.\n"; cout << "The size of a short int is:\t" << sizeof(short) << " bytes.\n"; cout << "The size of a long int is:\t" << sizeof(long) << " bytes.\n"; cout << "The size of a char is:\t\t" << sizeof(char) << " bytes.\n"; cout << "The size of a float is:\t\t" << sizeof(float) << " bytes.\n"; cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n"; return 0; } Exercise 3.2a a.Build the project and note the results. b.Add lines for bool, unsigned int, unsigned long int, and unsigned int.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 6 Before we use a variable, we need to declare its type, so that the compiler can reserve suitable memory space for it. Variables are declared in this way: It defines myVariable to be an integer. Hence 4 bytes will be reserved for its storage in memory. The name of the variable is case sensitive, hence myVariable is NOT the same as myvariable. Declaring Variables int myVariable;

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 7 We can declare one or more variables of the same kind at once It defines myVar1, myVar2, yourVar1, yourVar2 are all integers. Some names are keywords of the C++ language that cannot be used as variable names. e.g. return, while, for, if, etc. We can even initialize the value of the variables when making the declaration. Declaring Variables int myVar1, myVar2, yourVar1, yourVar2; int myVar1 = 10, myVar2, yourVar1, yourVar2 = 5;

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 8 #include using namespace std; int main() {unsigned short int smallNumber; smallNumber = 65535; // = cout << "small number:" << smallNumber << endl; smallNumber++; // = 0 cout << "small number:" << smallNumber << endl; smallNumber++; // = 1 cout << "small number:" << smallNumber << endl; return 0; } Maximum and Minimum Each data type has its own maximum and minimum limits. When the value goes beyond those limits, unexpected behavior may result. p.4

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 9 #include using namespace std; int main() { short int smallNumber; smallNumber = 32767; // = cout << "small number:" << smallNumber << endl; smallNumber++; // = cout << "small number:" << smallNumber << endl; smallNumber++; // = cout << "small number:" << smallNumber << endl; return 0; } Maximum and Minimum Signed integers do not behave in the same way. p.4

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 10 #include using namespace std; int main() {// ASCII code of 5 is 53 char c = 53, d = '5'; // They are the same int e = 53; cout << "c = " << c << endl; // c = 5 cout << "d = " << d << endl; // d = 5 cout << "e = " << e << endl; // e = 53 return 0; } Characters Besides numbers, C++ has also a data type called char, i.e. character. If a variable is declared as char, the compiler will consider the variable as an 8-bit ASCII character.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 11 ASCII Table : 6565 A6 B 6767 C 6868 D 6969 E 7070 F 7171 G 7272 H 7373 I 7474 J : 9797 a 9898 b9 c d e f g h i j Details of the table can be found in many places, for instance,

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 12 Special Printing Characters C++ compiler recognizes some special characters for formatting. Start with an escape character \ (e.g. \n means new line). Character \n \t \b \" \' \? \\ What it Means new line tab backspace double quote single quote question mark backslash escape sequence

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 13 Constants Unlike variable, constants cannot be changed. The value of a constant will be fixed until the end of the program. There are two types of constants: Literal Constants - come with the language e.g. a number 39 (you cannot assign another value to 39. ) Symbolic Constants - Like variables, users define a special name as a label for it. Unlike variables, it cannot be changed once it is initialized.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 14 A symbolic constant can be defined in two ways. 1. Old way - use the preprocessor command #define No type needs to be defined for studentPerClass. Preprocessor just replaces the word studentPerClass with 87 whenever it is found in the program. 2. A better way - use the keyword const Variable studentPerClass now has a fixed value 87. It has a type now. This feature helps the compiler to debug the program. Defining Symbolic Constants #define studentPerClass 87 const unsigned short int studentPerClass = 87 ;

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 15 Why do we need Constants? Help debugging the program Compiler will automatically check if a constant is modified by the program later (and reports it if modified.) Improve readability Give the value a more meaningful name. e.g. rather than writing 360, can use degreeInACircle Easy modification If a constant really needs to be changed, only need to change a single line in the source program.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 16 # include using namespace std; int main() {const int totalStudentNumber = 87; // Student no. must < 87 int num; cout << "Enter a student number: "; cin >> num; if (num > totalStudentNumber) cout << "\n Number not valid!!!\n"; : cout << "\n There are " << totalStudentNumber << " students in this class\n"; : return 0; } Give better readability Modify once and apply to whole program Give better readability Modify once and apply to whole program

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 17 Enumerated constants allow one to create a new type that contains a number of constants Makes COLOR the name of the new type. Set RED = 0, BLUE = 1, GREEN = 2, WHITE = 3, BLACK = 4 Another example Makes COLOR2 the name of the new type. Set RED = 100, BLUE = 101, GREEN = 500, WHITE = 501, BLACK = 700 Enumerated Constants enum COLOR {RED, BLUE, GREEN, WHITE, BLACK}; enum COLOR2 {RED=100, BLUE, GREEN=500, WHITE, BLACK=700};

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 18 # include using namespace std; int main() {const int Sunday = 0; const int Monday = 1; const int Tuesday = 2; const int Wednesday = 3; const int Thursday = 4; const int Friday = 5; const int Saturday = 6; int choice; cout << "Enter a day (0-6): "; cin >> choice; if (choice == Sunday || choice == Saturday) cout << "\nHave a nice weekend!\n"; else cout << "\nToday is not holiday yet.\n"; return 0; } Exercise 3.2b a.Build the project and note the result. b.Use enumerated constants method to simplify the program.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 19 References Teach Yourself C++ in 21 Days, Second Edition (

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 20 Acknowledgment The slides are based on the set developed by Dr. Frank Leung (EIE).