Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3275 – Iteration and loops Instructor: Diego Rivera-Gutierrez.

Similar presentations


Presentation on theme: "COP 3275 – Iteration and loops Instructor: Diego Rivera-Gutierrez."— Presentation transcript:

1 COP 3275 – Iteration and loops Instructor: Diego Rivera-Gutierrez

2 Administrative stuff Third Quiz on Friday (6/5) Homework #2 due tonight! Questions? Remember to be careful about input and following instructions! Homework #3-4-5 will be cumulative We will definitely be building pieces of a game. You have until tonight around 7pm to send me suggestions! Extra points if one of your suggestions get picked! (Unless it is Minesweeper which was my original suggestion) Homework will be uploaded by 9pm tonight. I’ll be providing solutions for every homework, in case you can’t finish your own version.

3 Administrative stuff Just as a reminder: You do get to drop lowest Quiz grade and lowest Homework grade.

4 For statements General structure: for( ; ; ) { } optional

5 Order of execution of a for: if( ){ if( ){ … } } for( ; ; ) { }

6 Why don’t we write it out? First: it would be a lot of repetitive work Second: for loops allow us to make it potentially infinite.

7 Let’s do a couple examples! Addition of the first n natural numbers 1+2+3+…+n Let’s code it together.

8 Random numbers Let’s say we want to print n random numbers… How would we do something random? It is actually somewhat challenging… Why? Computers follow instructions blindly Predictable… Predictable things aren’t statistically random C provides us a Pseudo-Random Number generator Further reading: https://www.random.org/randomness/

9 Generating random numbers We need two new libraries: #include We need three functions: rand() : the pseudo-random number generator. srand(unsinged int seed): sets the seed for the generator time(): gets the current time from the CPU

10 While loops General structure while( ) { }

11 while( ) { } Remember! Order of execution of a for : if( ){ if( ){ … }

12 while( ) { } Order of execution of a while if( ){ if( ){ … }

13 Do-while loops General structure do { } while( ) ; In my opinion the MOST forgotten semicolon in all of C

14 do { } while( ) ; Remember! Order of execution of a while if( ){ if( ){ … }

15 do { } while( ) ; Order of execution of a do-while if( ){ if( ){ … }

16 Let’s do some catching of user errors using while loops! For the rest of the class we will go through some examples!


Download ppt "COP 3275 – Iteration and loops Instructor: Diego Rivera-Gutierrez."

Similar presentations


Ads by Google