Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.

Slides:



Advertisements
Similar presentations
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Advertisements

©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
© 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.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Data types and variables
How Create a C++ Program. #include using namespace std; void main() { cout
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Starting Out with C++, 3 rd Edition 1 Chapter 2. Introduction to C++
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Input & Output: Console
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
Introduction to C The Parts of a C++ Program –Anatomy of a simple C++ program.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
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.
C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Starting Out with C++: From Control Structures through Objects 7 th edition By Tony Gaddis Source Code Chapter 2.
CSC 107 – Programming For Science. The Week’s Goal.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Introduction to C++. 2 What Is a Program Made Of?
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Chapter 2 Introduction to C++. 2 Topics 2.1 Parts of a C++ Program 2.2 The cout Object 2.3 The #include Directive 2.4 Variables and Constants 2.5 Identifiers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Bill Tucker Austin Community College COSC 1315
Chapter 2: Introduction to C++
Chapter 2. Introduction to C++
Documentation Need to have documentation in all programs
Starting Out with C++: From Control Structures through Objects
Introduction to C++ October 2, 2017.
Chapter 2: Introduction to C++
Chapter 2: Introduction to C++
2.1 Parts of a C++ Program.
Chapter 2: Introduction to C++.
Arithmetic Operations
Presentation transcript:

Lecture 3: The parts of a C++ program (Cont’d) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

The char Data Type 1 byte integer data type Used for storing characters as an int ASCII (American Standard Code for Information Interchange) Table

Character literals are not string literals! Program 2-12 and 2-13 char letter; Letter = ‘a’; char letter; Letter = “a”;

// Relationship between integers and characters #include using namespace std; int main() { char letter; letter = 65; cout << letter << endl; letter = 66; cout << letter << endl; return 0; }

// A simple C++ program #include using namespace std; int main() { char letter; letter = ‘A’; cout << letter << endl; letter = ‘B’; cout << letter << endl; return 0; }

Floating-Point Data Types Used to define variables that hold real numbers float (single – precision) double (double – precision) long double (at least 2x double size)

Tips 1.2F – forces literal to be stored as a float The following are equivalent floating-point literals 1.4E11 1.4e11 1.4E e

// A simple C++ program #include using namespace std; int main() { float distance; double mass; distance = E11; mass = 1.989E30; cout << “The sun is “ << distance << “ meters away.\n”; cout << “The sun\’s mass is “ << mass << “ kilograms.\n”; return 0; }

The bool Data Type Boolean variables can either be true or false ( 0 or 1) Program 2-16

// A simple C++ program #include using namespace std; int main() { bool boolValue; boolValue = true; cout << boolValue << endl; boolValue = false; cout << boolValue << endl; return 0; }

Determining the Size of a Data Type The sizeof operator can be used to determine the size of a data type on any system. Example: sizeof(int); Program 2.17

// A simple C++ program #include using namespace std; int main() { long double apple; cout << “The size of an integer is “ << sizeof(int) << bytes.\n”; cout << “The size of a long integer is “ << sizeof(long) << bytes.\n”; cout << “An apple can be eaten in “ << sizeof(apple) << bytes!\n”; return 0; }

Variable assignments and initializations An assignment operation assigns, or copies, a value into a variable. When a value is assigned as part of it’s definition, it is called initialization. Program 2-18

// A simple C++ program #include using namespace std; int main() { int month =2, days =28; cout << “Month “ << month << “ has “ << days << “ days.\n”; return 0; }

Scope A variable’s scope is that part of the program that has access to that variable. Complex, and the rules that define a variables scope will be covered throughout the lectures Rule 1: A variable cannot be used in any part of the program until it is defined.

Arithmetic Operators There are many operators for manipulating numeric values and performing arithmetic operations. Three types Unirary (requires only 1 operand. Exmp. -5) Binary (requires 2. Exmp. A = 3) Ternary (requires 3. Exmp. Discussed in chp. 4)

Common Arithmetic Operators Binary operators. +addition, total = cost + tax; -subtraction, cost = total – tax; *multiplication, tax = /division, salePrice = original / 2; %modulo, remainder = value % 3; Tip: when both operands in a division are integers, the statement will perform int division. Use one number as floating point if need. Do program 2-20

Comments and Programming Style Not used by the compiler, i.e. invisible // - single line comment /* ……… */ - multiline comment Programming Style is the visual arrangement of your code. Make it readable for yourself and others!

Homework Check moodle! Due Saturday before Midnight.