in

C# Loops and Examples

In the C# programming language, loops are control structures used to execute a given block of code more than once. Loops allow repetitive operations to be performed efficiently, making the code easier to read and maintain. There are three basic loop structures commonly used in C#: for, while and do-while loops. The for loop is typically used for operations that need to be repeated a certain number of times and contains the loop’s start, end and increment conditions.

The while loop repeats the block of code as long as a given condition is true, while the do-while loop checks the condition after it has been executed at least once. Loops are often used with data structures such as arrays and collections in iteration processes and contribute to making programs dynamic and flexible. When used correctly, C# loops are powerful and effective tools in programming.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Loops
{
    class Program
    {
        static void Main(string[] args)
        {
            //ForLoop();

            //WhileLoop();


            The difference with the //do-whihle loop is that it runs first and then checks the condition, so it will run 1 time even if the condition is not met

            //DoWhileLoop();

            //ForeachLoop();

            Console.ReadLine();
        }

        private static bool IsPrimeNumber(int number)
        {
            bool result = true;
            for (int i = 2; i < number-1; i++)
            {
                if (number%i == 0 )
                {
                    result = false;
                    i = number;  //for performance reasons, we used it like a "break" statement so that it does not continue once it finds that it is not prime

                }
            }
            return result;
        }
        private static void ForeachLoop()
        {
            string[] students = { "Berra", "Ali", "Ahmet" };
            foreach (var student in students)
            {
              //you cannot change the elements returned in the forach loop
//student="Engin" cannot be called
                Console.WriteLine(student);
            }
        }

        private static void DoWhileLoop()
        {
            int number = 10;

            do
            {
                Console.WriteLine(number);
                number--;

            } while (number >= 0);
        }

        private static void WhileLoop()
        {
            int number = 100;

            while (number >= 0)
            {
                Console.WriteLine(number);It runs forever as long as // is greater than 0.
                number--; //since it won't run forever
            }
            Console.WriteLine("Current number is {0}", number); //(-1)
        }

        private static void ForLoop()
        {
            for (int i = 0; i <= 100; i++) //(writing "i+=2" is the same as writing "i=i+2")
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("Finished");

            for (int i = 100; i >= 0; i = i - 2)
            {
                Console.WriteLine(i);
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.