Class 3 - Logical operator, loop
Logical Operators
Logical operators in programming are symbols or keywords that are used to perform logical operations on Boolean values. Boolean values represent true or false, and logical operators allow you to combine or manipulate these values to make decisions in your program.
Logical Operators in JavaScript¶
JavaScript supports three main logical operators: AND (&&
), OR (||
), and NOT (!
).
- AND (
&&
): The AND operator returnstrue
only if both operands aretrue
. Otherwise, it evaluates tofalse
.
- OR (
||
): The OR operator returnstrue
if at least one of the operands istrue
. It evaluates tofalse
only when both operands arefalse
.
- NOT (
!
): The NOT operator negates the boolean value of its operand. If the operand istrue
, it becomesfalse
; if it'sfalse
, it becomestrue
.
Truth Tables¶
A truth table is a tabular representation of all possible outcomes of a logical expression. Let's create truth tables for the AND, OR, and NOT operators.
- AND Truth Table (
&&
):
Operand 1 | Operand 2 | Result |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
- OR Truth Table (
||
):
Operand 1 | Operand 2 | Result |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
- NOT Truth Table (
!
):
Operand | Result |
---|---|
true | false |
false | true |
Practical Use Cases¶
Logical operators are frequently employed in conditional statements and loops, enhancing the decision-making capabilities of programs. Here's a simple example:
var x = 5;
var y = 10;
if (x > 0 && y > 0) {
console.log("Both x and y are positive.");
} else {
console.log("At least one of x or y is not positive.");
}
Loop¶
A loop is a programming construct that allows a set of instructions to be repeated multiple times. JavaScript provides several types of loops, but the most common ones are the "for" loop and the "while" loop.
The For Loop in JavaScript:¶
The for
loop in JavaScript is a powerful tool that allows you to repeat a set of instructions for a specified number of times. It consists of three optional expressions enclosed in parentheses, followed by a code block. Let's break down the structure of the for
loop:
Expression 1:¶
The first expression, expression1
, is executed once before the code block. This part of the loop is typically used to initialize a variable. For example:
In this example, let i = 0
initializes a variable i
to 0 before the loop begins.
Expression 2:¶
The second expression, expression2
, defines the condition for executing the code block. The loop will continue running as long as this condition is true. Using the previous example:
Here, i < 5
is the condition. The loop will keep running as long as i
is less than 5.
Expression 3:¶
The third expression, expression3
, is executed after each execution of the code block. It is commonly used for incrementing or decrementing a variable. Continuing with the example:
In this case, i++
increments the value of i
by 1 after each iteration.
Another example that counts from 1 to 5:
Explanation:
- var i = 1
: This initializes a variable i
to 1.
- i <= 5
: This is the condition that determines whether the loop should continue. It says, "Continue looping as long as i
is less than or equal to 5."
- i++
: This increments the value of i
by 1 after each iteration.
The loop prints the values of i
to the console from 1 to 5.