The document explains various conditional statements in programming, including 'if', 'if else', 'if else if', and 'nested if' statements, detailing their use cases and providing examples. It also covers different types of loops such as 'while', 'do while', 'for', and 'forEach', along with their applications and examples. Additionally, it discusses functions as reusable code blocks that can accept parameters and return values.
The document explains various conditional statements in programming, including 'if', 'if else', 'if else if', and 'nested if' statements, detailing their use cases and providing examples. It also covers different types of loops such as 'while', 'do while', 'for', and 'forEach', along with their applications and examples. Additionally, it discusses functions as reusable code blocks that can accept parameters and return values.
Conditional Statements: If
Statement
An ‘if statement executes a block of
code if a specified condition is true.
Use Case: It is used to make decisions
in code, executing code only when
certain conditions are met.
Example:
if (x > 10) {
console.log('x is greater than
10);
}Conditional Statements: If Else
Statement
The ‘if else’ statement executes one
block of code if the condition is true,
and another block of code if it is false.
Use Case: It is used when we need to
execute alternative code when the
condition is false.
Example:
if (x > 10){
console.log(x is greater than 101;
jelse{
console.log(x is less than or equal to 10);
}Conditional Statements: If Else
If Statement
The ‘if else if’ statement is used when
we have multiple conditions to test. It
allows us to specify multiple conditions
to be checked sequentially.
Use Case: It is used when you need to
check several conditions and execute
the corresponding code for the first
conditionstras is true.
console.log(x is greater than 10');
}else if (x == 10) {
console.log(x is exactly 10’);
Jelse{
console.log(x is less than 10’);
}Conditional Statements: Nested
If Statement
A 'nested if' is an if statement inside
another if statement. It is used when we
need to make further decisions within
an if block.
Use Case: It is used to check for
multipfereneiditions inside another
condfifar.1)
3}
console.log('x is greater than
10 and y is greater than 5’);
}
}Switch Statement
The 'switch' statement executes one
block of code depending on the value of
an expression. Each case in a switch is
compared with the expression.
Use Case: It is used when you want to
compare one variable to multiple values.
Example:
switch(day) {
case 1:
console.log(Monday’);
break;
case 2:
console.log(Tuesday’);
break;
default:
console.log(Another day’);Loops: While Loop
A 'while' loop repeats a block of code as
long as a specified condition is true.
Use Case: It is used when you don't
know how many times you need to loop
ahead of time, but you know the
condition.under which to stop.
let i = 0;
while (i < 5) {
console.log(i);
i++;
}Loops: Do While Loop
A ‘do while’ loop is similar to the while
loop, except the code block is
guaranteed to execute at least once.
Use Case: It is used when you need to
ensure that the code block runs at least
once, regardless of the condition.
Example:
let i = 0;
do {
console.log(i);
itt;
} while (i < 5);Loops: For Loop
A ‘for’ loop repeats a block of code a
specified number of times, based on the
provided condition.
Use Case: It is used when you know in
advance how many times the loop
should run.
Example:
for (let i = 0; i < 5; i++) {
console.log(i);
}How to find even numbers in an
array
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let evenNumbers = [];
for (let i = 0; i< numbers.length; i++) {
if (numbers[i] % 2 ===
{ evenNumbers.push(numbers[i]);
}
}
console.log("Even Numbers:",
evenNumbers);How to find max number in an array
let numbers = [3, 7, 2, 5, 9, 1, 4, 8];
let maxNumber = numbers[0];
for (let i = 1; i< numbers.length; i++) {
if (numbers[i] > maxNumber) {
maxNumber = numbers[i];
}
}
console.log("Maximum Number:", maxNumber);Loops: Foreach Loop
The 'forEach' loop is used to execute a
function once for each element in an
array.
Use Case: It is used when you want to
iterate over an array and perform an
actiog,ammach item.
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9,
10];
numbers.forEach(element => {
console.log(element)
);Functions
A function is a block of reusable code
designed to perform a single action or
calculate a value.
Use Case: Functions allow code
reusability, modularity, and cleaner code
organization.
Functions can accept parameters and
return values, which makes them highly
flexiblexte*pperforming various tasks.
function greet(name) {
return ‘Hello, '+ name;
console.log(greet(Alice’));