Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Similar documents
CS242 COMPUTER PROGRAMMING

2 nd Week Lecture Notes

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

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

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

UNIT- 3 Introduction to C++

Computer Programming : C++

Creating a C++ Program

A First Program - Greeting.cpp

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

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

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

Objectives. In this chapter, you will:

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

The C++ Language. Arizona State University 1

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

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

LECTURE 02 INTRODUCTION TO C++

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

CSCE 110 PROGRAMMING FUNDAMENTALS

Introduction to Programming

Programming. C++ Basics

Lecture 3 Tao Wang 1

Chapter 1 Introduction to Computers and C++ Programming

BITG 1233: Introduction to C++

WARM UP LESSONS BARE BASICS

Your First C++ Program. September 1, 2010

Getting started with C++ (Part 2)

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

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

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.

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

Introduction to Programming EC-105. Lecture 2

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

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

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

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Chapter 2: Introduction to C++

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

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

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

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

BASIC ELEMENTS OF A COMPUTER PROGRAM

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Chapter 2: Overview of C++

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

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

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

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

Engineering Problem Solving with C++, Etter/Ingber

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

UNIT-2 Introduction to C++

CSCE Practice Midterm. Data Types

C++ Final Exam 2017/2018

CMPE110 - EXPERIMENT 1 * MICROSOFT VISUAL STUDIO AND C++ PROGRAMMING

Fundamentals of Programming CS-110. Lecture 2

CSc Introduction to Computing

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

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

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

Module 7 b. -Namespaces -Exceptions handling

CHAPTER-6 GETTING STARTED WITH C++

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Fundamental of Programming (C)

Another Simple Program: Adding Two Integers

CHAPTER 3 BASIC INSTRUCTION OF C++

Ch 6. Functions. Example: function calls function

a. a * c - 10 = b. a % b + (a * d) + 7 =

I/O Streams and Standard I/O Devices (cont d.)

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

Homework #3 CS2255 Fall 2012

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

C: How to Program. Week /Mar/05

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Differentiate Between Keywords and Identifiers

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

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

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Integer Data Types. Data Type. Data Types. int, short int, long int

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

Chapter 2 - Introduction to C Programming

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

Chapter-9 INPUT AND OUTPUT OPERATORS

Topic 2: C++ Programming fundamentals

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Computers and Programming Section 450. Lab #1 C# Basic. Student ID Name Signature

Fundamentals of Programming

Introduction to the C++ Programming Language

Chapter 1 INTRODUCTION

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 2

A SHORT COURSE ON C++

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

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Chapter 2 Basic Elements of C++

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

Transcription:

1 Statements: A statement in a computer carries out some action. There are three types of statements used in C++; they are expression statement, compound statement and control statement. Expression statement Compound statement Control statement x=y; sum=x+y; a=b+c; x=x*x; y=a+x; If (a>b) a=i; k=a+1; 2 Getting Started with C++: The skeleton of a typical C++ program structure is given below: Program heading Begin Type or variable declaration Statements of operation Results end Lecture 4 The keyboard and screen I/O instructions in C++ are: (a): COUT/ display an object onto the video screen: Cout<<var.1<<var2<< <<var.n; (b): Cin/ It is used to read an object from a standard input device (keyboard): Cin>>var.1>>var.2>> >>var.n; To begin learning C++ lets examine our first C++ Program:

Example 1 // A program to print welcome cout << Welcome ; this line is for pre-processor directive. Any begins with # is processed before the program is compiled. C++ programs must be start with #include. Every group of related functions is stored in a separate library called (header file).to use the cin and cout, must include the header file iostream. main( ), is the name of C++ function. Every C++ program must have a function called main. void, is the return type of the main function. When the return type of a function is void, this function will not passes back any value to the calling function. Some programmers use int as a return type for the main function, in this case a return(0) statement must be written as a last statement of the main function-body., introducing the statements that define the function., indicates the end of the statements in the function. //, text after these symbols is a comment. It does not affect the program code, and compilers normally ignore it. cout, the input stream object. It passes the characters quotes ( ) to the terminal screen. cin, the input stream object. It reads the input values from the keyboard.

<<, the stream insertion operator (or send operator). >>, the stream extraction operator (or get from operator). ;, semicolon, the terminator of every C++ statement. The endl is used in c++ to represent a new line, as shown in the following example: Example 2 cout << hallow << endl; cout << students ; The \n is a special escape code, also used in C++ to represent a new line, as shown in the following example: Example 3 cout << hallow \n ; cout << students ;

3 Variables Declaration: A declaration is a process of naming the variables and their statements datatypes in C++. C++ allows declaration of the variables before and after executable statements. A variable ia an object that may be take on values of the specified type. Also,a variable is a location in the computer s memory where a value can be stored for later use by the program. Variables are like buckets that hold data. These data buckets are really locations in the computer s memory. The variable must be declared by specifying the datatype and the identifier. datatype id.1, id2,,idn; A variable defined by stating its type, followed by one or more spaces, followed by the one or more variable names separated by commas, then followed by semicolon. For example: unsigned short Int X; float Y; char A, a, c; Note: C++ does distinguish between above A and a variables (C++ is case-sensitive).

Example 4 The following program reads three different inputs and outputs it. int num=3; cout << number= <<num<< \n ; char ch= a ; cout << character= <<ch<< \n ; float fa=-34.45; cout<< real number= <<fa<< \n ; Output: Number=3 Character=a Real number=34.45 Example 5 The following program reads three different inputs and outputs it. int n; float f; char c; cout << input integer number: ; cin>>n; cout<<endl; cout << input decimal number: ; cin>>f; cout<<endl; cout << input character: ; cin>>c; 4 Constants: Like variables, constants are data storage locations. Unlike variables, and as the name implies, constants don t change. const int myage=23; const double pi=3.14; const float salary=20.5;

Example 6 Write a program that reads the radius of a circle, then computes and outputs its area. const float pi = 3.14; int r; float c; cout << enter the radius of circle: ; cin>>r; cout<<endl; c = r * r * pi; cout << the area of circle: << c; Example 7 The following program computes the arethmatic operators. int a,b,sum,sub,mul,div; cout << enter any two numbers<<endl; cin>> a>>b; sum=a+b; sub=a-b; mul=a*b; div=a/b; cout<< a= <<a<< b= <<b<< sum= <<sum<<endl; cout<< sub= <<sub<<endl; cout<< mul= <<mul<<endl; cout<< div= <<div<<endl; Output: Enter any two numbers 10 20 A=10 b=20 sum=30 Sub=-10 Mul=200 Div=0

Example 8 The following program computes different division operators. int x, y, z, r ; x= 7 / 2; cout << "x=" << x <<endl; y=17/(-3); cout << "y="<< y <<endl; z=-17/3; cout << "z="<< z <<endl; r=-17/(-3); cout << "r="<< r <<endl; The modulus operator % is used with integer operands (int, short, long, unsigned). It can t be used with float or double operands. Example 9 int y1, y2; y1 = 8 % 3; y2 = -17 % 3; cout << "y1="<< y1 <<endl; cout << "y2="<< y2 <<endl;