While Loops & Lists
CSCI 185: Repeating Code
1
Announcements
2
Announcements: Your Grade
The final exam is optional:
3
Outline
4
Outline
5
Repeating Tasks
Once you teach your computer to do something once, it is easy to get it to do the same thing many times. Repeating tasks is one of the things a computer does really well.
6
While Loops Metaphors
if (there is a pringle in the can) {
// eat a pringle
}
7
While Loops Metaphors
if (there is a pringle in the can) {
// eat a pringle
}
8
While Loops Metaphors
But do you really want to eat just one?
if (there is a pringle in the can) {
// eat a pringle
}
9
While Loops Metaphors
if (there is a pringle in the can) {
// eat a pringle
}
if (there is a pringle in the can) {
// eat a pringle
}
if (there is a pringle in the can) {
// eat a pringle
}
if (there is a pringle in the can) {
// eat a pringle
}
But what if you don’t know how many pringles are in the can?
10
While Loops Metaphors
if (there is a pringle in the can) {
// eat a pringle
}
// because no one can eat just one
while (there is a pringle in the can) {
// eat a pringle
}
11
While Loops Metaphors
if (there is a pringle in the can) {
// eat a pringle
}
// because no one can eat just one
while (there is a pringle in the can) {
// eat a pringle
}
12
While Loops Metaphors
if (there is a pringle in the can) {
// eat a pringle
}
// because no one can eat just one
while (there is a pringle in the can) {
// eat a pringle
}
13
While Loops Metaphors
if (there is a pringle in the can) {
// eat a pringle
}
// because no one can eat just one
while (there is a pringle in the can) {
// eat a pringle
}
14
While Loops Metaphors
if (there is a pringle in the can) {
// eat a pringle
}
// because no one can eat just one
while (there is a pringle in the can) {
// eat a pringle
}
15
While Loops Metaphors
if (there is a pringle in the can) {
// eat a pringle
}
// because no one can eat just one
while (there is a pringle in the can) {
// eat a pringle
}
16
While Loops Metaphors
if (there is a pringle in the can) {
// eat a pringle
}
// because no one can eat just one
while (there is a pringle in the can) {
// eat a pringle
}
17
...until they’re gone :(
18
19
20
21
4
6
3
6
Repeating Tasks
We are going to examine several ways of repeating code:
22
Some common scenarios
How do I...
23
While Loops
24
while (some condition is true) {
// execute code block
}
Pseudocode
Print the Statement
How do print this sentence over and over again?
console.log('Hello world!!');
25
00-while-loop-intro
Print the Statement
Solution: How do print this sentence over and over again?
// using the node.js interpreter:
while (true) { // the condition is always true
console.log('Hello world!')
}
26
00-while-loop-intro
How do I make it print 10 times and then stop?
To do this, you have to answer two questions:
27
How would you do it?
I’m going to run the program again, and you’re going to figure out when it’s printed the sentence 10 times...
...how did you do it?
28
Using variables to track state
29
Using variables to track state
What needs to change to get it to only print 10 times?
while (true) { // the condition is always true
console.log('Hey there! Hope you\'re doing OK!')
}
30
00-while-loop-intro
Using variables to track state
What needs to change to get it to stop after 10 beats?
let counter = 0;
while (counter < 10) {
console.log('Hey there! Hope you\'re doing OK!');� counter += 1;
}
31
Initialize iteration variable
Specify halt condition
00-while-loop-intro
Recall: If Statement Syntax
32
if (condition) {
statement 1
statement 2
…
}
CONDITION�Boolean expression that evaluates to True or False.
BLOCK�If the condition evaluates to True, the block executes. Otherwise, the block is skipped.
While Loop Syntax
33
while (condition) {
statement 1
statement 2
…
}
CONDITION�Boolean expression that evaluates to True or False.
BLOCK�While the condition evaluates to True, the block will continue to execute over and over. Otherwise, the block is skipped.
Recall: Comparison Operators Evaluate to Either true or false
Comparison operators compare two operands according to a comparison rule and evaluate to either True or False (boolean)�
34
Operator | Description |
== === | If the values of two operands are equal, then the condition becomes true. |
!= | If values of two operands are not equal, then condition becomes true. |
> | If the value of left operand is greater than the value of right operand, then condition becomes true. |
< | If the value of left operand is less than the value of right operand, then condition becomes true. |
>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. |
<= | If the value of left operand is less than or equal to the value of right operand, then condition becomes true. |
Recall: Logical Operators Also Evaluate to Either True or False
Logical operators provide additional ways to determine whether something is true or false:
35
Operator | Description |
&& | If both operands are true then the expression evaluates to true. Otherwise, the expression evaluates to false |
|| | If either or both operands are true then the expression evaluates to true. If both operands are false, the expression evaluates to false |
! | If the operand is false than the expression evaluates to true (and vice versa) |
Outline
36
While Loop Exercise: Circles
01-circles
37
Animations are also loops!
draw();
sleep(20);
}��03-animation-while-loop
38
Outline
39
Lists (AKA “Arrays”)
const myList = ["charlie", "freddie", "lucy"]
//access individual items: �console.log(myList[0]);�console.log(myList[1]);�console.log(myList[2]);
console.log(myList.length); //get length of list
myList.push("jimena"); // append item to bottom
myList.pop(); // remove item from bottom
40
"charlie" |
"freddie" |
"lucy" |
0
1
2
Examples of Lists
41
Objects: Syntax
What is an object?
Objects are a way to group variables and functions into a convenient package.
Example:
const ball1 = {
x: 300,
y: 150,
size: 130
}
42
key
value
Objects: Example
const ball1 = {
x: 300,
y: 150,
size: 130
}
const ball2 = {
x: 40,
y: 3450,
size: 50
}
console.log(ball1.x);
console.log(ball2.x);
console.log(ball2.size);
ball2.size += 0.5;
console.log(ball2.size);
43
Lists of Objects
It is also common to store data in lists of objects. In the following example, what do you think will be printed to the screen?:
const circleData = [
{x: 500, y: 50, d: 100, speedX: 3, speedY: 3},
{x: 300, y: 150, d: 50, speedX: 2, speedY: 0},
{x: 400, y: 400, d: 25, speedX: 0, speedY: 1},
{x: 40, y: 520, d: 80, speedX: 2, speedY: 0},
{x: 140, y: 120, d: 150, speedX: 0, speedY: -0.5},
{x: 350, y: 350, d: 70, speedX: 1, speedY: 1}
];
console.log(circleData[0]);
console.log(circleData[0].speedX);
console.log(circleData[2].d);
console.log(circleData[4].x);
44
Exercises
List of Strings
List of Images
List of Cards
45
Introduction to Homework 6
Creating a Photo Carousel
46
Outline
47
The Built-In Random Function
One important concept in computer science is the notion of randomness, which allows a programmer to simulate a number of different possible inputs quickly. Some applications:
48
Random Function Examples
Q: How do I generate a random number between 0 and 10?
const myNum = Math.floor(Math.random() * 11);
Q: How do I generate a random number between 5 and 15?
const myNum = Math.floor(Math.random() * 10) + 6;
Q: Can’t I just make a function to do this?
function randomInt(min, max) { � // min and max included � return Math.floor(Math.random() * (max - min + 1) + min);�}
49
Exercise 08-randomness
Open 08-randomness and take a look at sketch.js (ignore p5.js and utilities.js). Then, see if you can modify the code as follows:
50
Exercise 09-circle-animation: Working with data
51
09-circle-animation