DEV Community

Cover image for My precious, I want to Control the Flow of my precious
MarceloVarela22377
MarceloVarela22377

Posted on

My precious, I want to Control the Flow of my precious

Do you want to Control the Flow of your precious code?

Hi, my name is Marcelo Varela and I'm going to lead you through a short guide on how to do just that in your journey of javascript. So if you're ever in a bunch and forgot how to write something that has to do with Control Flow, never fear because this blog is here!!

Ok now let the fun begin as we dive on in!!

Control Flow

I have said this term once already and hinted at it all over the intro, but what is it? Control Flow is a term used to describe a group of items that effect variables in javascript. The group includes:

//Loops include: 

for(something){

}
while(something){

}

//and 

do(somehting){

}while(something)


Enter fullscreen mode Exit fullscreen mode
//statements include

if(something){

}

if(something){

} else if(something) {

} else(something) {

}

//and 

switch(something){

}
Enter fullscreen mode Exit fullscreen mode

Loops

loops can be useful for the lazy person who doesn't want to rewrite their code multiple times. Just Kidding, well not really, but you get the point, I hope... Looping is actually a very cool and time saving attribute javascript has. Being able to repeat multiple lines of code using only a couple lines of code is very useful.

So like what are the types of Loops?

At the start we saw 3 types of loops, which were: for loops, while loops and do...while loops

The For Loops repeats a function however many times the condition sees until it is false. In a for loop there are 4 parts:

  1. the variable is: (i = 0;)

  2. the condition is: (i < 8;)

  3. the final expression is: i++

  4. and your output is: console.log('golfers')

for(i = 0; i < 8; i++){
console.log('golfers');
}
Enter fullscreen mode Exit fullscreen mode

The While Loop is quite similar to the regular for loop__, in fact it has all the same attributes or pieces. But it is formed differently.

const golfers = {"marcelo", "andres", "lance", "hudson", "hans", "sebastion"}
let i = 0;
while(i <golfers.length;){
  console.log(names[i]);
  i++;
}
Enter fullscreen mode Exit fullscreen mode

The last loop function is the do...while loop

const workout ={'20 pushups', '1 minute plank', ' 3 dumbell sets', '12 mile Bike Ride'}
do{
  console.log('you still need to do ', workout);
  i++;
} while (i < workout.length)
Enter fullscreen mode Exit fullscreen mode

Statements

All right we got 3 types of these things we all say: Statements. Statements are used to act on true and false situations. Confusing right? Just think of it like saying if I grab the bag then the bag will be lifted. That's the concept. Yea weird how an awkward explanation could mean something so trivial and obvious. So let's get into the names of our items inside the grocery bag so we can start cooking the code.

For today we will be cooking:

if statements which tell us what to do if a certain condition is true or false.

const breadColor = 'golden brown';

if(breadColor = 'golden brown'){
  console.log('then the bread is fresh')
}
Enter fullscreen mode Exit fullscreen mode

Next we are serving else if statements

const breadColor = 'dark green';

if(breadColor = 'golden brown' || 'brown'){
  console.log('then the bread is fresh')
} else if (breadColor = 'dark green'){
  console.log('bread is molded')
} else {
console.log("that's some weird bread")
Enter fullscreen mode Exit fullscreen mode
  • The || means we OR, and if you see && it means AND

The final dish for today's main course is switch statements which is another way to say long lists of else if statements in a cleaner way.

const taste = 'sweet';

switch(taste){
  case 'sweet':
    console.log('your food is sweet');
    break;
  case 'sour':
    console.log('your food is sour');
    break;
  case 'bitter':
    console.log('your food is bitter');
    break;
  case 'spicy':
    console.log('your food is spicy');
    break;
  case 'salty':
    console.log('your food is salty');
    break;
  default:
    console.log('your food is bland');
Enter fullscreen mode Exit fullscreen mode

Notable Mentions

Two other things that are important are continue and break

Continue is that stain on your shirt you don't want to be seen, or a skip button.

const ballmph = {99, 102, 87, 67, 89, 92, 96, 101}

for(let i = 0; i < ballmph.length; i++){

  if(ballmph[i] === 67){
    continue;
  }
  console.log('you threw ', ballmph[I], 'mph')
}
Enter fullscreen mode Exit fullscreen mode

Break is your hold up wait a minute your the one I want or the only important one and disregard the others.

const ballmph = {99, 102, 87, 67, 89, 92, 96, 101}

for(let i = 0; i < ballmph.length; i++){

  if(ballmph[i] === 102){
    break;
  }
    console.log('you threw the fastest pitch of' , ballmph[i], 'mph')

}
Enter fullscreen mode Exit fullscreen mode

Well we learned a lot about loops and statements along with their uses. Along with that we learned about the uses of && in statements, || in statements, about break's uses and continue's uses.

Thanks for taking the time to read this blog. I hope you have a great day, afternoon, evening or night and I hope that this helps you along in your journey to be a coding master!!

Resources:

https://code.visualstudio.com

https://www.w3schools.com

https://www.udemy.com

https://docs.google.com/document/d/1djb1VATZFbmPwWoLPjwB1MrzYmyxqg3RMUCHKy1f5CI/edit?usp=sharing

Top comments (0)