cop 3275 – iteration and loops instructor: diego rivera-gutierrez

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

Upload: caitlin-cook

Post on 18-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

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

COP 3275 – Iteration and loops

Instructor: Diego Rivera-Gutierrez

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

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.

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

Administrative stuff

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

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

For statements

• General structure: for(<init statement> ; <loop condition> ; <loop expression>) {

<program statement>

}

optional

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

• Order of execution of a for:

<init statement>

if(<loop condition>){

<program statement>

<loop expression>

if(<loop condition>){

}

}

for(<init statement> ; <loop condition> ; <loop expression>) {

<program statement> }

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

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.

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

Let’s do a couple examples!

• Addition of the first n natural numbers

• 1+2+3+…+n

• Let’s code it together.

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

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/

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

Generating random numbers

• We need two new libraries:• #include <stdlib.h>• #include <time.h>

• 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

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

While loops

• General structure

while(<loop condition>) {

<program statement>

}

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

while(<loop condition>) {<program statement>

}

• Remember! Order of execution of a for:

<init statement>

if(<loop condition>){

<program statement>

<loop expression>

if(<loop condition>){

}

}

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

while(<loop condition>) {<program statement>

}

• Order of execution of a while

if(<loop condition>){

<program statement>

if(<loop condition>){

}

}

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

Do-while loops

• General structure

do {

<program statement>

} while(<loop condition>) ; In my opinion the

MOST forgotten semicolon in all of C

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

do {<program statement>

} while(<loop condition>) ;

• Remember! Order of execution of a while

if(<loop condition>){

<program statement>

if(<loop condition>){

}

}

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

do {<program statement>

} while(<loop condition>) ;

• Order of execution of a do-while

if(<loop condition>){

<program statement>

if(<loop condition>){

}

}

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

Let’s do some catching of user errors using while loops!

• For the rest of the class we will go through some examples!