cop 3275 – iteration and loops instructor: diego rivera-gutierrez
TRANSCRIPT
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.
Administrative stuff
• Just as a reminder: You do get to drop lowest Quiz grade and lowest Homework grade.
For statements
• General structure: for(<init statement> ; <loop condition> ; <loop expression>) {
<program statement>
}
optional
• 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> }
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.
Let’s do a couple examples!
• Addition of the first n natural numbers
• 1+2+3+…+n
• Let’s code it together.
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/
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
While loops
• General structure
while(<loop condition>) {
<program statement>
}
while(<loop condition>) {<program statement>
}
• Remember! Order of execution of a for:
<init statement>
if(<loop condition>){
<program statement>
<loop expression>
if(<loop condition>){
…
}
}
while(<loop condition>) {<program statement>
}
• Order of execution of a while
if(<loop condition>){
<program statement>
if(<loop condition>){
…
}
}
Do-while loops
• General structure
do {
<program statement>
} while(<loop condition>) ; In my opinion the
MOST forgotten semicolon in all of C
do {<program statement>
} while(<loop condition>) ;
• Remember! Order of execution of a while
if(<loop condition>){
<program statement>
if(<loop condition>){
…
}
}
do {<program statement>
} while(<loop condition>) ;
• Order of execution of a do-while
if(<loop condition>){
<program statement>
if(<loop condition>){
…
}
}
Let’s do some catching of user errors using while loops!
• For the rest of the class we will go through some examples!