Control Flow
Tags: javascript
Published: 2021-01-05 17:01:30

## Conditionals and Loops </a>
### When we use JavaScript we use programs as a sequence of statements </a>
### In this section of the blog we will go over "if..else, switch, do..while, for, forEach, break and while" statements </a>
#### if..else- the if statement executes if the specified condition is true, when the condition is false it will use another statement: </a>
``` Java
if (condition1) {
  // code to execute if condition1 is true
} else if (condition2) {
  // code to execute if condition2 is true
} else {
  // code to execute if condition1 and condition2 are false
}
```
#### switch- this statement checks the condition and executes the statements that follow: </a>
``` Java
const name  = prompt ("Favorite Modern Warfare main character:");
let character = "";
switch (name) {
  case "Sargent Woods":
    house = "Captain Price";
}
console.log(`Your favorite character is ${character}`);
```
#### Break- this statement ends the current switch statement: </a>
``` Java
const name  = prompt ("Favorite Modern Warfare main character:");
let character = "";

switch (name) {
  case "Sargent Woods":
    house = "Captain Price";
    break;
}
```
### Loops and iterations </a>
#### Loops and iterations are used to execute repetitive tasks </a>
#### while- this statement creates a loop that executes as specified code as long as the condition is true: </a>
``` Java
let age = 0;

while (age <= 18) {
console.log (i);
i++
}
```
#### do..while- this creates a loop that executes a block of code once, before checking if the condition is true, then repeat if true: </a>
``` Java
let time = 8
do {
console.log('Hello')
} while (time == 8
time++;
```
#### for- this statement creates a loop with an initialization, condition and final expression </a>
#### The initialization is the variable declaration. The condition is what has to happen to finish the iteration. The final expression is the increment of the variable used to iterate our code. </a>
``` Java
for (let time = 20; time <=400; i++) {
console.log(time);
}
```
### Continue and break </a>
#### break- this statement exists a statement or a loop: </a>
``` Java
let money = 500;
while(money < 2000) {
i++;
console.log('You have ${money} amount of money');
if(money = 40) {
break;
  }
}
```
#### Continue- this breaks one iteration in a loop when a special condition occurs, and continues with the next iteration in the loop: </a>
``` Java
let gameCost = 60;
while (gameCost >= 600) {
i++;
if (gameCost = 65) {
  continue;
  }
console.log('The Game costs ${gameCost} dollars!');
}
```
## Udemy Course Notes </a>
### Logical Opertors </a>
#### The logical operators are '||', and '&&' </a>
#### '||' represents or, and '&&' is and: </a>
``` Java
if(password.length >= 12 && password.includes('@')){
    console.log('that password is mighty strong!');
} else if(password.length >= 8 || password.includes('@') && password.length >= 5);
){
    console.log('that password is long enough!');
} 
// IF the "if" is false then the "else" code is used
else {
    console.log('password is not long enough!');
}
```
#### Logical Not (!)
#### the ! reverses a true statement to false and visa versa: </a>
``` Java
let user = false;

if(!user){
console.log('you must be logged in to continue');
}

console.log(!true);
console.log(!false);
```
### Conclusion </a>
#### Conditional statements allow for a large expansion for the user. Although i am not very familiar with Java my i have learned a lot about conditionals. They have a plethora of applications both for practice and for world situations.
### Sources
The Net Ninja's udemy JavaScript Course
https://www.w3schools.com/