.Open pages 48-58 Loops . Podcst on reading .See ./06.mp3 .Open Reading -- pages 48-59 Pages 48 to 58 introduce how C++ expresses the third structure used to write algorithms: .List Sequence .As_is { sequence } Selection .As_is if ( condition ) { sequence } Iteration .As_is while ( condition ) { sequence } .As_is for (initial; condition; increment ) { sequence } .Close.List . The while-loop Syntax .As_is while ( A ) { B } .Image while.gif [While A is true repeat B] Example: .As_is double x=1, sum = 0; .As_is while( x > 0.0001) .As_is { .As_is sum = sum + x; .As_is x = x/2; .As_is } Exercise: find out what the above does. The loop has an expression that it treats as a condition. It evaluates this condition and if it is true then it enters the statement that follows, and then goes back to the condition again. Use while loops any time you need a program to repeat itself while some condition is true. . The for-loop A for-loop is a short way to write a common type of while-loop. .As_is for ( A; B; C ) { D } .Image for.gif [Starting with A until B is false do D and then C] Note the odd use of two semicolons (";") in the for loop syntax. There must be exactly two of them and they must be inside the parentheses that follow the reserved word "for". This syntax started with C and has been inhetitted by C++, Java, PHP, and other modern languages. Use for-loops for counting, incrementing, and scanning across data: .As_is int i=1, sum = 0; .As_is for( i = 1; i <=N; i++) .As_is { .As_is sum = sum + i; .As_is } (should add up all the numbers from 1 to N) . Nested loops etc You can have a loop inside a branch of an if. .As_is cout << "N= "; cin >> N; .As_is if( N > 0 ) .As_is { .As_is int i=1, sum = 0; .As_is for( i = 1; i <=N; i++) .As_is { .As_is sum = sum + i; .As_is } .As_is } You can have complete if-statements inside loops. .As_is for( int i=1; i<=10; i++ ) .As_is { .As_is if( i % 3 == 0) .As_is { .As_is cout << i << " divisible by 3 " << endl; .As_is } .As_is else .As_is { .As_is cout << i << " not divisible by 3 " << endl; .As_is } .As_is }//end for You can even have loops inside loops. .See ./mtable.cpp In a recent presentation a student described the work he did with biologists in Santa Barbara last summer (2007). He noted that the algorithms had for-loops nested 3 or 4 levels deep. There is no limit to this nesting. Key points: .Set Use the structure that reflects your algorithm. Use indenting to display the nesting. .Close.Set . Hint -- do not use break The C++ `break;` command leads to confusing programs that sometimes have lead to disasters -- like, once, shutting down all the telephones in a part of New York one day! . Trap -- loops that never halt Take time to prove to yourself that every loop in every program can only repeat a finite number of times. Otherwise you have a program that runs forever -- not commonly useful. We have Junior and Senior level courses that show how to estimate how long an algorithm will take -- the analysis of algorithms. . Relation between for and while In theory you can replace .As_is for ( A; B; C) { D } by .As_is { A; .As_is while ( B ) .As_is { .As_is D .As_is C; .As_is } .As_is } But in practice -- learn to use the for loop. . Abbreviated for loops You can omit each or all of the three parts of a for-loop and it will compile and run. A missing condition is taken to be `true`. . Hint -- Write an algorithm first .Close .Open Syntax of for and while .As_is while ( condition ) { body } .As_is for ( initial; condition; increment ) { body } .Close . Example and exercise in class .See ./06ex.cpp .Open Questions . Why are break statements so bad They have been implicated in causing disasters. The `break` has two distinct meanings and some people for get this and produce code that does not work properly. Secondly, `break` skips oover statements to get to the end of the loop and this is easily forgotten. The fact is you can program anything with if and while. So rather than code like this .As_is while(true) .As_is { .As_is A .As_is if( B ) .As_is { .As_is C .As_is } .As_is else .As_is break; .As_is D .As_is } You can, instead use a bool flag. .As_is bool ok = true; .As_is while(ok) .As_is { .As_is A .As_is if( B ) .As_is { .As_is C .As_is } .As_is else .As_is ok=false; .As_is if( ok ) .As_is { .As_is D .As_is } .As_is } I'll be happy if you don't use break in projects. I'll only penalize you if it goes wrong! It won't appear in quizzes, finals, or labs. . Can you explain for loops For loops are compact way of writing counting and scanning loops. . Should you use while or for Use for for counting and scanning. Use while for all other iterations. . How many Layers deep have you gone in nesting while statements I'm not sure.... I've written for-loops at least 3 and possible 4 deep. . How does an if differ from a while An if selects one of two alternatives. A while repeats a sequence until a condition become false. . What does fabs mean It means "Floating-point Absolute Value". It does something like this .As_is if( x < 0 ) .As_is fabs=-x; .As_is else .As_is fabs=x; . WHat is your opinion on using the Microsoft Visual C++ expression editor I've found the "Visual" environments (except the original VB) to need a lot of setting up -- I've usual given up. . Are loops the same as while statements No. There are two kinds of loop (for and do-while) that are not 'while' statements. . Is Linux/Unix an OS just for programmers or can average users use it I think that Linux KDE is better than Vista! But it is striking that Apple Macintoshes come with a version of Unix and the users a smugly happy with it: "It just works". On a lighter note.... here is a fun video on the topic .See http://video.google.com/videoplay?docid=-5847227571896228342&q=linux+windows+macintosh&total=802&start=0&num=10&so=0&type=search&plindex=2 . Is there a limit to repetitions in a for loop No. . Is there a limit to repetitions in a while loop No. . Which is safest for or while They are equally safe to use. And both need care. . Can you just use an if and something instead of a while Yes. You can use the "g*t*" statement -- now considered to be a rude word! Or you use recursion (later in the course). Luckily -- we have a simple while! . Is there a typo at the top of page 58 Yes the condition in the for loop should be "i<=n" not "i<=". . How do logical operators function in C++ See .See ./05.html for my notes on these. By the way -- I don't know precisely what out compilers and computers do with them. I just know that they work they way C++ says they should. No. .Close .Open Next . Quiz 3 -- Loops Logic Ifs . Lab 04 -- Loops Logic Ifs . Project 2 -- Ifs -- Due .See ./projects.html#P2 .Close