Skip to content

Instantly share code, notes, and snippets.

@ikemtz
Created October 21, 2019 12:58
Show Gist options
  • Save ikemtz/c11d868560a2b1ace2b9f6f1a12f8ebf to your computer and use it in GitHub Desktop.
Save ikemtz/c11d868560a2b1ace2b9f6f1a12f8ebf to your computer and use it in GitHub Desktop.
Loop Examples for CIS110
// While Loop
var canGraduate = false;
// The while loop will execute the logic within as long as the logical condition is true
// In this example we're evaluating the negated (!) value of canGraduate
// Negating means that your flipping a false to true and vice-versa.
while (!canGraduate) {
passNextCourse();
//In this scenario, when you've met all the requirements for graduation
//This expression will set the value of canGraduate to true
canGraduate = haveIMetTheRequirementsForGraduation();
}
graduate();
// For Loop in it's simplest form
var coursesINeedToGraduate = 15;
// The for loop declaration has three important segments, these are seperated by (;):
// Segment: 1: Initialize your "counter" => mycourseCounter = 0
// Segment: 2: Loop condition, if this expression evaluates to true
// then the logic contained within the loop will be executed
// Segment: 3: Incrementer, This simply adds 1 for every loop
// Everytime you pass a class the myCourseCounter will increase by 1
// The (++) operator works across many languages, but all it does is increment a variable by 1
for (var myCourseCounter = 0; myCourseCounter < coursesINeedToGraduate; myCourseCounter++) {
passNextCourse();
}
graduate();
// ForEach
// I'm initializing an array (collection) here of courses I need to take to graduate
var coursesINeedToPass = ['CIS175', 'CIS110', 'SOC450', 'MAT311'];
// I'm using a forEach loop to iterate over each item in the array
coursesINeedToPass.forEach(course =>{
//This logic will be executed for each course
passNextCourse(course);
console.log(`Yay I passed ${course}!`);
});
// ForIn Loop
// I'm initializing an array (collection) here of courses I need to take to graduate
var coursesINeedToPass = ['CIS175', 'CIS110', 'SOC450', 'MAT311'];
// I'm using a forIn loop to iterate over each item in the array
for (course in coursesINeedToPass){
//This logic will be executed for each course
passNextCourse(course);
console.log(`Yay I passed ${course}!`);
};
// Recursive loop
var coursesINeedToGraduate = 15;
function passCoursesUntilICanGradate(){
// The decrement operator (--), the exact opposite of the increment operator
// Subtracts 1 from the variable every single time
coursesINeedToGraduate--;
// Here I'm checking if I have to take any additional courses
if (coursesINeedToGraduate > 0){
//Here my function is calling itself (it's recursive)
passCoursesUntilICanGradate();
}
//If I have no more courses to take
else {
// I'm essentially exiting the loop here and GRADUATING!!!
graduate();
}
}
// Bad ForEach
var coursesINeedToPass = ['CIS175', 'CIS110', 'SOC450', 'MAT311'];
coursesINeedToPass.forEach(course => {
// I hate Sociology so I want to switch it with a Math course
if (course.startsWith('SOC')){
// Here I'm adding an item to the coursesINeedToPass array (collection)
// This is not allowed in many languages
coursesINeedToPass.push('MAT600');
continue;
}
passNextCourse(course);
console.log(`Yay I passed ${course}!`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment