.Open 08 Control Structures . Previous -- classes and objects .See ./07.html .Open Prepare . 4.1 Introduction Here we look at more advanced techniques for coding our `main()` functions and `member functions` in our classes. 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 .List Sequence {....} Selection if(...).... else .... Iteration while(....) ... .Close.List . 4.5 if Selection Statement . 4.6 if...else Double-Selection Statement Two way choices: this or that. .As_is if ( condition ) .As_is statement executed if condition was true .As_is else .As_is statement executed if condition was false Example: We want to count the number of odd and even numbers supplied by our user: .As_is if( i % 2 == 0 ) .As_is odd=odd+1; .As_is else .As_is even=even+1; Exercise: There is a logic error above.... what is it? . 4.7 while Repetition Statement .As_is while( condition ) .As_is 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. .As_is ... .As_is cin>>i; .As_is while (i > 0 ) .As_is { .As_is if(i%2==0) .As_is even=even+1; .As_is else .As_is odd=odd+1; .As_is cin>>i; .As_is } .As_is cout << "#odd " << odd << "#even "<< even << endl; .As_is ... .Close Prepare . Deliver a question + P4 Project iteration 4 should have a print out of a program... complete with errors, probably. .Open Questions and Answers . What are the differences between float and double Both have addition, subtraction, multiplication, division, etc.... But this is because float numbers are converted to doubles before the arithmetic is done! Double take twice the space when the number is stored. Float is less accurate and doesn't have as large a range of numbers. So you are more likely to have errors with `float` than with `double`. We used `float` when RAM was expensive and measured in Kb not Gb. 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 .See [BohmJaccopini66] and (on campus) to the Assoc Computer Machinery's digital library at .See http://doi.acm.org/10.1145/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 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: .As_is assert(x>=0); . How do you compile Pseudocode You can't because it is a `fake` code. It is read and written by humans, for humans, only. 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. .As_is /*Algorithm .As_is 1. Open data file .As_is ... .As_is */ . When is it worth writing Pseudocode Any time you are not sure how to code something.... write it in strict English first and then translate it by hand into C++ or a real code. . 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: .As_is switch(condition) .As_is { .As_is case true: true_part; break; .As_is default: false_part; .As_is } .Close .Open Exercises Without compiling and running this program .See ./hailstone.cpp and using pencil and paper, predict what it outputs. .Close . Next -- For statements .See ./09.html . Quiz 3 -- Classes and Objects .Close 08 Control Structures