A First Program - Greeting.cpp

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

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

Introduction to Programming

UNIT- 3 Introduction to C++

2 nd Week Lecture Notes

BITG 1233: Introduction to C++

Computer Programming : C++

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

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

CHAPTER 3 BASIC INSTRUCTION OF C++

Introduction to Programming EC-105. Lecture 2

Chapter 1 Introduction to Computers and C++ Programming

CSCE 110 PROGRAMMING FUNDAMENTALS

6.096 Introduction to C++ January (IAP) 2009

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

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

Objectives. In this chapter, you will:

Fundamentals of Programming CS-110. Lecture 2

Chapter 2: Introduction to C++

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

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

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

Operations. Making Things Happen

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

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

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

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Visual C# Instructor s Manual Table of Contents

Chapter 2: Overview of C++

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

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

Declaration and Memory

LECTURE 02 INTRODUCTION TO C++

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

Programming. C++ Basics

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

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

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

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

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

Chapter 1 INTRODUCTION

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

The C++ Language. Arizona State University 1

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.

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

CSCI 123 Introduction to Programming Concepts in C++

CHAPTER 3 Expressions, Functions, Output

Chapter 2: Using Data

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

Reserved Words and Identifiers

Creating a C++ Program

VARIABLES & ASSIGNMENTS

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

Chapter 2. C++ Basics

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

Introduction of C++ OOP forces us to think in terms of object and the interaction between objects.

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

Chapter 2. Outline. Simple C++ Programs

Chapter 2 Elementary Programming

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

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Introduction to C ++

1.3b Type Conversion

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

CS102: Variables and Expressions

CS313D: ADVANCED PROGRAMMING LANGUAGE

C: How to Program. Week /Mar/05

Introduction to Programming using C++

Basics of Java Programming

Lecture 2 Tao Wang 1

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

EEE145 Computer Programming

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

Engineering Problem Solving with C++, Etter/Ingber

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Computer System and programming in C

Chapter 2 - Introduction to C Programming

Programming and Data Structures

Getting started with C++ (Part 2)

These are reserved words of the C language. For example int, float, if, else, for, while etc.

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.

Unit 3. Constants and Expressions

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Operators in java Operator operands.

JAVA Programming Fundamentals

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

Input And Output of C++

Increment and the While. Class 15

This watermark does not appear in the registered version - Slide 1

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

Computational Physics Operating systems

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

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

1. In C++, reserved words are the same as predefined identifiers. a. True

Introduction to C# Applications

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

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Add Subtract Multiply Divide

Transcription:

C++ Basics

A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include <iostream> using namespace std; int main() { cout << "!!!Hello World!!!" << endl; return 0; } Insertion operator Ends executions of main() which ends program Insertion statement Comments Provides simple access Function

Output

Contents Escape Sequences Variables and Data types Basic Input in c++ Arithmetic Operators Operator Precedence

