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
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
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);
Mumbai, often referred to as the financial capital of India, is a hub for businesses and industries. With countless corporate…
When it comes to security services in Noida, there are several reputable companies that cater to various needs. Let me…
Are you tired of dealing with tooth decay, gum problems, and expensive dental treatments? Look no further! Steel Bite Pro…
In the world of fine jewelry, oval moissanite rings have emerged as a captivating choice for those seeking elegance and…
Noida, located in the state of Uttar Pradesh, is a fast-growing hub of digital marketing agencies in India. Choosing the…
As we move into 2023, the world of motorcycles in India is set to see some exciting changes. Manufacturers are…
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.
Thank You!
Your wonderful feedback will aid us in moving superbly.