CHAPTER 3 BASIC INSTRUCTION OF C++

Similar documents
Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++

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

LECTURE 02 INTRODUCTION TO C++

The C++ Language. Arizona State University 1

BITG 1233: Introduction to C++

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

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

VARIABLES & ASSIGNMENTS

Chapter Two MULTIPLE CHOICE

Computer Programming : C++

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

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

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

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

REVIEW. The C++ Programming Language. CS 151 Review #2

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Objectives. In this chapter, you will:

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

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

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

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Lecture 2 Tao Wang 1

A First Program - Greeting.cpp

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

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

UNIT- 3 Introduction to C++

Lecture 3 Tao Wang 1

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Engineering Problem Solving with C++, Etter/Ingber

Unit 3. Constants and Expressions

Your first C++ program

Introduction to Programming

DEPARTMENT OF MATHS, MJ COLLEGE

2 nd Week Lecture Notes

Chapter 2 Basic Elements of C++

Getting started with C++ (Part 2)

Introduction to the C++ Programming Language

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

Fundamental of Programming (C)

Chapter 2: Overview of C++

Chapter 1 Introduction to Computers and C++ Programming

Chapter 2. Outline. Simple C++ Programs

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Creating a C++ Program

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

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.

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

Chapter 1 INTRODUCTION

6.096 Introduction to C++ January (IAP) 2009

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

Visual C# Instructor s Manual Table of Contents

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Fundamentals of Programming CS-110. Lecture 2

CSc Introduction to Computing

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Basics of Java Programming

Programming with C++ as a Second Language

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Chapter 2: Using Data

LECTURE 3 C++ Basics Part 2

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Introduction to Programming EC-105. Lecture 2

Full file at C How to Program, 6/e Multiple Choice Test Bank

Input And Output of C++

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

Understanding main() function Input/Output Streams

Fundamentals of Programming

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

CSCE 110 PROGRAMMING FUNDAMENTALS

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

Full file at

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Maciej Sobieraj. Lecture 1

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

A flow chart is a graphical or symbolic representation of a process.

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

Programming. C++ Basics

BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 2. Lexical Elements & Operators

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Topic 2: C++ Programming fundamentals

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

UNIT-2 Introduction to C++

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

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

Transcription:

CHAPTER 3 BASIC INSTRUCTION OF C++ MOHD HATTA BIN HJ MOHAMED ALI Computer programming (BFC 20802)

Subtopics 2 Parts of a C++ Program Classes and Objects The #include Directive Variables and Literals Identifiers Data Types Integer Char Floating point bool Arithmetic Operators Comments

Parts of a C++ Program 3 // sample C++ program #include <iostream> using namespace std; int main() { } beginning of function named main beginning of block for main cout << "Hello, there!"; return 0; end of block for main send 0 to operating system comment preprocessor directive which namespace to use string literal output statement

Special Characters 4 Character Name Meaning // Double slash Beginning of a comment # Pound sign Beginning of preprocessor directive < > Open/close brackets Enclose filename in #include ( ) Open/close parentheses Used when naming a function { } Open/close brace Encloses a group of statements " " Open/close quotation marks Encloses string of characters ; Semicolon End of a programming statement

5 Classes and Objects The cout Object Displays output on the computer screen You use the stream insertion operator << to send output to cout: cout << "Programming is fun!";

6 Classes and Objects The cout Object Can be used to send more than one item to cout: Or: cout << "Hello " << "there!"; cout << "Hello "; cout << "there!";

7 Classes and Objects The cout Object This produces one line of output: cout << "Programming is "; cout << "fun!"; Programming is fun!

8 Classes and Objects The endl Manipulator You can use the endl manipulator to start a new line of output. This will produce two lines of output: cout << "Programming is" << endl; cout << "fun!"; You do NOT put quotation marks around endl Programming is fun! This is a lowercase L

9 Classes and Objects The \n Escape Sequence You can also use the \n escape sequence to start a new line of output. This will produce two lines of output: cout << "Programming is\n"; cout << "fun!"; Programming is fun! Notice that the \n is INSIDE the string.

The #include Directive 10 Inserts the contents of another file into the program This is a preprocessor directive, not part of C++ language #include lines not seen by compiler Do not place a semicolon at end of #include line #include <iostream>

