Class Note for EECS 168 with Professor Haverkamp at KU
Class Note for EECS 168 with Professor Haverkamp at KU
Popular in Course
Popular in Department
This 16 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at Kansas taught by a professor in Fall. Since its upload, it has received 32 views.
Reviews for Class Note for EECS 168 with Professor Haverkamp at KU
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
Iterative Statements 0 The while Statement Counting Loops Conditional Loops In nite Loop Errors The for Statement Autoincrement and Autodecrement Operators The dowhile Statement Nested Loops Common Errors Software Engineering Tips The while Statement 0 Often need to do repetitive calculations to solve problems 0 Computers are good at this humans are typically bad at this 0 A while loop can be used to iteratively execute a block of code 0 Need to specify the following 1 the code to execute before loop 2 the Boolean condition for continuing iteration 3 the block of code to be repeated 0 The program will execute block of code over and over as long as the Boolean condition remains TRUE 0 Code after the loop is executed when condition becomes FALSE While loop syntax initialization code while Boolean expression block of statements to be repeated update variables in Boolean expression Counting Loops o Often need to perform an operation xed number of times 0 Counting loops allow us to do this 0 Need to do the following 1 initialize the loop counter 2 while counter has NOT reached desired number 0 perform desired operations 0 increment loop counter 0 check loop counter again Counting loop example int count 0 while count lt 10 cout ltlt count ltlt quotsquaredquot ltlt countcount ltlt endl count count l Zero iterations loop int value 42 while value lt 42 cout ltlt value ltlt quotdoubledquot ltlt value2 ltlt endl value value l Conditional Loops 0 We can vary the number of iterations based on the data values 0 Conditional loops process data until given situation arises 0 Can also be used to check for end of input or invalid data values 0 Need to do the following 1 perform initialization code 2 while condition remains TRUE the program 0 perform desired operations 0 check terminating condition again Conditional loop example int amt 99 int pow 0 while amt gt O cout ltlt quotamtquot ltlt amt ltlt quot powquot ltlt pow ltlt endl amt amt2 pow pow 1 cout ltlt quotamtquot ltlt amt ltlt quot powquot ltlt pow ltlt endl Input varying loop int num cout ltlt quotEnter a number use 1 to endnquot cin gtgt num while num gt 0 val sqrtnum cout ltlt val ltlt quotsquaredquot ltlt num ltlt endl cout ltlt quotEnter a number use 1 to endnquot cin gtgt num Calculate sum of variable number of values int count 0 int sum 0 int num cout ltlt quotEnter sequence of values use 1 to endnquot cin gtgt num while num gt 0 count count 1 sum sum num cin gtgt num Calculate and print average if count gt O cout ltlt quotAverage quot ltlt sum count ltlt endl else cout ltlt quotNo data enterednquot Infinite Loop Errors 0 It is possible to create while loops that execute forever 0 Often these in nite loops are often unplanned and unwanted 0 To get out of in nite loop you need to kill your program using quotC o Occasionally in nite loops are used on purpose this is not recommended but you may see it in other programmer39s code Trivial in nite loop example while true cout ltlt quotHello Momnquot Accidental in nite loop example int total 0 int count 0 while count lt 10 total total count missing count increment In nite loop caused by an IO error code will execute forever if a character is typed as input instead of a number height 0 while height lt 42 cout quotEnter height quot cin gtgt height The for Statement 0 The for loop provides a compact syntax for iteration 0 Typically used for counting loops but can be used for any loop 0 Allows you to specify the following all on one line 1 initialization statements 2 Boolean expression for continuing loop 3 statements to be executed after loop For loop syntax for initialization Boolean expression increment statements to be repeated For loop example for int num O num lt 10 num numl cout ltlt num ltlt quotcubedquot ltlt numnumnum ltlt endl 7 Autoincrement and Autodecrement Operators o The and operators add or subtract one to a variable 0 O rewrite i il as i rewritej jl asj 0 Similar short form combines assignment and operators 0 O O O O rewriteaabasab rewriteccdascd rewriteaabasab rewriteccdascd rewriteaabasab o Produces shorter and often faster code Example using compact operators int sum 0 int product l for int count 0 count lt 13 count sum count product count Assign rst then increment int num 5 int val num now val is 5 num is 6 Increment rst then assign int num 5 int val num now nal is 6 num is 6 The dowhile Statement The dowhile loop puts Boolean expression after the body of loop Body will be always executed at least once If Boolean expression is TRUE the loop will continue Useful for selected applications Example read input and check if valid Do while example int value do cout ltlt quotEnter a number between 09 quot cin gtgt value while value lt 0 value gt 9 10 Nested Loops o Often necessary for one loop to include another loop 0 Need separate initializations Boolean expressions and increments o If the outer loop executes N times and the inner loop executes M times then the inner loop body is executed N x M times Square printing example for int height0 height lt 14 height Outer loop code for int width0 width lt 17 width Inner loop code cout ltlt quotquot cout ltlt endl Factorial example for int number 1 number lt 42 number Outer loop code int factorial l for int count 1 count lt number count Inner loop code factorial count cout ltlt quotNumber quot ltlt number ltlt quotFactorial quot ltlt factorial ltlt endl 11 Common Errors 0 Extra semicolon after for or while causes logic error 0 Counters and other variables not properly initialized o Boolean expression will never become FALSE in nite loop 0 Loop executes incorrect number of times off by one common 0 Never update for loop counter variable inside for loop 0 Never use same counter variable for nested loops Incorrect nested loop example int index for index 0 index lt 10 index for index 0 index lt 10 index cout ltlt quotResult quot ltlt 3 index 5 ltlt endl cout ltlt quotIndex quot ltlt index ltlt endl 12 Software Engineering Tips 0 Print debugging messages inside each loop to count loop iterations and show values of important variables 0 Anticipate that loops may execute zero times and make sure that all variables are initialized and code runs properly 0 Anticipate and avoid in nite loops by making sure you get quotcloserquot to the loop terminating condition after each loop iteration Fibonacci Example Output the Fibonacci sequence 1 1 2 3 5 8 13 21 Invented 100s of years ago to model population growth Do you notice the pattern First two values are equal to one Following values are the sum of the previous two values 13 Initial Fibonacci Program Purpose Program to print 9 Fibonacci numbers Solution without iteration using 9 variables Author John Gauch include ltiostreamgt using namespace std int main Output rst two values int num1 1 cout ltlt num1 ltlt endl int num2 1 cout ltlt num2 ltlt endl Calculate and output remaining values int num3 num1 num2 cout ltlt num3 ltlt endl int num4 num2 num3 cout ltlt num4 ltlt endl int num5 num3 num4 cout ltlt num5 ltlt endl int num6 num4 num5 cout ltlt num6 ltlt endl int num7 num5 num6 cout ltlt num7 ltlt endl int num8 num6 num7 cout ltlt num8 ltlt endl int num9 num7 num8 cout ltlt num9 ltlt endl return 0 14 Intermediate Fibonacci Program Purpose Program to print 9 Fibonacci numbers Solution without iteration using 3 variables Author John Gauch include ltiostreamgt using namespace std int main Output rst two values int numl l cout ltlt numl ltlt endl int num2 l cout ltlt num2 ltlt endl Calculate and output remaining values using repeated code int num3 numl num2 cout ltlt num3 ltlt endl numl num2 num2 num3 num3 numl num2 cout ltlt num3 ltlt endl numl num2 num2 num3 num3 numl num2 cout ltlt num3 ltlt endl numl num2 num2 num3 num3 numl num2 cout ltlt num3 ltlt endl numl num2 num2 num3 num3 numl num2 cout ltlt num3 ltlt endl numl num2 num2 num3 num3 numl num2 cout ltlt num3 ltlt endl numl num2 num2 num3 num3 numl num2 cout ltlt num3 ltlt endl return 0 15 Final Fibonacci Program Purpose Program to print 9 Fibonacci numbers Solution with iteration Author John Gauch include ltiostreamgt using namespace std int main Output rst two values int num1 1 cout ltlt num1 ltlt endl int num2 1 cout ltlt num2 ltlt endl Calculate and output remaining values using iteration int count 3 while count lt 9 int num3 num1 num2 cout ltlt num3 ltlt endl num1 num2 num2 num3 count count 1 16
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'