My precious, I want to Control the Flow of my precious
Tags: html, css, javascript, webdev
Published: 2020-11-23 14:35:40

####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:__

```js
//Loops include: 

for(something){

}
while(something){

}
 
//and 
 
do(somehting){

}while(something)


```
```js
//statements include

if(something){

}

if(something){

} else if(something) {

} else(something) {

}

//and 

switch(something){

}
```

##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')__*
```js
for(i = 0; i < 8; i++){
console.log('golfers');
}
```

###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.
```js
const golfers = {"marcelo", "andres", "lance", "hudson", "hans", "sebastion"}
let i = 0;
while(i <golfers.length;){
  console.log(names[i]);
  i++;
}
```

###The last loop function is the __`do...while loop`__
```js
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)
```

##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. 
```js
const breadColor = 'golden brown';

if(breadColor = 'golden brown'){
  console.log('then the bread is fresh')
}
```

### Next we are serving __`else if statements`__

```js
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")
```


- 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.

```js
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');
```

##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. 

```js
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')
}
```

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

```js
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')

}
```


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