Basic Elements of C++ Chapter 2.

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Basic Elements of C++ Chapter 1.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Chapter 2: Basic Elements of C++. Objectives In this chapter, you will: – Become familiar with the basic components of a C++ program, including functions,
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter 2: Basic Elements of C++
Chapter 1.2 Introduction to C++ Programming
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++ Chapter 1.
Computer Programming BCT 1113
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of C++
Chapter 2 Elementary Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
C++ Programming Basics
Presentation transcript:

Basic Elements of C++ Chapter 2

Chapter Topics The Basics of a C++ Program Data Types Arithmetic Operators and Operator Precedence Expressions Input Increment and Decrement Operators Output Preprocessor Directives Program Style and Form More on Assignment Statements

The Basics of a C++ Program A C++ program is a collection of one or more subprograms (functions) Function Collection of statements Statements accomplish a task Every C++ program has a function called main

Welcome to C++ Programming Example Program #include <iostream> using namespace std; int main() { cout<<"Welcome to C++ Programming"<<endl; return 0; } Welcome to C++ Programming Program Output

The Basics of a C++ Program Programming language a set of rules, symbols, special words Rules syntax – specifies legal instructions Symbols special symbols ( + - * ! … ) Word symbols reserved words (int, float, double, char …)

Identifiers Rules for identifiers Evaluate the following must begin with letter or the underscore _ followed by any combination of numerals or letters recommend meaningful identifiers Evaluate the following ElectricCharge 23Skidoo snarFbLat

Data Types Definition: a set of values combined with a set of operations

Data Types Simple data types include Integer data types include Integers Floating point Enumeration Integer data types include char short int long bool Numerals, symbols, letters Numbers without decimals Values true and false only

Floating-Point Types Stored using scientific notation the sign of the number, the significant digits of the number the sign of the power of 10 the power of 10

Data Types Different floating- point types Note that various types will have different ranges of values require different amounts of memory

Data Types The string Type A string is a sequence of characters a programmer-defined type requires #include <string> A string is a sequence of characters "Hi Mom" "We're Number 1!" "75607"

Arithmetic Operators and Operator Precedence Common operators for calculations + - * / % Precedence same as in algebraic usage Inside parentheses done first Next * / % from left to right Then + and - from left to right Note operator precedence chart, page 1035

Expressions An expression includes constants variables function calls combined with operators 3 / 2 + 5.0 sin(x) + sqrt(y)

Expressions Expressions can include values all of the same type 3 + 5 * 12 – 7 values of different (compatible) types 1.23 * 18 / 9.5 An operation is evaluated according to the types of the operands if they are the same, the result is the type of the operands if the operands are different (int and float) then the result is float

Type Casting Implicit change of type can occur when operands are of different type It is possible to explicitly specify that an expression be converted to a different type static_cast < type > (expression) static_cast <int> (3.5 * 6.9 / x)

Input Storing data in the computer's memory requires two steps Allocate the memory by declaring a variable Have the program fetch a value from the input device and place it in the allocated memory location cin >> x 123 x

Note optional initialization of the variable Allocating Memory Variable A memory location whose content may change during program execution Declaration: Syntax: type identifier; Example: double x; int y = 45; Note optional initialization of the variable

Note required initialization of the named constant Allocating Memory Named Constant A memory location whose content cannot be changed Declaration Syntax: const type identifier = value; Example const double PI = 3.14159; Note required initialization of the named constant

Putting Data Into Variables At initialization time Assignment statement Syntax: variable = expression; Example x = 1.234; volume = sqr (base) * height; Input (read) statement Syntax: cin >> variable ; Example cin >> height; Program Example

Increment and Decrement Operators Pre-increment ++x; equivalent to x = x + 1; Pre-decrement --x; Changes the value before execution of a statement y = ++x; Post-increment intVal++; Post-decrement intVal--; Changes the value after execution of the statement y = x++;

Manipulator for carriage return Output Values sent to an output device Usually the screen Can also be a file or some device Syntax for screen output: cout << expression << … Example cout << "The total is "<< sum << endl; Insertion operator Manipulator for carriage return Output command Values to be printed Sample Program

Output Escape sequences also used to manipulate output cout << "The total is\t "<< sum << endl;

Preprocessor Directives Commands supplied to the preprocessor Runs before the compiler Modifies the text of the source code before the compiler starts Syntax start with # symbol #include <headerFileName> Example #include <iostream>

Preprocessor Directives Note the preprocessor step in the sequence

Namespace The #include <iostream> command is where cin and cout are declared They are declared within a namespace called std When we specify using namespace std; Then we need not preface the cin and cout commands with std::cin and std::cout

Program Style and Form Every program must contain a function called main int main (void) { … } The int specifies that it returns an integer value The void specifies there will be no arguments Also can say void main( ) { … }

Program Style and Form Variables usually declared inside main at beginning of program Use blanks and space to make the program easy for humans to read Semicolons ; required to end a statement Commas used to separate things in a list

Program Style and Form Documentation Comments specified between /* this is a comment */ and following // also a comment Always put at beginning of program /* name, date, cpo, purpose of program */

Program Style and Form Names of identifiers should help document program double electricCharge; // instead of ec Prompt keyboard entry cout << "Enter the value for x -> "; cin >> x;