Harshil Chovatiya - Day 5 and 6: Control Flow - Loops, Loop Control Statements, and Functions in JavaScript

Harshil Chovatiya - Day 5 and 6: Control Flow - Loops, Loop Control Statements, and Functions in JavaScript

Harshil Chovatiya - Day 5 and 6: Control Flow - Loops, Loop Control Statements, and Functions in JavaScript

Day 5: Control Flow - Loops in JavaScript

Overview:

  • Loops allow you to repeatedly execute a block of code based on a condition.
  • JavaScript supports `for` and `while` loops.

In-Depth:

1. for Loops:

`for` loops are used when you know in advance how many times you want to execute the code.

                    
                    
    for (var i = 0; i < 5; i++) {
      console.log("Iteration " + i);
    }
                    
                

2. while Loops:

`while` loops are used when you want to repeat code while a condition is true.

                    
                    
    var count = 0;
    while (count < 5) {
      console.log("Count: " + count);
      count++;
    }
                    
                

3. do...while Loops:

`do...while` loops are similar to `while` loops, but the code block is executed at least once, even if the condition is false.

                    
                    
    var x = 5;
    do {
      console.log("This will run at least once.");
    } while (x < 3);
                    
                

Day 6: Control Flow - Loop Control Statements and Functions

Overview:

  • Loop control statements allow you to fine-tune the behavior of loops.
  • Functions are reusable blocks of code that can be called with different inputs.

In-Depth:

1. Loop Control Statements (Continued):

`break` is used to exit a loop prematurely.
`continue` is used to skip the current iteration and move to the next one.

                    
                    
    for (var i = 0; i < 10; i++) {
      if (i === 5) {
        break; // Exit the loop when i is 5
      }
      if (i === 3) {
        continue; // Skip iteration when i is 3
      }
      console.log("Iteration " + i);
    }
                    
                

2. Functions:

Functions are blocks of code that can be reused by calling their name.
They can accept input values called parameters and return output values.

                    
                    
    // Function declaration
    function greet(name) {
      return "Hello, " + name + "!";
    }
    
    // Function call
    var message = greet("John");
    console.log(message); // Output: "Hello, John!"
                    
                

3. Function Parameters and Return Values:

Functions can have parameters to accept input values.
They can also return values using the `return` keyword.

                    
                    
    function add(x, y) {
      return x + y;
    }
    var sum = add(3, 4);
    console.log(sum); // Output: 7
                    
                

4. Function Expressions:

Functions can also be defined as expressions, often used for anonymous functions or callbacks.

                    
                    
    var multiply = function(x, y) {
      return x * y;
    };
    var product = multiply(2, 5);
    console.log(product); // Output: 10
                    
                

Days 5 and 6 combine the concepts of loops, loop control statements, and functions in JavaScript. These topics are essential for creating dynamic, efficient, and reusable code. Loops help automate repetitive tasks, while functions allow you to modularize your code for better organization and maintainability. Understanding how to use these features is key to becoming a proficient JavaScript programmer.

Comments