>), and assignment operators (=) to write a program that gets user input and calculates the sum of two integers. 4) Additional C++ concepts like arithmetic operators, memory, variables, and if statements to test conditions using relational and equality operators.">>), and assignment operators (=) to write a program that gets user input and calculates the sum of two integers. 4) Additional C++ concepts like arithmetic operators, memory, variables, and if statements to test conditions using relational and equality operators.">
[go: up one dir, main page]

0% found this document useful (0 votes)
65 views18 pages

ITC Lec 08 C++

This document provides an overview of a lecture on C++ programming. It discusses: 1) The structure of a simple C++ program that prints "Welcome to C++" including main functions, comments, preprocessor directives, and return statements. 2) How to use stream insertion operators (<<), escape characters, and multiple statements to print text to the screen. 3) How to use variables, data types like int, stream extraction operators (>>), and assignment operators (=) to write a program that gets user input and calculates the sum of two integers. 4) Additional C++ concepts like arithmetic operators, memory, variables, and if statements to test conditions using relational and equality operators.

Uploaded by

Anas Asif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views18 pages

ITC Lec 08 C++

This document provides an overview of a lecture on C++ programming. It discusses: 1) The structure of a simple C++ program that prints "Welcome to C++" including main functions, comments, preprocessor directives, and return statements. 2) How to use stream insertion operators (<<), escape characters, and multiple statements to print text to the screen. 3) How to use variables, data types like int, stream extraction operators (>>), and assignment operators (=) to write a program that gets user input and calculates the sum of two integers. 4) Additional C++ concepts like arithmetic operators, memory, variables, and if statements to test conditions using relational and equality operators.

Uploaded by

Anas Asif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Lecture 08: C++

CS 101: Introduction to Computing

C++

Saima Jabeen

1 // Fig. 1.2: fig01_02.cpp

Lecture 08: C++

2 // A first program in C++


3 #include <iostream>
4
5 int main()

CS 101: Introduction to Computing


Comments
Written between /* and */ or following
a //.
Improve program readability and do not
cause the computer to perform any action.

6 {

preprocessor directive
7
std::cout << "Welcome to C++!\n";Message to the C++ preprocessor.
Lines beginning with # are preprocessor
8
directives.
9
return 0;
// indicate that program
ended
successfully
#include
<iostream>
tells the
preprocessor to include the contents of the
10 }
C++
programs contain
or more
file <iostream>,
which one
includes
functions,
oneoperations
of which must
input/output
(suchbeasmain
printing
Parenthesis
are
used
to
indicate
a
function
to the screen).
Welcome to C++!
int means that main "returns" an integer
value. More
in Chapter 3.
Prints the string of characters
contained
between
the quotation marks.
return is a way
to exit a
function from a function.
A left
brace { begins
Thecase,
entire
line, including
std::cout,
the <<the body of
return 0, in this
means
every function
and a right brace }
operator,
the string "Welcome
to C++!\n"
that the program
terminated
it. a statement.
and the semicolon (;),ends
is called
normally.
All statements must end with a semicolon.

Lec08: C++

CS 101: Introduction to Computing

A Simple Program:
Printing a Line of Text

std::cout

Standard output stream object

Connected to the screen

std:: specifies the "namespace" which cout belongs to


std:: can be removed through the use of using statements

<<

Stream insertion operator

Value to the right of the operator (right operand) inserted into


output stream (which is connected to the screen)

std::cout << Welcome to C++!\n;

Escape character

Indicates that a special character is to be output

Lec08: C++

CS 101: Introduction to Computing

Simple Program:
Printing a Line of Text

Escape Sequence

Description

\n

Newline. Position the screen cursor to the beginning


of the next line.
Horizontal tab. Move the screen cursor to the next
tab stop.

\t
\r

\a
\\
\"

Carriage return. Position the screen cursor to the


beginning of the current line; do not advance to the
next line.
Alert. Sound the system bell.
Backslash. Used to print a backslash character.
Double quote. Used to print a double quote
character.

There are multiple ways to print the text

Following are more examples


4

Lec08: C++

CS 101: Introduction to Computing

1 // Fig. 1.4: fig01_04.cpp


2 // Printing a line with multiple statements
3 #include <iostream>
4
5 int main()
6 {
7

std::cout << "Welcome ";

std::cout << "to C++!\n";

9
10

return 0;

// indicate that program ended successfully

11 }

Welcome to C++!

Unless new line '\n' is specified, the text


continues on the same line.

Lec08: C++

CS 101: Introduction to Computing

1 // Fig. 1.5: fig01_05.cpp


2 // Printing multiple lines with a single statement
3 #include <iostream>
4
5 int main()
6 {
7

std::cout << "Welcome\nto\n\nC++!\n";

8
9

return 0;

// indicate that program ended successfully

10 }

Welcome
to
C++!

Multiple lines can be printed with one


statement.
6

Lec08: C++

Variables

CS 101: Introduction to Computing

Another Simple Program:


Adding Two Integers

Location in memory where a value can be stored for use by a


program
Must be declared with a name and a data type before they can be
used
Some common data types are:

int - integer numbers


char characters
double - floating point numbers

Example: int myvariable;

Declares a variable named myvariable of type


int

Example: int variable1, variable2;

Declares two variables, each of type int


7

Lec08: C++

CS 101: Introduction to Computing

Another Simple Program:


Adding Two Integers
>> (stream extraction operator)
When used with std::cin, waits for the user to input a value and
stores the value in the variable to the right of the operator
The user types a value, then presses the Enter (Return) key to send
the data to the computer
Example:
int myVariable;
std::cin >> myVariable;

Waits for user


myVariable

input,

then

stores

input

in