Variables and Literals 11 Variables Variable: a storage location in memory Has a name and a type of data it can hold Must be defined before it can be used: int item;

12 Variables and Literals Variables Variable Definition

13 Variables and Literals Literals Literal: a value that is written into a program s code. "hello, there" (string literal) 12 (integer literal)

14 Variables and Literals Literals 20 is an integer literal This is a string literal

Identifiers 15 An identifier is a programmer-defined name for some part of a program: variables, functions, etc. You cannot use any of the C++ key words as an identifier. These words have reserved meaning.

Variable Names 16 A variable name should represent the purpose of the variable. For example: itemsordered The purpose of this variable is to hold the number of items ordered.

Identifier Rules 17 The first character of an identifier must be an alphabetic character or and underscore ( _ ), After the first character you may use alphabetic characters, numbers, or underscore characters. Upper- and lowercase characters are distinct

Valid and Invalid Identifiers 18 IDENTIFIER VALID? REASON IF INVALID totalsales total_sales Yes Yes total.sales No Cannot contain. 4thQtrSales No Cannot begin with digit totalsale$ No Cannot contain $

19 Data Types Integer Data Types Integer variables can hold whole numbers such as 12, 7, and -99.

Data Types 20 Defining Variables Variables of the same type can be defined - On separate lines: int length; int width; unsigned int area; - On the same line: int length, width; unsigned int area; Variables of different types must be in different definitions

Data Types 21 char Data Type Used to hold characters or very small integer values Usually 1 byte of memory Numeric value of character from the character set is stored in memory: CODE: char letter; letter = 'C'; MEMORY: letter 67

Character Strings 22 A series of characters in consecutive memory locations: "Hello" Stored with the null terminator, \0, at the end: Comprised of the characters between the " " H e l l o \0

23 Data Type Floating-Point Data Types The floating-point data types are: float double long double They can hold real numbers such as: 12.45-3.8 Stored in a form similar to scientific notation All floating-point numbers are signed

24 Data Type bool Data Type Represents values that are true or false bool variables are stored as small integers false is represented by 0, true by 1: bool alldone = true; bool finished = false; alldone finished 1 0

Variable Assignments 25 An assignment statement uses the = operator to store a value in a variable. item = 12; This statement assigns the value 12 to the item variable. The variable receiving the value must appear on the left side of the = operator. This will NOT work: // ERROR! 12 = item;

Variable Initialization 26 To initialize a variable means to assign it a value when it is defined: int length = 12; Can initialize some or all variables: int length = 12, width = 5, area;

Arithmetic Operators 27 Used for performing numeric calculations C++ has unary, binary, and ternary operators: unary (1 operand) -5 binary (2 operands) 13-7 ternary (3 operands) exp1? exp2 : exp3

Binary Arithmetic Operators 28 SYMBOL OPERATION EXAMPLE VALUE OF ans + addition ans = 7 + 3; 10 - subtraction ans = 7-3; 4 * multiplication ans = 7 * 3; 21 / division ans = 7 / 3; 2 % modulus ans = 7 % 3; 1

A Closer Look at the / Operator 29 / (division) operator performs integer division if both operands are integers cout << 13 / 5; // displays 2 cout << 91 / 7; // displays 13 If either operand is floating point, the result is floating point cout << 13 / 5.0; // displays 2.6 cout << 91.0 / 7; // displays 13.0

A Closer Look at the % Operator 30 % (modulus) operator computes the remainder resulting from integer division cout << 13 % 5; // displays 3 % requires integers for both operands cout << 13 % 5.0; // error

Comments 31 Used to document parts of the program Intended for persons reading the source code of the program: Indicate the purpose of the program Describe the use of variables Explain complex sections of code Are ignored by the compiler

Comments 32 Single Line Begin with // through to the end of line: int length = 12; // length in inches int width = 15; // width in inches int area; // calculated area // calculate rectangle area area = length * width;

Comments 33 Multi-Line Comments Begin with /*, end with */ Can span multiple lines: /* this is a multi-line */ comment Can begin and end on the same line: int area; /* calculated area */

34 Thank You Q & A