Creating a C++ Program

Similar documents
Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

6.096 Introduction to C++ January (IAP) 2009

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++

UNIT- 3 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

Chapter 2: Introduction to C++

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

Introduction to Programming

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

Introduction to C# Applications

LECTURE 02 INTRODUCTION TO C++

Objectives. In this chapter, you will:

BITG 1233: Introduction to C++

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

The C++ Language. Arizona State University 1

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

Lecture 2 Tao Wang 1

C: How to Program. Week /Mar/05

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

DEPARTMENT OF MATHS, MJ COLLEGE

Chapter 1 Introduction to Computers and C++ Programming

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

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

Chapter 2 - Introduction to C Programming

Programming. C++ Basics

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

Chapter 2 Basic Elements of C++

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

Computer Programming : C++

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

CS242 COMPUTER PROGRAMMING

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

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

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

Lecture 3 Tao Wang 1

Presented By : Gaurav Juneja

Fundamental of Programming (C)

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

CSc Introduction to Computing

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

CHAPTER 3 BASIC INSTRUCTION OF C++

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

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

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

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

A Fast Review of C Essentials Part I

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

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

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

Fundamentals of Programming CS-110. Lecture 2

Introduction to Programming EC-105. Lecture 2

A First Program - Greeting.cpp

Typescript on LLVM Language Reference Manual

C Language, Token, Keywords, Constant, variable

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Programming and Data Structures

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

Chapter 2. C++ Basics

UNIT-2 Introduction to C++

The Warhol Language Reference Manual

Differentiate Between Keywords and Identifiers

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

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

Chapter 2: Overview of C++

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

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

UEE1302 (1102) F10: Introduction to Computers and Programming

Chapter 2: Using Data

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

BTE2313. Chapter 2: Introduction to C++ Programming

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

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

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

CSCE 110 PROGRAMMING FUNDAMENTALS

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

INTRODUCTION 1 AND REVIEW

Chapter 1 & 2 Introduction to C Language

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

VARIABLES & ASSIGNMENTS

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Introduction to Programming

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

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

CS201 Some Important Definitions

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

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Basics of Java Programming

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

Programming for Engineers Introduction to C

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

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

ANSI C Programming Simple Programs

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

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

Java Notes. 10th ICSE. Saravanan Ganesh

Transcription:

Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer. 1

Creating a C++ Program created using an IDE source code Object code Executable code: exe file An Introduction to Programming with C++, Fifth Edition 2

