You are on page 1of 21

Introduction to C++ Programming

First Program in C++: Printing a Line of Text

#include <iostream>
Line 3
#include <iostream> // allows rogram to out ut data to t!e screen notifies t!e re rocessor to include in t!e rogram t!e contents of t!e input/output stream header file <iostream>

int main"#
Line $ int main"#
is a art of e%er& C++ rogram' T!e arent!eses after main indicate t!at main is a rogram (uilding (loc) called a function T!e left (race* +* "line ,# must (egin t!e body of e%er& function' corres onding rig!t (race* .* "line /0# must end eac! function1s (od&

std::cout << 23elcome to C++45n26


Line 7

std::cout << 23elcome to C++45n26 // dis la& message

instructs t!e com uter to perform an action namel&* to rint t!e string of c!aracters contained (etween t!e dou(le 8uotation mar)s' T!e entire line 7* including std::cout* t!e << o erator* t!e string 23elcome to C++45n2 and t!e semicolon "6#* is called a statement' T!e notation std::cout s ecifies t!at we are using a name* in t!is case cout* t!at (elongs to 2names ace2 std'

9sca e se8uences

:&ntax 9rror

Printing a line of text wit! multi le statements'

Printing multi le lines of text wit! a single statement'

-not!er C++ Program: -dding Integers

;emor& Conce ts
<aria(le names suc! as num(er/* num(er0 and sum actuall& corres ond to locations in t!e com uter1s memor&'
std::cin >> num(er/6 // read first integer from user into num(er/

3!en executed* t!e c!aracters t& ed (& t!e user are con%erted to an integer t!at is laced into a memor& location to w!ic! t!e name num(er/ !as (een assigned (& t!e C++ com iler'

3!ene%er a %alue is laced in a memor& location* t!e %alue o%erwrites t!e re%ious %alue in t!at location6 t!us* lacing a new %alue into a memor& location is said to (e destructive'

;emor& Conce ts "cont''#


std::cin >> num(er06 // read second integer from user into num(er0

sum = num(er/ + num(er06 // add t!e num(ers6 store result in sum

3!en a %alue is read out of a memor& location* t!e rocess is nondestructive'

-rit!metic o erators

Precedence of arit!metic o erators

-cti%it& /

9%aluate: &=0>?>?+3>?+,

:olution

@ecision ;a)ing: 98ualit& and Aelational B erators


Conditions in if statements can (e formed (& using t!e equality operators and relational operators' allows a rogram to ma)e a decision (ased on t!e trut! or falsit& of some condition T!e relational o erators all !a%e t!e same le%el of recedence and associate left to rig!t T!e e8ualit& o erators (ot! !a%e t!e same le%el of recedence* w!ic! is lower t!an t!at of t!e relational o erators* and associate left to rig!t'

98ualit& and relational o erators

98ualit& and relational o erators

cont''

But ut

You might also like