6.2. Basics of JavaScript: Conditions and Loops

 

Conditional Statements

Conditional statements are like instructions for the computer: "If something is true, do this; otherwise, do that." They allow your program to make decisions based on specific conditions.

Types of conditional statements:

if: This statement tells the program what to do if a certain condition is true.

else if: If the first condition is not true, you can add another condition to be checked.

else: This block will execute if none of the previous conditions are true.

Conditional statements enable your program to make different choices depending on what is happening. They automate routine tasks and make your program smarter and more adaptable.

Let's look at a couple of examples.

Example 1: Student grading:

let score = 85;if (score >= 90) {  console.log("Excellent!");} else if (score >= 70) {  console.log("Good");} else if (score >= 50) {  console.log("Satisfactory");} else {  console.log("Unsatisfactory");}

 

Example 2: Checking login and password:

let username = "user123";let password = "securepass";if (username == "user123" && password == "securepass") {  console.log("You have successfully logged in");} else {  console.log("Incorrect username or password");}

The && symbol means that both conditions must be true. If you want either condition to be true, you can use the || symbol. Also, note that in the if...else constructs, the double equal sign (==) is used for comparison. If you use a single equal sign (=), it is interpreted as an assignment operation.

Exercise 1: Write a function called checkNumber that takes a number as an argument and returns a message "The number is positive," "The number is negative," or "The number is zero" depending on the given number. Also, print the result to the console.

 

Loops

Imagine you have a task that needs to be repeated multiple times, such as printing "Hello" ten times. Loops help you do this without copying and pasting the same thing over and over again. Loops are like repetitive steps, a way to tell the program, "Repeat this action many times until a condition is met."

Types of loops:

for: This loop allows you to perform actions a specific number of times when you already know the number of repetitions in advance.

while: If you don't know in advance how many times an action needs to be repeated, you can use this type of loop.

Loops are very convenient when you need to process a lot of data or repeat the same action. They reduce the need for duplicating code and make the program more efficient.

Example 1: Output numbers from 1 to 5:

for (let i = 1; i <= 5; i++) {  console.log(i);}

In the parentheses after for, we have three expressions. First, we specify the start of the loop, which is 1. Then comes the end of the loop, which is 5. Finally, the increment step, indicating by how much the initial variable should increase with each iteration, which is 1 in our case. The ++ sign after the variable means to add one (increment). The -- sign would mean subtracting one (decrement).

 

Example 2: Output numbers from 0 to 10 with a step of 2, i.e., print 0, 2, 4, 6, 8, and 10:

for (let i = 0; i <= 10; i+=2) {  console.log(i);}

 

Example 3: Finding the sum of numbers from 1 to 10:

let sum = 0;let number = 1;while (number <= 10) {  sum += number;  number++;}console.log("Sum: " + sum);

In the parentheses after the while operator, there is always a comparison expression. If the expression is true, the code block inside the curly braces {…} will be executed.

 

Exercise 2: Write a function called calculateFactorial that takes ONLY a positive integer number n as an argument and returns the factorial of that number. The factorial of a number is the product of all positive integers from 1 to n.