Part 3: Control Statements

Previous Part
Part 2: Data Types and Variables

Control structures are used in programming to execute different actions based on certain conditions or to repeat a set of actions a certain number of times.

In C#, there are three main types of control structures: conditional statements, loops, and jump statements.

    1.Conditional statements: if-else statements, switch statements

    2.Loops: for loops, while loops, do-while loops

    3.Break and continue statements

Conditional statements

a. if-else statements

The if-else statement allows you to execute a block of code if a particular condition is true, and another block of code if the condition is false.

Syntax:

      if(condition)
      {
          // code to be executed if the condition is true
      }
      else
      {
          // code to be executed if the condition is false
      }


Example:


            int x = 10;

            if(x < 20)
            {
                Console.WriteLine("x is less than 20");
            }
            else
            {
                Console.WriteLine("x is greater than or equal to 20");
            }
                


Output:

       x is less than 20

        



b. switch statements
The switch statement allows you to execute different blocks of code based on the value of an expression.

Example:

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

  



Output:

       Wednesday

        



Loops

Loops are used to repeat a block of code a certain number of times or until a certain condition is met. There are three main types of loops in C#: for loops, while loops, and do-while loops.


a. For loops
For loops allow you to execute a block of code a certain number of times.

Here's an example:

      for (int i = 0; i < 10; i++)
      {
          Console.WriteLine(i);
      }
        



b. While loops
While loops allow you to execute a block of code as long as a certain condition is true.

Here's an example:

      int i = 0;
      while (i < 10)
      {
          Console.WriteLine(i);
          i++;
      }

        



c. Do-while loops
Do-while loops are similar to while loops, but the condition is checked at the end of the loop, so the block of code is always executed at least once.

Here's an example:


      int i = 0;
      do
      {
          Console.WriteLine(i);
          i++;
      } while (i < 10);

        



Jump statements

Jump statements are used to transfer control to a different part of the program. There are two main types of jump statements in C#: break and continue.

a. Break:
The break statement is used to exit a loop or switch statement.

Here's an example:

      for (int i = 0; i < 10; i++)
      {
          if (i == 5)
          {
              break;
          }
          Console.WriteLine(i);
      }

        



b. Continue:
The continue statement is used to skip the current iteration of a loop and move on to the next iteration.

Here's an example:

      for (int i = 0; i < 10; i++)
      {
          if (i == 5)
          {
              continue;
          }
          Console.WriteLine(i);
      }

        


Next Part:

Part 4: Arrays and Strings in C#

No comments:

Post a Comment