JavaScript.pdf

Full Transcript

CSS(22519) CP1-6 JavaScript Statements Selection Statements m: Explain selection statements with examples in JavaScript. Selection statements are used to make decisions in a program. They evaluate conditions and execute specific blocks of...

CSS(22519) CP1-6 JavaScript Statements Selection Statements m: Explain selection statements with examples in JavaScript. Selection statements are used to make decisions in a program. They evaluate conditions and execute specific blocks of code based on the result of those conditions. The most common selection statements in JavaScript are if, else if, else, and switch. 1. if-else Statement: The if-else statement executes a block of code if the condition is true; otherwise, it executes the else block. let age = 20; if (age >= 18) { console.log("You are an adult."); } else { console.log("You are a minor."); } 2. else if Statement: The else if statement is used to check multiple conditions in sequence. let score = 85; if (score >= 90) { console.log("Grade: A"); } else if (score >= 75) { console.log("Grade: B"); } else { console.log("Grade: C"); } 3. switch Statement: The switch statement evaluates an expression and matches the result with a case to execute corresponding code. let day = 2; switch (day) { 1 | Page CSS(22519) CP1-6 case 1: console.log("Monday"); break; case 2: console.log("Tuesday"); break; case 3: console.log("Wednesday"); break; default: console.log("Invalid day"); } IF Statement m: Explain the if statement with examples in JavaScript. The if statement in JavaScript is used to execute a block of code based on whether a specified condition evaluates to true. It allows for decision-making in your code by controlling the flow of execution. Syntax of if Statement: if (condition) { // code to execute if condition is true } condition: An expression that evaluates to true or false. Code Block: The code inside the curly braces {} runs if the condition is true. Code: if Statement with else To handle cases where the condition might be false, you can use an else block. let score = 50; if (score >= 60) { console.log("You passed."); 2 | Page CSS(22519) CP1-6 } else { console.log("You failed."); } IF…Else Statement m: Explain the if...else statement with examples in JavaScript. The if...else statement is used to execute one block of code if a condition evaluates to true and another block of code if the condition evaluates to false. This control structure allows you to handle two different paths of execution depending on whether the condition is met or not. Syntax of if...else Statement: if (condition) { // code to execute if condition is true } else { // code to execute if condition is false } condition: An expression that evaluates to true or false. Code Block for if: The code inside the curly braces {} that runs if the condition is true. Code Block for else: The code inside the curly braces {} that runs if the condition is false. Code: Basic if...else Statement let number = 10; if (number % 2 === 0) { console.log("The number is even."); } else { console.log("The number is odd."); } 3 | Page CSS(22519) CP1-6 IF…ElseIF Statement m: Explain the if...else if statement with examples in JavaScript. The if...else if statement is used to evaluate multiple conditions in sequence, providing a way to execute different blocks of code based on which condition is true. This structure allows you to handle more than two scenarios by chaining multiple conditions together. Syntax of if...else if Statement: if (condition1) { // code to execute if condition1 is true } else if (condition2) { // code to execute if condition2 is true } else if (condition3) { // code to execute if condition3 is true } condition1, condition2, condition3,...: Expressions that evaluate to true or false. Code Blocks: The code inside the curly braces {} for the first true condition will execute. If no condition is true, none of the else if blocks will run. Code: Grading System let marks = 82; if (marks >= 90) { console.log("Grade: A"); } else if (marks >= 80) { console.log("Grade: B"); } else if (marks >= 70) { console.log("Grade: C"); } else if (marks >= 60) { console.log("Grade: D"); 4 | Page CSS(22519) CP1-6 } else { console.log("Grade: F"); } Nested IF Statement m: Explain nested if statements with examples in JavaScript. Nested if statements are used to test multiple conditions by placing one if statement inside another. This allows for more complex decision-making processes, where a secondary condition is checked only if the primary condition is true. Nested if statements can be useful when you need to evaluate conditions in a hierarchical manner. Syntax of Nested if Statements: if (condition1) { // code to execute if condition1 is true if (condition2) { // code to execute if condition2 is also true } else { // code to execute if condition2 is false } } else { // code to execute if condition1 is false } condition1: The outer condition that is evaluated first. condition2: The inner condition that is evaluated only if condition1 is true. Code Blocks: The blocks of code inside the curly braces {} execute based on the truth value of the conditions. Code: Age and Membership Check let age = 25; 5 | Page CSS(22519) CP1-6 let hasMembership = true; if (age >= 18) { if (hasMembership) { console.log("Access granted to the VIP lounge."); } else { console.log("Access granted to the general area."); } } else { console.log("Access denied. You must be at least 18 years old."); } Switch Case Statement m: Explain the switch statement with examples in JavaScript. The switch statement in JavaScript is used to execute one block of code among many based on the value of an expression. It provides an alternative to multiple if...else if statements when dealing with multiple discrete values. The switch statement can be more readable and organized, especially when you have several possible values to handle. Syntax of switch Statement: switch (expression) { case value1: // code to execute if expression equals value1 break; case value2: // code to execute if expression equals value2 break; // more cases... default: // code to execute if no case matches } 6 | Page CSS(22519) CP1-6 expression: The value or variable to be compared with case values. case value1, case value2,...: The values to compare with the expression. break: Ends the current case block. Without break, execution will continue into the next case (known as "fall-through"). default: The block executed if none of the case values match the expression. It is optional. Code: User Role Access let role = "admin"; switch (role) { case "admin": console.log("Access to all features."); break; case "editor": console.log("Access to edit content."); break; case "viewer": console.log("Access to view content."); break; default: console.log("No access."); } Looping Statement m: Explain looping statements in JavaScript with examples. Looping statements in JavaScript are used to execute a block of code repeatedly based on certain conditions. They allow you to perform repetitive tasks efficiently. The main types of looping statements in JavaScript are for, while, and do...while. 7 | Page CSS(22519) CP1-6 For Loop m: Explain the for loop in JavaScript with examples. The for loop in JavaScript is a control flow statement used to repeat a block of code a specified number of times. It is particularly useful when the number of iterations is known before the loop starts. The for loop combines initialization, condition-checking, and iteration in a single line, making it concise and effective for various tasks. Syntax of for Loop: for (initialization; condition; increment/decrement) { // code to execute in each iteration } Initialization: Sets up the loop counter. This code runs once before the loop starts. Condition: Evaluated before each iteration. The loop continues as long as this condition is true. Increment/Decrement: Updates the loop counter after each iteration. This step usually increments or decrements the counter to eventually stop the loop. Code: Summing Numbers let sum = 0; for (let i = 1; i

Use Quizgecko on...
Browser
Browser