= (assignment operator)
Assigns value to a variable
Binary operator (has two operands)
Example:
sum = variable1 + variable2;
8

Lec08: C++

CS 101: Introduction to Computing

1 // Fig. 1.6: fig01_06.cpp


2 // Addition program
3 #include <iostream>
4
5 int main()
6 {
7
int integer1, integer2, sum;
// declaration
8
9
std::cout << "Enter first integer\n"; Notice
// prompt
how std::cin is used to get user
10
std::cin >> integer1;
// read an
input.
integer
11
std::cout << "Enter second integer\n"; // prompt
12
std::cin >> integer2;
// read an
integer
13
sum = integer1 + integer2;
// assignment of
sum
14
std::cout << "Sum is " << sum << std::endl; // print sum
15
16
return 0;
// indicate that program ended successfully
std::endl flushes the buffer
and prints a newline.
17 }
Enter first integer
Variables can be output using std::cout << variableName.
45
Enter second integer
72
Sum is 117
9

Lec08: C++

CS 101: Introduction to Computing

Memory Concepts
Variable names

Correspond to locations in the computer's memory


Every variable has a name, a type, a size and a value
Whenever a new value is placed into a variable, it replaces the
previous value - it is destroyed
Reading variables from memory does not change them

A visual representation

integer1

45

10

Lec08: C++

CS 101: Introduction to Computing

Arithmetic
Arithmetic calculations

Use * for multiplication and / for division


Integer division truncates remainder
7 / 5 evaluates to 1
Modulus operator returns the remainder
7 % 5 evaluates to 2

Operator precedence

Some arithmetic operators act before others (i.e., multiplication


before addition)
Be sure to use parenthesis when needed

Example: Find the average of three variables a, b and c


Do not use: a + b + c / 3
Use: (a + b + c ) / 3

11

Lec08: C++

CS 101: Introduction to Computing

Arithmetic
Arithmetic operators:

C++ operation Arithmetic Algebraic


operator
expression
+
Addition
f+7

C++ expression

Subtraction

pc

p - c

Multiplication

bm

b * m

Division

x/y

x / y

Modulus

r mod s

r % s

f + 7

Rules of operator precedence:

Operator(s)

Operation(s)

Order of evaluation (precedence)

()

Parentheses

Evaluated first. If the parentheses are nested, the


expression in the innermost pair is evaluated first. If there
are several pairs of parentheses on the same level (i.e.,
not nested), they are evaluated left to right.

*, /, or %

Multiplication Division Evaluated second. If there are several, they re


Modulus
evaluated left to right.

+ or -

Addition
Subtraction

Evaluated last. If there are several, they are


evaluated left to right.
12

Lec08: C++

CS 101: Introduction to Computing

Decision Making: Equality and Relational


Operators
if structure
Test conditions truth or falsity. If condition met execute, otherwise
ignore

Equality and relational operators


Lower precedence than arithmetic operators

Table of relational operators on next slide

13

Lec08: C++

CS 101: Introduction to Computing

Decision Making: Equality and Relational


Operators
Standard algebraic
equality operator or
relational operator

C++ equality
or relational
operator

Example
of C++
condition

Meaning of
C++ condition

>

>

x > y

x is greater than y

<

<

x < y

x is less than y

>=

x >= y

x is greater than or equal to y

<=

x <= y

x is less than or equal to y

==

x == y

x is equal to y

!=

x != y

x is not equal to y

Relational operators

Equality operators

14

Lec08: C++

CS 101: Introduction to Computing

Decision Making: Equality and Relational


Operators
using statements
Eliminate the need to use the std:: prefix
Allow us to write cout instead of std::cout
To use the following functions without the std:: prefix, write the
following at the top of the program
using std::cout;
using std::cin;
using std::endl;

15

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

// Fig. 1.14: fig01_14.cpp


Lec08:
C++
// Using
if statements,
relational
// operators, and equality operators
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

// program uses cout


// program uses cin
// program uses endl

CS 101: Introduction to Computing

Notice the using statements.

int main()
{
int num1, num2;
cout << "Enter two integers, and I will tell you\n"
<< "the relationships they satisfy: ";
cin >> num1 >> num2;
// read two
integers
Enter
two integers, and I will tell you

the relationships they satisfy: 3 7

if ( num1 == num2 )
cout << num1 << " is equal to " << num2 << endl;

if ( num1 != num2 )
cout << num1 << " is not equal to " << num2 << endl;
if ( num1 < num2 )
cout << num1 << " is less than " << num2 << endl;

The if statements test the truth


of the condition. If it is true,
body of if statement is executed.
If not, body is skipped.
3 is
not equal to 7
To include multiple statements in
a body, delineate them with
braces {}.

3 is less than 7

if ( num1 > num2 )


cout << num1 << " is greater than " << num2 << endl;
if ( num1 <= num2 )
cout << num1 << " is less than or equal to "
<< num2 << endl;

3 is less than or equal to 7


16

34

if ( num1 >= num2 )

Lec08: C++
35
36

CS 101: Introduction to Computing

cout << num1 << " is greater than or equal to "


<< num2 << endl;

37
38

return 0;

// indicate that program ended successfully

39 }

Enter two integers, and I will tell you


the relationships they satisfy: 3 7
3 is not equal to 7
3 is less than 7
3 is less than or equal to 7
Enter two integers, and I will tell you
the relationships they satisfy: 22 12
22 is not equal to 12
22 is greater than 12
22 is greater than or equal to 12
Enter two integers, and I will tell you
the relationships they satisfy: 7 7
7 is equal to 7
7 is less than or equal to 7
7 is greater than or equal to 7
17

Lec08: C++

CS 101: Introduction to Computing

References
Dietal and Dietal : How to Program C++
3rd Edition

18

You might also like