3.5 Loops

Loops are a way to iterate over something for finite or infinite amount of times.

Let's say we want to write "Hello, World!" 2 times in C#, we just need to write "Console.WriteLine("Hello, World!"); 2 times, but what if we have to write "Hello, World" a 100 times or 1000 times just like we used to get punishment at school of writing that we won't do this again after doing some mistakes 😂. So we have 2 options to go for 1st is a non programmers way that is write "Hello, World!" a 1000 times and 2nd is the programmer's way that is to use a looping construct and fool the punisher 😂.

Let's try to implement this punishment scenario, but before implementing the example let's understand "For Loops".

For Loops

For Loops are a way to iterate over some sort of range, it has 3 components initialization/start, condition/stop and step.

for(initialization/start; condition/stop; steps) {
    // Code
}

So, now as we have understood the structure of For Loop, it's our duty to fool the punisher and code a solution for fooling the punisher.

As we are programmers we will always go for the programmers way 😂. So, let's code our problem awayyyy!

for(int i = 0; i<1000; i++) {
    Console.WriteLine("Hello, World!");
}

So, now as we learnt about For Loops, it's now time to solve some problems on it.

1. WAP to print a table of a number using for loops.

using System;
class HelloWorld {
  static void Main() {
    int num = 2;
    for(int i = 0; i<12; i++) {
        int final = num * i;
        Console.WriteLine(num + " * " + i + " = " + final);
    }
  }
}

Output

2 * 0 = 0
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
2 * 11 = 22

2. WAP to print a factorial of a number using for loops.

using System;
class HelloWorld {
  static void Main() {
    int num = 4;
    int fac = 1;
    for(int i = num; i>0; i--) {
        fac = fac * i;
    }
    Console.WriteLine("Factorial of " + num + " = " + fac);
  }
}

Output

Factorial of 4 = 24

While Loops

While loop is like a brother of For Loop, they are very good friends and are often used. While loops runs till the condition is true.

To write a while loop we have to initialize a variable, then a condition and later on increment or decrement.

While loops should be used when we don't know how many times we want to loop and it will stop when a certain condition is met.

Structure of While Loop

while(condition) {
    // Do something
}

So, as we have learnt the structure of While Loop, so now let's fool the punisher again 😂 but this time using While Loop.

using System;
class HelloWorld {
  static void Main() {
    int i = 0;
    while(i<100) {
        Console.WriteLine("Hello, World! " + i);
        i += 1;
    }
  }
}

As you have learnt While Loop, now you have to practise the some problems.

1. WAP to print a table of a number using while loops.
using System;
class HelloWorld {
  static void Main() {
    int num = 2;
    int table = 0;
    
    while(table <= 12) {
        int final = num * table;
        Console.WriteLine(num + " * " + table + " = " + final);
        table += 1;
    }
  }
}

Do While Loops

The do while loop is similar to the while loop with one main difference - the code within the curly braces of a do while loop is executed at least once even if the condition is false.

do {
    // Code
} while(condition);

Let's again fool the punisher again 😂.

using System;
class HelloWorld {
  static void Main() {
      int count = 0;
      
      do {
          Console.WriteLine("Hello, World! " + count);
          count+=1;
      } while(count <= 100);
  }
}

NOTE: Remember, in a do while loop even if you condition isn't true the problem will loop atleast once.

Jump Statements

A jump statement is a statement that instructs the program to deviate from its normal flow sequence and jump to another line of code. Jump statements are commonly used in loops and other control flow statements.

Break Statements

The break statement is used to terminate the loop or statement in which it present.

Continue Statements

This statement is used to skip over the execution part of the loop on a certain condition.