Comments in C++ Single line comments (//) Multiple line comments (/*.*/)

Escape sequences Escape Sequence Description \n New Line \t Tab space \r Carriage return \a Alert. Sound system bell \\ Print back slash character \ Print single quote character \ Print double quote character

New Line Character \n #include<iostream> using namespace std; int main ( ) { cout << "Hello \n Welcome to C++"; return 0; } Output: Hello Welcome to C++

New Line Character \n #include<iostream> using namespace std; int main ( ) { cout << "Hello \n"; cout<< Welcome to C++"; return 0; } Output: Hello Welcome to C++

Tab space \t #include<iostream> using namespace std; int main (void) { cout << "Hello \n"; cout<< Hello! \t Welcome to C++ ; return 0; } Output: Hello Hello! Welcome to C++

Bits & Bytes A bit is a single numeric value either 1 or 0. that encodes a single unit of digital information. A byte is a sequence of bits. 8 bits=1 byte.

Variables A storage box, its type defines the kind of stuff you store in it E.g. shoe box used for shoes Jewelry box used for jewelry CD kit used for CDs 12

Variable Variable is a box In computer, a storage box is a memory location on RAM 13

Memory in Computer First you ask computer to give you a box and you also have to specify what type of box you want Each little box is a memory location Occupied Memory Spaces Free Memory Spaces 14

Data Types A computer program operates on data and produces an output. In C++, each data must be of specific data type. The data type determines how the data is represented in the computer and kind of processing the computer can perform on it. Types of data integer (int) Float (float) Boolean Characters (char) 15

Integers (int) integers are whole numbers with a range of values. used to store numbers Example: 5, 6, 100, 2500 On a 32-bit system such as Windows, an int occupies 4 bytes (which is 32 bits) of memory. This allows an int to hold numbers in the range from 2,147,483,648 to 2,147,483,647. 16

Float used to represent floating point numbers. Examples: 9.1314, 3.1254 Occupies 4 bytes (32 bits) in memory 17

Character Character types can hold a single character. Occupy only 1 byte (eight bits) of memory. Commonly used to store ASCII characters. 18

ASCII Coding System American Standard Code for Information Interchange (ASCII) Originally designed as a 7-bit code purpose was to standardize a binary code to give the computer user the capability of using several machines to process data regardless of the manufacturer for transmitting and processing data 8-bit version of ASCII

boolean Boolean or Flag type is a type which can represent only two values: 0 and 1, usually identified with false and true respectively. This type can be stored in memory using a single bit. e.g. main() { } bool x=true; //or x=false 21

Variables A storage box, its type defines the kind of stuff you store in it In computer, a storage box is a memory location on RAM 22

Memory in Computer First you ask computer to give you a box and you also have to specify what type of box you want Each little box is a memory location Occupied Memory Spaces Free Memory Spaces 23

Rules for Variable Naming You can use upper and lowercase letters, digits from 0 to 9 and underscore ( _ ). The first character must be letter or underscore ( _ ). Identifier can be as long as you like but only the first 250 character are recognizable in C++ Compiler.

Integer Variables Example #include<iostream> using namespace std; int main ( ) { int var1; //define var1 int var2; //define var2 var1 = 20; //assign value to var1 var2 = var1 + 10; //assign value to var2 cout<< Result = ; cout<<var2<< endl; //displaying the sum of var1 + 10 return 0; } Output: Result=30

Character Variable Example #include<iostream> using namespace std; int main ( ) { char charvar1 = A ; //define char variable char charvar2 = \t ; cout << charvar1; // display character cout << charvar2; charvar1 = B ; //reassigning charvar1 to B cout << charvar1; return 0; } Output: A B

Float Variable Example #include<iostream> using namespace std; int main ( ) { float radius=2; float Pi =3.14; cout <<"Area is " << Pi * rad * rad<< endl; return 0; } Output: Area is 12.56

Constant values used in a program that are not changed during the course of program. e.g. Circumference=2*pi*r. area=pi*r*r pi can be declared as a constant here. 28

Defining constant main() } { const int i = 5; i = 10; //error, can not modify constant i++; // error, can not modify constant

Basic Input in c++ cin with extraction operators >>. cin>> tells computer to wait for input from user (keyboard). When a value is entered, it needs to be stored/boxed in the appropriate variable.

Input example int age; cin >> age; 31

Arithmetic operators Addition + Subtraction - Multiplication * Division / Remainder % Shorthand: a=a+b a=a-b a=a*b a=a/b a=a%b a+=b a-=b a*=b a/=b a%=b 34

Operator precedence ( ) * / % + - 35

Activity 4 What prints when each of the following c++ statement is executed? Assume x=2 and y=3. 1. cout<<x; 2. cout<<x+x; 3. cout<< x= ; 4. cout<< x= <<x; 5. cout<<x+y<< = <<y+x;

Increment Operators You often need to add 1 to the value of an existing variable. Normal way to do this is count = count + 1; Or using an arithmetic assignment operator count += 1; Another approach is ++count; The ++ operator increments its arguments.

Increment Operators Prefix and Postfix The increment operator can be used in two ways Prefix: meaning that the operator precedes the variable Postfix: meaning that operator follows the variable. Example: 1. result = result * ++count; Note: In this scenario count is incremented first than multiplied by result. 2. result = result * count++

Increment Operator Example int main () { int count = 10; cout<< count = <<count; // count=10 cout<< count= << ++count; //count = 11 cout<< count= <<count++; //count = 11 cout<< count= <<count; //count = 12 return 0; }

Integer and Float Conversion rules that are used for implicit conversion of floating point and integer values in C++ are; An arithmetic operation between an integer and integer always yields an integer result. Operation between a real and real always yields a real result Operation between an integer and real always yields a real result.

Type Conversion In Assignment In some Cases, it may happen that the type of the expression on the right hand side and the type of the variable on the left hand side of the assignment operator may not be same. In that case the value of the expression is promoted or demoted depending on the type of the variable on left hand side of assignment operator.

Type Conversion in Assignments Example int i; float y; i = 35.9; y = 10; As 35.9 is of float type it cannot be stored in i of int type. In this case float is demoted to an int and then value is stored in i. So the value of i will be 35. Same will happen in y i.e. 10 will be promoted to 10.00 and then will be stored in y.

Type Conversion in Assignment float a=1.0, b = 2.0, c = 10.0; int s; s = a * b * c / 100 + 32 / 4 3 * 1.1; In the above example, some of the operands are of type int and some of them are of type float. As we know during evaluation of the expression int will be promoted to float and result would be of type float. But when this float value is assigned to s, it is again demoted to an int and then stored in s.