Conditionals and Loops
When we use JavaScript we use programs as a sequence of statements
In this section of the blog we will go over "if..else, switch, do..while, for, forEach, break and while" statements
if..else- the if statement executes if the specified condition is true, when the condition is false it will use another statement:
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:
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:
const name = prompt ("Favorite Modern Warfare main character:");
let character = "";
switch (name) {
case "Sargent Woods":
house = "Captain Price";
break;
}
Loops and iterations
Loops and iterations are used to execute repetitive tasks
while- this statement creates a loop that executes as specified code as long as the condition is true:
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:
let time = 8
do {
console.log('Hello')
} while (time == 8
time++;
for- this statement creates a loop with an initialization, condition and final expression
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.
for (let time = 20; time <=400; i++) {
console.log(time);
}
Continue and break
break- this statement exists a statement or a loop:
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:
let gameCost = 60;
while (gameCost >= 600) {
i++;
if (gameCost = 65) {
continue;
}
console.log('The Game costs ${gameCost} dollars!');
}
Udemy Course Notes
Logical Opertors
The logical operators are '||', and '&&'
'||' represents or, and '&&' is and:
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:
let user = false;
if(!user){
console.log('you must be logged in to continue');
}
console.log(!true);
console.log(!false);
Conclusion
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/
Top comments (0)