>), 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.">
ITC Lec 08 C++
ITC Lec 08 C++
C++
Saima Jabeen
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++
A Simple Program:
Printing a Line of Text
std::cout
<<
Escape character
Lec08: C++
Simple Program:
Printing a Line of Text
Escape Sequence
Description
\n
\t
\r
\a
\\
\"
Lec08: C++
9
10
return 0;
11 }
Welcome to C++!
Lec08: C++
8
9
return 0;
10 }
Welcome
to
C++!
Lec08: C++
Variables
Lec08: C++
input,
then
stores
input
in
= (assignment operator)
Assigns value to a variable
Binary operator (has two operands)
Example:
sum = variable1 + variable2;
8
Lec08: C++
Lec08: C++
Memory Concepts
Variable names
A visual representation
integer1
45
10
Lec08: C++
Arithmetic
Arithmetic calculations
Operator precedence
11
Lec08: C++
Arithmetic
Arithmetic operators:
C++ expression
Subtraction
pc
p - c
Multiplication
bm
b * m
Division
x/y
x / y
Modulus
r mod s
r % s
f + 7
Operator(s)
Operation(s)
()
Parentheses
*, /, or %
+ or -
Addition
Subtraction
Lec08: C++
13
Lec08: C++
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 <= y
==
x == y
x is equal to y
!=
x != y
x is not equal to y
Relational operators
Equality operators
14
Lec08: C++
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
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
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;
3 is less than 7
34
Lec08: C++
35
36
37
38
return 0;
39 }
Lec08: C++
References
Dietal and Dietal : How to Program C++
3rd Edition
18