-
Set up a minimal C++ program.
-
A
computer
is a machine that follows instructions,
and the list of instructions you put into the computer is called a
program.
-
Why do we say the
#include
s?
What would go wrong if we didn’t?
(The
#include
s
are not needed for words that are built-in to the C++ language, such as
int
and
for
.
Needed for words that are not built-in to the C++ language, such as
cout
and
cin
,
EXIT_SUCCESS
and
EXIT_FAILURE
.)
-
Why do we say
using namespace std;
?
What would go wrong if we didn’t?
(See
dime1.C
vs.
dime4.C
.)
-
Why do we
return EXIT_SUCCESS
or
EXIT_FAILURE
?
-
Every statement in C++ ends with a semicolon,
just as every sentence in English ends with a period
(or exclamation point or question mark).
Why don’t we end every statement in C++ with a period
to make it more like English?
-
Indent every statement by a number of tabs
that is equal to the number of pairs of
{
cury braces}
that enclose the statement.
The
}
of a
for
loop is directly below the
f
.
See
for2.C
.
The
}
of an
if
statement is directly below the
i
.
See
if.C
.
The
}
of
main
(at the end of the program)
is directly below the
{
of
main
(at the top of the program).
See
dime1.C
.
-
Compile and execute a C++ program on our machine
storm.cis.fordham.edu
.
-
Use
wget
to download a C++ program from the web into your home directory on
storm.cis.fordham.edu
.
What does the
w
stand for?
- Use
c++
to translate a C++ program into the language of 1’s and 0’s
that the computer actually understands.
What file does this command create?
-
Execute a C++ program on
storm.cis.fordham.edu
and send the program’s output to the screen.
What does the
./
in front of the
a.out
mean?
jsmith@storm:~$ cd
jsmith@storm:~$ ./a.out
-
Use
>
to
execute a C++ program on
storm.cis.fordham.edu
and send the program’s output to a file in your home directory.
jsmith@storm:~$ cd
jsmith@storm:~$ ./a.out > filename.txt
-
Output with
cout
.
-
A string of characters is in
"
double quotes",
not
'
single quotes'.
See
dime1.C
.
-
Within the double quotes,
the invisible newline character has a backslash
(
\n
),
not a forward slash
(/n
),
and a lowercase n
,
not an uppercase N
.
-
Output two or more items with one
cout
statement.
Output the contents of a variable.
cout << price << "\n";
-
Variables.
-
In a single statement, we can
- Define a variable (create the variable)
- Declare the variable
(announce the name and data type of the variable)
- Initialize the variable
(put a value into the variable at the moment when the variable is born)
For example,
int i {10};
double price {29.95};
-
Do not put unpredictable garbage into a variable when the variable is born.
double price; //BAD: puts unpredictable garbage into the variable.
-
How long does a variable, once created, remain in existence?
- A variable created within the
{
curly braces}
of
main
lives until we
return
from
main
(with either
EXIT_SUCCESS
or
EXIT_FAILURE
),
with the following exception.
-
A variable created within the parentheses of a
for
loop lives only as long as we are going around and around the loop.
for (int i {0}; i < 10; ++i) {
cout << i << "\n";
}
//i no longer exists when we get to this point.
-
A variable created within the parentheses of a “range
for
loop”
is reborn every time around the loop.
-
Use an operator to change the contents of an existing variable.
For example,
i = 10; //assignment with a single, not a double, equal sign
i += 2;
++i;
cin >> i;
-
Perform input in five steps.
- Prompt the user with a question.
- Create a variable to receieve the input.
- Attempt to draw input from
cin
into the variable.
- Check if the input was successful. If not,
- Send an error message to
cerr
.
- Terminate the program prematurely with an exit status of
EXIT_FAILURE
.
-
Check if the value that the user input was valid.
If not,
- Send an error message to
cerr
.
- Terminate the program prematurely with an exit status of
EXIT_FAILURE
.
cout << "In what year were you born?\n";
int year {0};
cin >> year;
if (!cin) {
cerr << Sorry, that wasn't a number.\n";
return EXIT_FAILURE;
}
if (year < 1924 || year > 2024) { //The || means "or".
cerr << "Sorry, you could not have been born then.\n";
return EXIT_FAILURE;
}
Why doesn’t
this attempt
at input compile with
c++
?
-
Data types:
the two most important ones that we’ve seen are
int
vs.
double
.
-
When you perform arithmetic with
int
s,
the result is an
int
.
For example,
the value of the expression
i+j
is of data type
int
.
-
When you perform arithmetic with
double
s,
the result is a
double
.
For example, the value of the expression
d+e
is of data type
double
.
-
You need to know the data type of an expression,
because some operators (e.g., division)
give you different results depending on this data type.
//i and 3 are ints, so the expression i/3 is also an int.
//The value of this expression is therefore 3
int i {10};
cout << i/3 << "\n";
//d and 3.0 are doubles, so the expression d/3.0 is also a double.
//The value of this expression is therefore 3.33333
double d {10.0};
cout << d/3.0 << "\n";
- Expressions
- An expression is made of operators and operands.
- The operators are things like
+
-
*
/
%
=
<<
- The operands can be variables
(
i
j
k
)
or
literals
(10
20
30.75
).
-
How many operands can an operator have?
- Some operators are unary:
-i
- Most operators are binary:
i+j
- One operator we haven’t seen yet is ternary:
a?b:c
- Division, quotient, remainder.
Break a large quantity into a quotient and remainder
using
int
operations:
int pennies {613}; //the large quantity
int dollars {pennies / 100}; //quotient
int cents {pennies % 100}; //remainder
- Operators that can change the value of a variable.
For example,
+=
*=
++
>>
- Operator precedence:
a+b*c
- Operator associativity:
1-2-3
z = y = x
-
Typical question:
how many operators are in the following expression?
In what order are they executed, and why?
i = 10 * j
i = (10 * j) + 20
cout << 10 + 20 << "\n"
-
Control Structure
while
loops
(will not be a big topic on the midterm)
for
loops,
counting up and down (e.g., bottles of beer),
counting by ones or by tens.
We’ve done four things with
for
loops
(see below).
-
Nested
for
loops:
//Output a 10 by 10 square of X's.
//i is the row number, j is the column number.
for (int i {0}; i < 10; ++i) {
for (int j {0}; j < 10; ++j) {
cout << "X";
}
cout << "\n";
}
if
statements
-
Three pairs of comparison operators in the expression in the
parentheses of an
if
statement:
==
and
!=
<
and
>=
>
and
<=
Know the difference between the operators
=
and
==
.
-
Combine
if
statments with else
into a chain of else/if
s.
if (n > 0) {
cout << "The number is positive.\n";
} else if (n < 0) {
cout << "The number is negative.\n";
} else {
cout << "The number is zero.\n"; //if none of the above
}
-
Combine
if
statments by nesting
(and1.C
and
and2.C
)
just as we can combine
for
statements by nesting
(lucy2.C
).
-
Arrays will not be on the midterm,
because we haven’t covered them yet.
Four tasks we’ve done with for loops
//Output the same thing over and over.
for (int i {0}; i < 10; ++i) {
cout << "X";
}
cout << "\n";
//Output the series of values that we're looping through.
for (int i {0}; i < 10; ++i) {
cout << i << "\n";
}
//Add up the series of values that we're looping through.
int sum {0};
for (int i {0}; i < 10; ++i) {
sum += i; //means sum = sum + i
}
cout << "The total is " << sum << ".\n";
//Multiply a value by a certain factor over and over again.
double money {1000.00};
for (int i {0}; i < 10; ++i) {
money *= factor; //means money = money * factor
}
cout << "The money has grown to " << money << ".\n";