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

javascriptlesson5

This document covers JavaScript control flow, including if statements, switch statements, and various types of loops such as for, while, and do...while. It also explains error handling techniques using try...catch blocks and the throw statement. The content is structured to provide examples and explanations for each concept to enhance understanding.

Uploaded by

vanguyen103
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

javascriptlesson5

This document covers JavaScript control flow, including if statements, switch statements, and various types of loops such as for, while, and do...while. It also explains error handling techniques using try...catch blocks and the throw statement. The content is structured to provide examples and explanations for each concept to enhance understanding.

Uploaded by

vanguyen103
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

JavaScript Lesson 5: Control Flow, Loops, and Error Handling

Page 1: Introduction to Control Flow

 What is Control Flow?

o Control flow refers to the order in which the computer executes statements in a script.
JavaScript normally runs code line by line, but control flow allows the code to make
decisions and repeat actions.

 if Statement:

o The if statement is used to execute a block of code if a specified condition is true.

Example:

else and else if:

 You can use else to define a block of code that will run if the condition is false, and else if to
check multiple conditions.

Example:

Page 2: switch Statement


 switch Statement:

o The switch statement is another way to execute different blocks of code based on
different conditions. It’s often used when there are multiple possible outcomes for a
single expression.

Example:

 Why Use switch?

o The switch statement is generally used when you have many conditions based on the
same value. It can be cleaner and more readable than using multiple if-else statements.

Page 3: Loops in JavaScript

 Introduction to Loops:

o Loops allow you to repeat a block of code multiple times until a specified condition is
met. JavaScript supports several types of loops, including for, while, and do...while.

 for Loop:

o The for loop is the most common loop used to iterate over arrays and other sequences.

Example:
 Explanation:

 Initialization: let i = 0 (sets up a loop variable).

 Condition: i < 5 (loop runs while this is true).

 Final Expression: i++ (increases i by 1 after each loop iteration).

 while Loop:

 The while loop repeats as long as the condition is true.

Example:

Page 4: do...while Loop and forEach Loop

 do...while Loop:

o The do...while loop is similar to the while loop, but it guarantees the loop will run at
least once, because the condition is checked after the code block is executed.

Example:
forEach() Loop:

 The forEach() loop is specifically designed to loop through arrays. It takes a callback function that
will be executed for each element of the array.

Example:

Error Handling in JavaScript

 Introduction to Error Handling:

o Errors in your code can stop your program from working correctly. JavaScript provides
mechanisms to catch and handle errors, so your program doesn't crash unexpectedly.

 try...catch Block:

o The try...catch statement allows you to run code that may throw an error and handle
that error gracefully.

Example:
throw Statement:

 You can also throw your own errors using the throw statement.

Example:

You might also like