Lecture 3. C++ Basics and Flow Control
Lecture 3. C++ Basics and Flow Control
Department
Computer Programming
(MEng 1052)
Lecture 3
C++ Basics
February, 2015
Sample Dialogue
Enter the number of candy bars in a package
and the weight in ounces of one candy bar.
Then press return.
11 2.1
11 candy bars
2.1 ounces each
Total weight is 23.1 ounces.
Variable Declarations
Every variable in a C++ program must be declared. When
Variable Declarations
The word int in the first of these two declarations is an
11
Variable Declarations
Every variable in a C++ program must be declared before
12
Variable Declarations
Syntax
The syntax for a programming language (or any other kind of
Assignment Statements
The most direct way to change the value of a variable is to use
14
an assignment statement.
An assignment statement is an order to the computer saying,
set the value of this variable to what I have written down.
The following line from the sample program is an example of
an assignment statement:
total_weight = one_weight * number_of_bars;
This assignment statement tells the computer to set the value
of total_weight equal to the number in the variable one_weight
multiplied by the number in number_of_bars.
An assignment statement always consists of a variable on the
left-hand side of the equal sign and an expression on the righthand side
Assignment Statements
An assignment statement ends with a semicolon.
The expression on the right-hand side of the equal sign may be
Assignment Statements
As another example, the following assignment statement
16
Assignment Statements
17
18
20
screen, you will have to place such a long cout statement on two
or more lines.
A better way to write the previous long cout statement is:
You should not break a quoted string across two lines, but
otherwise you can start a new line anywhere you can insert a
space.
The computer does not insert any extra space before or after the
items output by a cout statement. That is why the quoted strings
in the samples often start and/or end with a blank.
The blanks keep the various strings and numbers from running
together.
22
you want to insert the space, then use a string that contains
only a space, as in the following:
\n tells the computer to start a new line of output. Unless you
tell the computer to go to the next line, it will put all the output
on the same line.
If you wish to insert a blank line in the output, you can output
the newline character \n by itself:
Another way to output a blank line is to use endl, which means
lines:
These two lines make the library iostream available. This is the
the std (standard) namespace. This means that the names you
use will have the meaning defined for them in the std
namespace.
25
26
27
28
29
30
Number Types
31
33
34
Arithmetic Expressions
35
36
type int, numbers of type double, and even with one number
of each type.
If both operands (that is, both numbers) are of type int, then
the result of combining them with an arithmetic operator is of
type int.
If one, or both, of the operands is of type double, then the
result is of type double. This is also true for all operators +,
, *, or /.
For example, 7.0/2 has one operand of type double, namely
7.0. Hence, the result is the type double number 3.5.
However, 7/2 has two operands of type int and so it yields the
type int result 3.
37
When used with negative values of type int, the result of the
39
41
FLOW OF CONTROL
42
43
44
45
46
47
48
49
50
51
52
53
54
2.
55
3.
56
number of times.
A portion of a program that repeats a statement or group of
statements is called a loop.
The C++ language has a number of ways to create loops.
One of these constructions is called a while statement or
while loop.
57
59
60
A while loop
The portion between the braces, { and }, is called the body
61
A while loop
Lets consider the first sample dialogue and see how the while
loop performs.
The user types in 3 so the cin statement sets the value of
count_down to 3.
Thus, in this case, when the program reaches the while
statement, it is certainly true that count_down is greater than 0,
so the statements in the loop body are executed.
Every time the loop body is repeated, the following two
statements are executed:
A while loop
After the computer repeats the loop body three times, the value
A do-while loop
A while loop might execute its loop body zero times.
A do-while statement is similar to a while statement except that
64
65
66
A do-while loop
In a do-while loop, the first thing that happens is that the
67
68
69
$50 and suppose the bank charges you 2% per month interest.
How many months can you let pass without making any
payments before your balance owed will exceed $100?
After one month the balance would be $50 plus 2% of $50,
which is $51. After two months the balance would be $51 plus
2% of $51, which is $52.02. After three months the balance
would be $52.02 plus 2% of $52.02, and so on.
In general, each month increases the balance by 2%. The
program could keep track of the balance by storing it in a
variable called balance. The change in the value of balance for
one month can be calculated as follows:
70
71
72
73
Exercises
1.
Answer
74
Exercises
2.
Exercises
3.
76
Comments
In order to make a program understandable, you should
77
Comments
There is another way to insert comments in a C++ program.
78
79
80
Example
Write a
complete C++
program that
asks the user
for a number
of gallons and
then outputs
the equivalent
number of
liters. There
are 3.78533
liters in a
gallon. Use a
declared
constant.
81
82
83
84
85
86
87
branches.
The switch statement is another kind of C++ statement that
also implements multiway branches.
When a switch statement is executed, one of a number of
different branches is executed.
The choice of which branch to execute is determined by a
controlling expression given in parentheses after the
keyword switch.
88
90
91
92
93
94
The n <= 10 says the loop will continue to iterate the body as
95
96
97
98
99
100
Exercise
What is the output of the following (when embedded in a
complete program)?
Answer
101
End of Lecture 3
Next Lecture
Lecture 4: Functions
102