Skip to content

Latest commit

 

History

History
89 lines (66 loc) · 1.59 KB

loops.md

File metadata and controls

89 lines (66 loc) · 1.59 KB

Javascript Loops

###While###

var x = 0;
while (x < 5) {
	console.log(x);
	x++;
}

###For###

var myLotteryNumbers = [4, 8, 15, 16, 23, 42];
for (var i = 0; i < myLotteryNumbers.length; i++) {
	console.log("The next winning number is:" + myLotteryNumbers[i]);
}
console.log("I win the lottery!");

###For In###

// This is an object, not an array!
var mysteryActor = {
	"Labyrinth": "Jareth the Goblin King",
	"Basquiat": "Andy Warhol",
	"The Prestige": "Nikola Tesla",
	"Arthur and the Invisibles": "Emperor Maltazard",
	"SpongeBob SquarePants": "L.R.H"
};

for (var movie in mysteryActor) {
	console.log("Our mystery actor appeared in " + movie + " as " + mysteryActor[movie] + ".")
}

console.log("Who was our mystery actor?");

Python Loops

###While###

x = 0
while x < 5: 
	print x
	x += 1

###For###

my_lottery_numbers = [4, 8, 15, 16, 23, 42]
for number in my_lottery_numbers:
	print "The next winning number is:", number

print "I win the lottery!"

###For In###

## Using lists we get from dictionaries:
mystery_actor = {
	"Labyrinth": "Jareth the Goblin King",
	"Basquiat": "Andy Warhol",
	"The Prestige": "Nikola Tesla",
	"Arthur and the Invisibles": "Emperor Maltazard",
	"SpongeBob SquarePants": "L.R.H"
}

for movie in mystery_actor:
	print "Our mystery actor appeared in %s as %s." % (movie, mystery_actor[movie])

print "Who was our mystery actor?"

# (or, alternately)

for movie, role in mystery_actor.items():
    print "Our mystery actor appeared in %s as %s." % (movie, role)