1
|
- Homework 2 is available and is due in three days (June 19th).
- Project 1 due in 6 days (June 23rd)
- Write a binomial root solver using the quadratic equation.
|
2
|
- You used an integrated development environment (emacs) to write,
compile, and run your program.
- You wrote C++ programs that:
- Declared variables
- Performed mathematical operations
- Read input, wrote output
- Performed boolean operations
- Made decisions (branched) based on boolean expressions
|
3
|
- You will learn how emacs makes programming easier (tabify, jump to
error, window control, etc).
- You will write C++ programs that do everything the programs from last
week did plus:
- Variable scoping
- Iterative control structures
- Other control structures such as “switch,” “break,” and “continue.”
- Use all the control structures in concert
|
4
|
- The scope of a variable x is the portion of the program from which x
can be “seen.”
- Up to this point we have only placed variable declarations at the start
of the program so that we wouldn’t have to worry about scope
resolution.
- With iterative control structures scope starts to become an issue.
|
5
|
- Braces are the most common way of defining the scope of a variable.
- Variables declared after an “{” are destroyed when the matching close
brace “}” is encountered.
- This allows us to return memory allocated to variables when we don’t
need them anymore.
|
6
|
- int main
- {
- {
- int x = 0;
- }
- cout << x << endl;
- return 0;
- }
|
7
|
- int main
- {
- int y = 0;
- {
- int x = 0;
- }
- cout << y << endl;
- return 0;
- }
|
8
|
- Scopes “{ … }” can be nested just like we have been doing with “if”
statements.
|
9
|
- {
- int x = 0;
- {
- int y = 0;
- // x exists, y exists
- }
- // x exists, y destroyed
- }
|
10
|
- {
- int x = 0;
- {
- int y = 0;
- // x exists, y exits, z does not yet exist
- }
- {
- int z = 0;
- // x exists, y destroyed, z exists
- }
- // x exists, y destroyed, z destroyed
- }
|
11
|
- Variable scope resolution can become tricky when there are two or more
variables with the same name in different scopes.
- The compiler would not have allowed this:
- int x = 0;
- int x = 1;
- We would have received a “variable redefinition” warning.
- But…
|
12
|
- … it does allow this:
- int x = 0;
- {
- int x = 1;
- }
- This is useful because we don’t want to have to use a different name
for every single variable in a program. Here one x is said to “shadow”
the other.
|
13
|
- If I use “x” at different points in the program I could be getting
different variables.
- int x = 0;
- // here x = 0
- {
- int x = 1;
- // here x = 1
- }
- // here x = 0
|
14
|
- When a name is used the name is searched for in the current scope
first, then the scope enclosing this one, then the one enclosing that
one, etc.
|
15
|
- There are lots of other kinds of scope rules and exceptions that we
will see throughout the semester (e.g.):
- Global scope (global variables)
- File scope
- Class scope
- Static Variables
|
16
|
- We can execute any algorithm with just “if” statements.
- But often we need to repeat the same piece of code over and over.
Sometimes thousands or millions of times. Writing the same code with if
statements alone in this case is impractical.
- Instead we use iterative control structures. Iteration is simply the
process of doing something over and over.
|
17
|
|
18
|
|
19
|
|
20
|
|
21
|
|
22
|
|
23
|
|
24
|
|
25
|
|
26
|
- Early programmers used goto statements to accomplish the same tasks as
while and for loops but there are many different ways to do the same
kind of loop with goto.
- For and while loops constrain the sorts of code you are likely to see
and make it easy to recognize right away what the programmer is trying
to do.
|
27
|
- Anything that can be done with a for loop can be done with a while loop,
and vice versa.
- While loops and for loops are equivalent.
|
28
|
- If they are equivalent whey bother with the for loop?
- When a programmer uses a for loop it almost always means they are
counting something: elements in a list, branches in a tree, it is almost
always something discrete.
- Programmers use a while loop when the terminating condition is not so
clear, for example reading input from a user.
|
29
|
- for ( init_variables; pre-condition; statements )
- {
- statements…
- }
- e.g.
- for ( int i = 0; i < 100; i++ )
- {
- statements…
- }
- Note: i only has scope within the loop
|
30
|
- for ( init; pre-condition; statements )
- {
- statements…
- }
- Init is executed only once when the loop starts. Pre-condition and
statements are executed on every loop.
- Pre-condition is executed at the start of the loop and “statements” are
evaluated at the end.
|
31
|
- for ( init; pre-condition; statements )
- {
- statements…
- }
- Init can have multiple declarations and statements can have many
statements separated by commas:
- for ( int i = 0, j=10; i < j; i += 5, j++ )
- {
- statements…
- }
|
32
|
- for (int i = 0; i < 100; i++ )
- {
- statements…
- }
- int i = 0;
- while( i < 100 )
- {
- statements…
- i++;
- }
|
33
|
- Note: Microsoft Visual C++ 6.0 does not use standard scoping and for
loop syntax. MSVC 6 allows you to initialize variables in the for loop
but not declare them there. This means they have scope outside the for
loop.
|
34
|
- int result = 1, base = 2, power = 0;
- cin >> power;
- for ( i = 1; i <= power; i++)
- {
- result = result * base;
- }
- cout << result;
|
35
|
- All control structures can be nested in C++.
- Typically a program will consist of an outer loop (usually a “while”
loop), that runs the program over and over until some condition is met
and…
- Selection control structures such as “if” statements that decide which
code to run.
|
36
|
- for ( int i = 0; i < 2; i++ )
- {
- for ( int j = 0; j < 4; j++)
- {
- cout << i << “ ”<< j << “ ”;
- }
- cout << endl;
- }
- 0 0 0 1 0 2 0 3
- 1 0 1 1 1 2 1 3
|