Tracing a program will now involve tracing instructions that are repeated
many times.
4.2 Algorithms
An algorithm is a precise description or recipe for achieving some goal,
given some data. The description need to be precise enough that a machine
can carry out the instructions.
Algorithms often have complex structure.
4.3 Pseudocode
Pseudo code was used after flow charts went out of fashion in the 1980's.
4.4 Control Structures
There are three classic types of control structure
if ( condition )
statement executed if condition was true
else
statement executed if condition was false
Example: We want to count the number of odd and even numbers supplied by our user:
if( i % 2 == 0 )
odd=odd+1;
else
even=even+1;
Exercise: There is a logic error above.... what is it?
4.7 while Repetition Statement
while( condition )
statement repeated if condition is true and until it becomes false.
Example -- we want to read a series of numbers until one is less than or equal to zero and count the number of odd and even numbers.
...
cin>>i;
while (i > 0 )
{
if(i%2==0)
even=even+1;
else
odd=odd+1;
cin>>i;
}
cout << "#odd " << odd << "#even "<< even << endl;
...
. . . . . . . . . ( end of section Prepare) <<Contents | End>>
Deliver a question + P4
Project iteration 4 should have a print out of a program... complete with errors,
probably.
Questions and Answers
These days..... use double for all measurements and int for counting.
We have if and else clauses, is there a then clause
No.
Have there been calls for other control structures
Yes. But they haven't survived the consensus. When an idea has
lots of support it gets put into a language like C++.
Indeed the tendency has been to reduce the number of control structures.
One thing -- we have three or four statements (break, raise, goto, return,...) which modify the standard behaviors of the three structures. More later in this class and/or CS202.
The number of structures is pretty much standard these days.
Can you prove that only Sequence + Selection + Iteration are needed
The proof takes several pages and the use of Boolean variables (later). You
can find it by following this link
[BohmJaccopini66]
and (on campus) to the Assoc Computer Machinery's digital library at
[ 355592.365646 ]
(off campus -- sign into library with your Coyote ID first).
Other than if and else is there another way to check a condition
We use switch to choose between a set values. The rest of the control structures
give you loops rather than choices.
There is a function in the <cassert> library that can checks is a condition is true, and if not, aborts the program. For example if I'm about the calculate the square root of an unknown number x then I should put this command first:
assert(x>=0);
As a result Pseudocode omits a lot of things that compilers need -- declarations, correct syntax, horrible details, predefined commands, etc.
How do you type Pseudocode
You can use any word processor or editor.
I often put my Pseudocode in a comment at the start of a function with the heading: Algorithm.
/*Algorithm
1. Open data file
...
*/
Can an if select several lines without using braces
NO!
Can you use while for anything except repeating statement
I don't think so.
Can you put while inside a while
Yes.
You can any control structure inside another one... and so on. Example in lab05.
Can you make C++ start executing the last statement in the program
No.
The best you can do is to start with a command that jumps, somehow, to the last one. But C++ programs
always start executing at "main".
Do if-else and switch work the same way
No. If-else is always 2 way and switch is n-way. The machine code is quite different. If-else
has a two way condition branch command (BNZ for example). A switch is translated by computing the address of the next
statement from the value in the switch.
However, you can fake an if_else with a case:
switch(condition)
{
case true: true_part; break;
default: false_part;
}
. . . . . . . . . ( end of section 08 Control Structures) <<Contents | End>>
Abbreviations