Post

# Overview Of Loops In C#

Hello Good Morning Folks,Hope You all are doing well and learning the new things everyday with your full of energy.

Today, We are going to learn the new thing with just doing some crazy things with the C-Sharp Loops and it contains the solution for the Hackerrank Day 5 30daysofcode Challenge Solution.So, Let's Begin with understanding the three words "What","Why" and "How". In the terms of

• What are the Loops?
• Why we have to or we should have to use Loops ?
• How we can implement the Loops ?

So, Firstly the Loops is something which we can say that it is a logical statements which are executed multiple time by providing some starting and the ending number of steps.

Let' say i want some functionality like whenever i send some number to the programmer it should have to give me the table of that number as example i want the table of the 3.

So, What are the steps i am going to do.

Take input from user,

``````//imports
//namespace
//class
//main methos

/*...Now the body parts goes here...*/

//close main
//close class
close namespace``````

Multiply it with 1 and print the out put then multiply with 2 and print output and so on till the multiply it with 10 and showing the output. Still Confusing let' s see the below code snippet.

``````/*...Inside the Body Part after getting the input from user...*/\

Console.WriteLine("{0} X 1 = ",n,(n*1));
Console.WriteLine("{0} X 2 = ",n,(n*2));
Console.WriteLine("{0} X 3 = ",n,(n*3));
Console.WriteLine("{0} X 4 = ",n,(n*4));
Console.WriteLine("{0} X 5 = ",n,(n*5));
Console.WriteLine("{0} X 6 = ",n,(n*6));
Console.WriteLine("{0} X 7 = ",n,(n*7));
Console.WriteLine("{0} X 8 = ",n,(n*8));
Console.WriteLine("{0} X 9 = ",n,(n*9));
Console.WriteLine("{0} X 10 = ",n,(n*10));``````

So, As you can see on the above code snippet we can get the whole table of the user inputed number. but here thing which is going wrong is the usage of the functionality we already have . just look at the code again and think which is the common part is going same in all of the statement which is printing the output ?

Yes, Correct the multiplication with n by different number which we can achieve by using the loop by simply increment it till the 10 so, here are some of the loops which we can use

• For Loop - take a intilize variable then confition till that condition it works and the increment or decrement args based on requirement
• While Loop - it only take a condition as args and work till the argument becomes false
• Do While loop - it will execute first and then check the condition and work same like the while but it will execute one time even if the condition is false.

Note
There are much more chances that while or do-while loop can go into the infinite state if your logic or condition is not strong or perfect.

Now, Let's see why we have to use this ? In the above code snippet i have given the small example but what if we want the multiplication of the 1 to 100 for particular number. Yes, it is very much difficult to count the number of lines and also the work with the console.writeline. So, For that purposes we have to use this loops concept.

Also, Now let' Move on to the part of the coding example, here the following example is for the for loop.

``````//imports
//namespace
//class
//main method

//let n as the inputted variable.
for(int i=1;i<=10;i++)
{
Console.WriteLine("{0} X {1} = {2}",n,i,n*i);
}``````

The above code snippet used the for loop which makes the code more easy to understand and modify the condition as per our requirement if we require to update till the table from 1 to 100 for particular number we just have to simply modify the condition which makes a sence and easy for you and also provide the code enhancement.

For the While Loop,

``````//imports
//namespace
//class
//main method

//let n as the inputted variable.
int i=1;
while(i<=10)
{
Console.WriteLine("{0} X {1} = {2}",n,i,n*i);
i++;
}``````

The above code snippet which can used with the while loop where we just require to give the condition for the looping.

following code is for the do while loop.

``````//imports
//namespace
//class
//main method

//let n as the inputted variable.
int i=0;
do
{
i++;
if(i>0 && i<=10)
{
Console.WriteLine("{0} X {1} = {2}",n,i,n*i);
}
}while(i<=10);``````

Here , in the above code snippet you can see the do...while which is going to execute first and then make the i as 1 and then check the condition and and then you get the first output and then checked with the while condition and it continuous till the 10 steps....

here, is the full source for the table for any particular number by using the all looping controls in the comment section of the code you can check and uncomment which you want...

NOTE
This code is the solution for the HackerRank 30days of coding solution Loops.

``````using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution
{
public static void Main(string[] args)
{
}
public static void printTableWithFor(int n)
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(n + " x " + i + " = " + (n * i));
}
}
public static void printTableWithWhile(int n)
{
int i=1;
while(i <= 10)
{
Console.WriteLine(n + " x " + i + " = " + (n * i));
i++;
}
}
public static void printTableWithDoWhile(int n)
{
int i=0;
do
{
i++;
if(i>0 && i<=10)
Console.WriteLine(n + " x " + i + " = " + (n * i));

}while(i <= 10);
}
}
``````