BTE2313. Chapter 2: Introduction to C++ Programming

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

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

CS242 COMPUTER PROGRAMMING

Chapter 2: Introduction to C++

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

Objectives. In this chapter, you will:

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

Lecture 2 Tao Wang 1

Introduction to Programming EC-105. Lecture 2

Computer Programming : C++

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

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

Creating a C++ Program

LECTURE 02 INTRODUCTION TO C++

CSc Introduction to Computing

Getting started with C++ (Part 2)

6.096 Introduction to C++ January (IAP) 2009

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

Your first C++ program

Fundamentals of Programming CS-110. Lecture 2

The C++ Language. Arizona State University 1

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

Chapter 2 Basic Elements of C++

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

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

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

BITG 1233: Introduction to C++

CHAPTER 3 BASIC INSTRUCTION OF C++

Exercise: Using Numbers

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

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

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

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

Typescript on LLVM Language Reference Manual

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

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

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

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

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Programming. C++ Basics

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

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.

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

A A B U n i v e r s i t y

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Lesson 2 Variables and I/O

Chapter 2: Overview of C++

A First Program - Greeting.cpp

Fundamental of Programming (C)

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

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

4. Structure of a C++ program

Variables and Functions. ROBOTC Software

ARG! Language Reference Manual

Introduction to C# Applications

Chapter 2: Using Data

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

Fundamentals of C Programming

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

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

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

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

2 nd Week Lecture Notes

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Key Differences Between Python and Java

Chapter 1 INTRODUCTION

VARIABLES & ASSIGNMENTS

Chapter 2 C++ Fundamentals

Program Organization and Comments

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

Programming with C++ as a Second Language

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

C++ basics Getting started with, and Data Types.

CSCI 1061U Programming Workshop 2. C++ Basics

Relational Operators and if. Class 10

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

DEPARTMENT OF MATHS, MJ COLLEGE

UNIT- 3 Introduction to C++

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Visual C# Instructor s Manual Table of Contents

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

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

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

SSOL Language Reference Manual

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

UEE1302 (1102) F10: Introduction to Computers and Programming

Full file at

The SPL Programming Language Reference Manual

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Variables. Data Types.

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Introduction to the C++ Programming Language

CSC Web Technologies, Spring Web Data Exchange Formats

IT 374 C# and Applications/ IT695 C# Data Structures

Differentiate Between Keywords and Identifiers

CSI33 Data Structures

Transcription:

For updated version, please click on http://ocw.ump.edu.my BTE2313 Chapter 2: Introduction to C++ Programming by Sulastri Abdul Manap Faculty of Engineering Technology sulastri@ump.edu.my

Objectives In this chapter, you will learn about: 1) keywords/reserve words, special symbols, and identifiers in C++ 2) data types 3) assignment statement 4) Modifying your first C++ Program 5) Structure a program, with comments as part of documentation

The Basics of a C++ Program (cont.) Basic of C++: reserved words or keywords (such as if, while, int, etc.) Consists of symbols (such as { } = <=! [ ] * & (and more) Consists of programmer-defined names known as identifiers for variables, constants and functions

Reserved Words (Keywords) Reserved word /keywords: Cannot be changed within program Cannot be used for other than their anticipated use Examples: float void include long return

Identifiers Identifiers are variables/constants name that are given by programmer: Must use letter, digits, _ (underscore) and have 1-31 chars long Must start with a letter or an underscore ( _ ) Case-sensitive, where Mum is different than mum should be meaningful: studentnumber is better than n or sn

Identifiers (cont.) Legal identifiers in C++: second comparison payslip

C++ Data Types Each data item has a type and a name type determines the range of possible values it can hold and operations that can be used on it Can we do this? x = "Joe" + 1 ; Commonly used data types: int, float, double, char, string.

C++ Data Types (cont.) integral floating char short int long bool float double long double unsigned/signed

int Data Type int keyword is used for integer numbers. cannot have any decimal point, comma, or other symbol + or - in the numbers are allowed example of constants can be coded/written as 1234, -12, +67 Examples: int x; //data type & identifier s name without value yet int x, y; //declares 2 integers without values int numpeople = 16000; //declare and set initial value

float and double Data Type Used for real numbers that have decimal point, + or are allowed and no comma or other symbols are allowed. Example of constants can be coded/written as 2.11, -78.98, 0.025, 10.5e6 (10.5 times 10 to the 6th power) Declaration Examples: float _pi = 3.1416; float y = 10.5, //declares names and sets initial value //comma here is used to go to next line z = 15.6543; double x = 1.5; double y = 245.643; Float biggest = 3.28e4; // exponential notation: 32800

char Data Type Only for one character Constants are coded/written using single quotes B', b', 9', $', '-', @',!' A blank space is considered a character which is written as ' ' Example of declaration s: char go; char selection = s';

String Data Type Strings are used for sequence of characters and written/coded by string keyword. From 0 to many characters Constants for string are written/coded with double quotes!!hello world!! Examples: string str; string name = Daniel ;

bool Data Type bool keyword is used for Boolean data, that can have only 2 values; either true or false This data type id used to manipulate logical (Boolean) expressions true or false are logical values and they are all reserved words

C++ Expression A C++ expression consists of combination of constants, variables and also operators. C++ expression can be evaluated to calculate a value of any data. Expression can be a variable or a constant, or an operation such as x + y, or a function call such as CalcArea(2, 4)

Assignment Operator An operator to give (assign) a value to a variable. Denote as = Only variable can be on the left side. An expression is on the right side. Variables keep their assigned values until changed by another assignment statement or by reading in a new value.

Syntax for Assignment Operator Assignment operation is used for storing result of an expression into a variable. expression on right will be evaluated first, and followed by storing the result in the memory location of the variable indicated on left

Assignment Operator Mechanism Example: int count = 0; int starting; starting = count + 5; Expression evaluation: Get value of count: 0 Add 5 to it. Assign to starting 0 12345 (garbage) 5

C++ Program: Example #include <iostream> using namespace std; int main() { int number1, number2, summation; number1 = 20; number2 = 14; summation = number1 + number2; return 0; }

#include <iostream> First C++ Program: Try this! using namespace std; int main() { cout << Hi BTE2313 students!\n ; //display the string return 0; } //main function ends here

Use of Blanks/Whitespaces One or more blanks can be used to isolate numbers They are also used to separate between reserved words/keywords and also from other symbols They cannot appear within a reserved word or identifier If blanks are properly utilized, it will make the program more readable.

Semicolons; Brackets () and Commas, All C++ statements must end with a semicolon, which is also known as a statement terminator Curly brackets or braces { } are not a C++ statement Can be regarded as delimiters Commas separate items in a list

Comments In C++, there are comments option, which are not for the C++ compiler, but for the reader. There are two types of comments, single line and multiple lines. Single line comment begins with // // Example of a single line comment Multiple lines comments are bounded between /* and */ /* */ Example of several lines comments