0% found this document useful (0 votes)
2 views

December 2024 class - Switch, Loops, and Functions

This lab manual focuses on fundamental JavaScript concepts including Switch Case, Loops, and Functions, providing explanations, syntax, examples, and exercises for practice. It covers how to use switch statements, various types of loops (for, while, do...while), and how to define and use functions. The manual is designed for hands-on learning and reinforces concepts through practical exercises.

Uploaded by

Johan Cortez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

December 2024 class - Switch, Loops, and Functions

This lab manual focuses on fundamental JavaScript concepts including Switch Case, Loops, and Functions, providing explanations, syntax, examples, and exercises for practice. It covers how to use switch statements, various types of loops (for, while, do...while), and how to define and use functions. The manual is designed for hands-on learning and reinforces concepts through practical exercises.

Uploaded by

Johan Cortez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical Information Technology

https://thePracticalIT.com
(240) 877-8161 | [email protected]
911 Silver Spring Ave STE 101, Silver Spring MD 20910

Lab Session: Switch Case, Loops (for, while, do...while, continue, break),
and Functions

Date: March 11 2025

Objective: This lab manual is designed to help you understand and practice fundamental
JavaScript concepts: Switch Case, Loops (for, while, do...while, continue, break), and
Functions. Each section includes an explanation, syntax, examples, and hands-on exercises to
reinforce your learning.

How to Use This Manual


Read the explanation and study the syntax.
Run the provided examples in your browser’s console or a .js file linked to an HTML page.

Part 1: Switch Case


Objective
Learn how to use the switch statement to execute different blocks of code based on the value of
an expression.

Explanation
The switch statement evaluates an expression and matches its value to a case label. If a match
is found, the corresponding block of code executes. The break keyword prevents "fall-through"
to subsequent cases, and default handles unmatched cases.

Syntax
switch (expression) {
case value1:
// Code to execute if expression === value1
break;
case value2:
// Code to execute if expression === value2
break;
default:
// Code to execute if no case matches
}
Example:
let day = 3;
let dayName;

switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
default:
dayName = "Invalid day";
}

console.log(dayName); // Output: Wednesday

Exercises
1.​ Write a switch statement to determine the month name based on a number (1-12). Test
with month = 5 and month = 13.
2.​ Create a switch statement that takes a letter grade (A, B, C, D, F) and outputs a
message like "Excellent" for A, "Good" for B, etc. Include a default case for invalid
grades.

Part 2: Loops (for, while, do...while, continue, break)


Objective
Understand how to use loops (for, while, do...while) to repeat code and control flow with
continue and break.

Explanation
Loops allow repetitive execution of code. JavaScript provides three main loop types:

●​ for loop: Best for a known number of iterations.


●​ while loop: Runs as long as a condition is true.
●​ do...while loop: Runs at least once, then checks the condition.

●​ continue: Skips the current iteration and moves to the next.


●​ break: Exits the loop entirely.
Syntax and Examples
1. For Loop
Syntax:
for (initialization; condition; increment) {
// Code to repeat
}

Example:
for (let i = 1; i <= 5; i++) {
console.log(i); // Output: 1, 2, 3, 4, 5
}

2. While Loop
Syntax:
while (condition) {
// Code to repeat
}

Example:
let count = 0;
while (count < 3) {
console.log("Count: " + count); // Output: Count: 0, Count: 1, Count: 2
count++;
}

3. Do...While Loop
Syntax:
do {
// Code to repeat
} while (condition);

Example:
let num = 0;
do {
console.log("Number: " + num); // Output: Number: 0
num++;
} while (num < 1);

4. Continue and Break


Example with continue:
for (let i = 0; i < 5; i++) {
if (i === 2) continue; // Skip 2
console.log(i); // Output: 0, 1, 3, 4
}

Example with break:


for (let i = 0; i < 5; i++) {
if (i === 3) break; // Stop at 3
console.log(i); // Output: 0, 1, 2
}

Exercises

1.​ Write a for loop to print even numbers from 1 to 10.


2.​ Use a while loop to count down from 5 to 1 and print each number.
3.​ Create a do...while loop that prompts the user for a number until they enter a value
greater than 10 (use prompt() in the browser).
4.​ Modify the for loop from Exercise 1 to skip the number 6 using continue.
5.​ Write a loop that prints numbers from 1 to 10 but stops at 7 using break.

Part 3: Functions

Objective

Learn how to define and use functions to encapsulate reusable code in JavaScript.

Explanation

Functions are blocks of code designed to perform specific tasks. They can take parameters
(inputs), return values (outputs), and be reused throughout a program.

Syntax

function functionName(parameter1, parameter2) {

// Code to execute

return value; // Optional


}

Example with Parameters and Return

function greet(name) {

return "Hello, " + name + "!";

console.log(greet("Alice")); // Output: Hello, Alice!

Exercises

1.​ Write a function isEven(num) that takes a number and returns true if it’s even, false
otherwise.
2.​ Create a function factorial(n) that calculates the factorial of a number (e.g., 5! = 5 × 4 × 3
× 2 × 1).
3.​ Define a function square(x) that returns the square of a number.
4.​ Write a function printNumbers(start, end) that prints all numbers between start and end
(inclusive) using a for loop.

You might also like