C#

C#- Programming Guide | Starting Phase Learning(11)

Loopings in C#:

When we need to execute a block of code several times then we use a loop statement which allows us to execute a statement or a group of statements multiple times and following is the general example of a loop statement −

        Console.WriteLine("Welcome top tenOclocks");
        Console.WriteLine("Welcome top tenOclocks");
        Console.WriteLine("Welcome top tenOclocks");
        Console.WriteLine("Welcome top tenOclocks");
        Console.WriteLine("Welcome top tenOclocks");
        Console.WriteLine("Welcome top tenOclocks");
        Console.WriteLine("Welcome top tenOclocks");
        Console.WriteLine("Welcome top tenOclocks");
        Console.WriteLine("Welcome top tenOclocks");
        Console.WriteLine("Welcome top tenOclocks");
        Console.WriteLine("Welcome top tenOclocks");
        Console.WriteLine("Welcome top tenOclocks");

Here, instead of repeating the statements we can perform loopings.
i.e

for(int i = 0 ; i<12 ; i++)
{
Console.WriteLine(“Welcome top tenOclocks”);
}

This will help us to achieve several advanatages.
Advantages of using loops
1.Code redundancy
2.Reducing number of statemnets
3.More suitable
4.Faster
5.Saves time

We have different types of looping statements

1.while loop
It repeats a statement or a group of statements while a given condition is true. It tests the condition before executing the loop body.

2.for loop
It executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

3.do…while loop
It is similar to a while statement, except that it tests the condition at the end of the loop body

4.nested loops
You can use one or more loop inside any another while, for or do..while loop.

5.foreach, in
A loop like for but which has got effective uses while collecting object type data.

**Here in this phase, we will learn while and for loop. Remaining loops is dealt in the next learning phase

while loop:

The while statement executes a statement or a block of statements whenever the specified Boolean expression evaluates to true.
You can at any point within the while statement block, can break the loop. To break the loop we have the break statement.
You can exit a while loop by the goto, return, or throw statements.

Here, the initialization is done before the loop starts and increment or decrement steps fall inside the loop.

int i= 0;
while (i < 5) //Expression
{ //loop starts
Console.WriteLine(i);
++i; //incrementing the value of i after each loop
}

Explanation:
int i = 0 – Initialising teh value
while (i < 5) //Expression – Checks as like if else condition
Console.WriteLine(i); //prints the value of i
++i; //incrementing the value of i

For loop:

The for statement too executes a statement or a block of statements whenever the specified Boolean expression evaluates to true.
But here, we can initialize and increment the variable in for statement itself, giving an additional feature from while.
At any point within the for statement block, you can break out of the loop by using the break statement, or step to the next iteration in the loop by using the continue statement. You also can exit a for a loop by the goto, return, or throw statements.

Structure of the for statement
for (initializer; condition; iterator)
{
}

for (initializer; condition; iterator);//Its called an empty loop
for(;;;) //Infinite loop – It has no condition , initializer and iterator

All three sections are optional. The body of the loop is either a statement or a block of statements.
The for statement defines initializer, condition, and iterator sections:

for(int i = 0 ; i<12 ; i++)
{
Console.WriteLine(“Welcome to tenOclocks”);
}

Initializer :
It means initializing the value of a variable.Here you can keep more than one variables like
for(int i = 2 , a = 5 ; ; )

condition :
Similiar to if :
if( i > 2) {
}
Here too we define a condition, which is going to be called till the condition(expression written inside ) gets false.We can also end the loop within the body of for loop by using keyword i.e break;
for( ; i >=4 ; )//condition
{
//body
if(i == 3)
break;
)

iterator :
Iterator we use it to increement the value of a variable after each loop is made .
for( ; ; i++ , j– )//iterator i and j
{
//body
)

Combining all :
for(int i = 1 ; j = 10 ; i<= j ; i++,j–)//for loop
{
if(i == 3 )
break;
else if(j ==4)
continue;
else
console.WriteLine(i +j);
}

Explanation:
1.for expression-
Initializer int i = 1 , j = 6
Condition i<= j
Iterator i++,j–
2.After each loop the value of i gets incremented by 1 while value of j gets decremented by 1 .
3.In the body we have if else condition
4.When the value of i equals to 3 it just continues without printing anything
5.When the value of j equals to 4 the loop ends
6.else it prints the value of (i +j);

tenocloc

Leave a Comment

View Comments

  • Heу I am so thrilled I found your blog, I really found you by
    mistake, while I was searching on Askjeeѵe for
    something else, Regardless I ɑm here now and would ϳust like to say
    cheers for a remarkable post and a all round interesting blog
    (I also love the theme/design), I don’t have
    time to go through it all аt the minute but I hɑve book-marked it and also included your RSS feeds, so
    when I havе time I will be back to read much more, Please do keep up the superb b.

Recent Posts

Life in Ukraine: Everything You Need to Know

What Ukraine is like? Life in Ukraine: Ukraine is located in Eastern Europe. It is the second largest country on…

4 weeks ago

INDIAN WEDDING BRIDAL ENTRY IDEAS

Indian Wedding Entry Ideas Indian Wedding Bridal Entry Ideas are keeping trend nowadays in any Indian wedding. People are becoming…

1 month ago

Complete Guide: Facts To Know Before Buying Moissanites

Why people going for moissanites these days? Moissanites : Diamonds have become a symbol of love and commitment, but did…

1 month ago

Top 10 Indian Spices Used In Cooking

Top 10 Indian Spices: Food and taste is a special gift to humans. Indian food tastes are one of the…

2 years ago

Top 5 Indian Parathas To Have In This Winter

Top 5 Indian Parathas To Have In This Winter:Parathas are incredibly delicious with its texture, tenderness, exuding with butter and…

2 years ago

Tom Cruise Goes Angry On The Mission Impossible Crew

Tom Cruise Goes Angry On The Mission Impossible Crew: A famous Hollywood Star Tom Cruise who is currently being filming…

2 years ago