Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday 8 April 2015

UNDERSTANDING LOOPS IN C#

Today in Engineer's World, we are discussing a very important topic of C# - Loops in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS


Click imaginationhunt.blogspot to see latest Blogs

To read Can we have more than one main method in C#? - Click here.

LOOP's In C#

Loop is defined as structure, series, or process, the end of which is connected to the beginning.

What is looping in C#?
Looping is a concept in which we want to execute a block of code several number of times under certain conditions. A loop statement allows us to execute a statement or a group of statements multiple times and following is the general form of a loop statement in most of the programming languages:


Figure-1

First of all we need to understand why do we need to use loops? Let us understand this by taking a general example. Suppose we have to print Alphabet from A-Z. Now, without using loop, we have to write the print statement 26 times. But with loops all we have to need a single print command and we are able to access all the 26 alphabets.
C# provides following types of loop to handle looping requirements-

1. for loop:
 

The initialization statement is executed only once at the beginning of the for loop. Then, the condition is checked by the program. If the condition is false, loop is terminated. But if condition is true then the statement/code inside body is executed. This process repeats until condition is false.

Syntax: The Syntax for for loop is



 
Practical Implementation
Try this example to understand better


In this code, you see that it will print i from 1 to 26. So, the way it works is- the first time it comes into the for loop the value of i is set to 1. It will check here if i is less than or equal to 26 and 1 is less than 26, then it will come into the loop execute the console statement and print 'c' i.e. "A". And next time it goes over you will notice that something called as incrementor/ decrementor will increment the value of i by 1. Every time it comes into this particular loop the value is incremented by 1, till the value of i reaches to 27. Once it reaches by 27, condition fails. So it comes out of the particular loop.
Hence, this is how for loop execute.

2. while loop:
 

The while loop checks whether the condition is true or not. If it is true, code inside the body of while loop is executed. Before ending the braces { }, it will increment or decrement its value. Then again the condition is checked whether it is true or not. This process continues until the condition becomes false.

Click imaginationhunt.blogspot to see latest Blogs

Syntax: The Syntax for while loop is


Practical Implementation
Try this example to understand better


A while loop checks for the condition and if the condition is true, then it will print the body part. It will execute till the value of i reaches to 27. As soon as it reaches 27, the code stop and jump out of the loop.
Note: Use Namespaces as per the usage. Avoid unnecessary usage of them and Assembly.

3. do-while loop:
 

The do-while loop is similar to while loop with one very important difference. In while loop, condition is checked at first before body of loop but in case of do-while loop, body of loop is executed first then only condition is checked. That's why the body of do-while loop is executed at least once.

Syntax: The Syntax for do-while loop is

 

Practical Implementation
Try this example to understand better
 
 

According to do-while and above code it will come and assign the value of i to 1 and at console it will print the value of c i.e., 'A'. Then it will increment the value by 1. So i become 2 and now it will check 2 is less than equal to 26, true. Do this loop again. So you see that do while loop will loop till this while condition is met. Now,  it seems that you have a question in your mind, what is the difference between while and do-while loop. If you notice the while loop first check for the condition and then it print out like run the block of code, whereas the do-while loop will first execute the block of code and then it will check for the condition. So you see that even if the condition is wrong. Suppose we have initialized the value of i=58 which is not less than 26. Even then this particular do-while will execute this block of code which is not in case for while looping.
So, the do-while loop will guarantee that it will run at least once, whereas the while statement will check for the condition and only if the condition is true it will run this.

4. nested loop: 

You can use one or more loop inside any another while, for or do-while loop.

Syntax: The Syntax for nested loop is


Practical Implementation
Try this example to understand better
 

The example code is build using for loop but you can use any of the 3 loop(for or while or do-while) and make them nested. And output for the above code is shown in figure below.


To read Data Types in C# - Click here


Related Questions:


#newtoprgm try firstprogram

Q-1 Select the output for following set of code:

static void Main()
{
      int i;
      for(i=0; ; )
      {
            Console.WriteLine("Imaginationhunt.blogspot");
      } 
      Console.ReadLine();

    A) No output
    B) hello
    C) hello printed infinite times
    D) Code will give error as expression syntax
     

    Ans- Option(c)
    Explanation: Condition for the loop is absent. So, loop keep executing till infinite times. And output is like:
    hello
    hello
    hello
    .
    .
    .



    Q-2 Write the code for this pattern? 


    Ans- This can be achieved by using nested looping


    Click imaginationhunt.blogspot to see latest Blogs

    Keep learning and sharing...
    Read More »

    Featured post

    Think that makes you rich and richer

     Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...