Processing a C++ Program (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3

The Problem Analysis Coding Execution Cycle (cont d.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 4

Structure of C++ Program To include header files in our program Header File Contains built-in-functions. These are also called pre-defined or already developed functions Preprocessor Directives Angle or pointed brackets Return type of main function. It is used to determines whether the program is executed successfully or not. Void means nothing Body Begin 1 2 #include<iostream.h> #include<conio.h> void main( ) { Function Name main function Parenthesis shows function. It is used to pass parameters/arguments h stands for header file File name(standard input output ) Starting point of program execution 3 Program Body Body End Cout stands for console output.. It uses insertion operator (<<) print the output on the monitor/console. } cout<< Welcome to Computer Scientists ; getch(); Each statement must end with semicolon. A statement without Get character String or message to be semicolon generates function. It get a displayed on monitor. It must be syntax error. character at runtime. enclosed in double quotes( ) But we use it to stay screen to see output 5

Standard input and output streams cin Key Board >> Standard input stream cout << Monitor << cerr Memory Standard output stream

Simple Steps to write and run a program in C++. 1. Go to this path C:\TC\BIN then double click on short to open C++ language. 2. Now save your program by choosing a meaningful program file name. e.g. welcome.cpp is a file name for a program that will display welcome message at runtime. 3. Now write your program as explained in the previous slide. (translation of algorithm to a c++ program). 7

4. Press Alt+F9 key to compile program. If your program is error free then following screen will appear: 8

5. Now press Ctrl+F9 key to run the program. Following screen shows the output of the program: Now press enter to go back to the source code of the program 9

Another Sample Program Program: #include<stdio h> void main() { int number; cout<< Enter an integer: \n ); //message for user cin>>number; //get input cout<< The number you entered is: <<number; getch(); } Output: Enter an integer: 25 The number you entered is: 25 10

Programming Style C++ is a free-format language, which means that: Extra blanks (spaces) or tabs before or after identifiers/operators are ignored. Blank lines are ignored by the compiler just like comments. Code can be indented in any way. There can be more than one statement on a single line. A single statement can continue over several lines. In order to improve the readability of your program, use the following conventions: Start the program with a header that tells what the program does. Use meaningful variable names. Document each variable declaration with a comment telling what the variable is used for. Place each executable statement on a single line. A segment of code is a sequence of executable statements that belong together. Use blank lines to separate different segments of code. Document each segment of code with a comment telling what the segment does. 11

Variable: Location in memory where value can be stored An identifier is a name for a variable, constant, function, etc. Rules to Declare an Identifier (variable) Alphabets from A to Z or a to z The digits from 0 to 9 Underscore(_) can be used The first character of an identifier can not be a digit The name of an identifier can not be a reserve word No space allowed in the name of identifier Valid Name: A Student_Name _Fname Pi Inalid Name: $Sum //special ch. 6StName // 1 st letter digit F name // no space allowed int // reserve word 12

Identifier (variable) Declaration Syntax Data-Type Space Variable-Name(Indentifier); e.g. int frstnumber; char choice; float divide; long output; Identifier (variable) Initialization Syntax Data-Type Space Variable-Name(Indentifier) = Value; e.g. int frstnumber=10; char choice= y ; float divide=0.0; 13

Memory Concepts cin>> first; Identifier Variable Assume user entered 45 cin>>second; Assume user entered 72 first 45 first 45 second 72 sum = first + second; first 45 second 72 sum 117 14

Assignment Operator (=) = (assignment operator) Assigns value to variable Binary operator (two operands) Example: sum = variable1 + variable2; 15

How many bytes I am eating? integer data short int long 2 bytes 2 bytes(16 bit system) 4 bytes (32 bit system) 4 bytes Floating point data float double long double 4 bytes 8 bytes 10 bytes Character char 1 byte Boolean bool 1 byte 16

Memory Allocation for Arithmetic Data Types Size (bytes) Range of Values Name (alternate) Description AVR GCC MSVC++ AVR GCC MSVC++ char Character or small integer 1 1 short int (short) Short integer 2 2 int Integer 2 4 long int (long) Long integer 4 4 long long int (long long) Really long integer 8 8 float 'Single-precision' floating point number 4 4 double 'Double-precision' floating point number 4 8 Signed: -128 to 127 (-2 (8-1) to 2 (8-1) -1) Unsigned: 0 to 255 (0 to 2 8-1) Signed: -32768 to 32767 Unsigned: 0 to 65535 Signed: -32768 to 32767 Unsigned: 0 to 65535 Signed: -2,147,483,648 to 2,147,483,647 Unsigned: 0 to 4,294,967,295 Signed: -9.2E+18 to 9.2E+18 Unsigned: 0 to 1.8E+19 1E 38 (7 decimal digits of precision) 1E 38 (7 decimal digits of precision) Signed: -128 to 127 Unsigned: 0 to 255 Signed: -32,768 to 32,767 Unsigned: 0 to 65535 Signed: -2,147,483,648 to 2,147,483,647 Unsigned: 0 to 4,294,967,295 Signed: -2,147,483,648 to 2,147,483,647 Unsigned: 0 to 4,294,967,295 Signed: -9.2E+18 to 9.2E+18 Unsigned: 0 to 1.8E+19 1E 38 (7 decimal digits of precision) 1E 308 (15 decimal digits of precision) long double Long double-precision floating point number 8 1E 308 (15 decimal digits of precision) 17

18

Arithmetic Rules of operator precedence Operators in parentheses evaluated first Nested/embedded parentheses Operators in innermost pair first Multiplication, division, modulus applied next Operators applied from left to right Addition, subtraction applied last Operators applied from left to right 19

Comments Non - executable statements Comments are used for program documentation Two formats Single Line Comments Multi Lines Comments // This program is used to show the Welcome Message. /* This program is used to show the square of even numbers from 10 to 100. */ 20

Tokens Tokens are individual words and punctuation marks in passage of text. In C++, program the smallest individual units are known as C Tokens. C++ has Six types of Tokens. The Tokens are shown in figure. C++ programs are written using these tokens and the syntax of the language. 21

22

C++ keywords Each keyword has a predefined purpose in the language. Do not use keywords as variable and constant names!! Exmples: bool, break, case, char, const, continue, do, default, double, else, extern, false, float, for, if, int, long, namespace, return, short, static, struct, switch, typedef, true, unsigned, void, while etc.. etc 23

Thank You New Computer Scientists 24

Memory Concepts Variable names Correspond to actual locations in computer's memory Every variable has name, type, size and value When new value placed into variable, overwrites previous value 25