Topic : Loops in C# Programming Language: Loop Concept:



What is loop:

Loops are used to perform a set of instructions repeatedly. Loops allow us to perform statements repeatedly and just as importantly, to stop the action when warranted. Frequently, it's decision-making that makes computer programs seem smart, but looping makes program powerful. by using loops, we can write one set of instruction that executes thousands or even millions of times.

The repetitive capabilities of computers make them good tools for processing large amounts of information. there may be a situation, when we need to execute a block of code several numbers of times. in our daily life, most of the things are repeated. Days and nights repeat themselves 30 times a months. Four seasons replace each other every year. we can see similar phenomenon in the practical life.
For Example: in the payroll system, some procedures are same for all the employees. These are repeatedly applied while dealing with the employees. So repetition is very useful structure in the programming.



loop concept

For Example:

Let's suppose we have to calculate the sum of first 10 whole numbers i.e. add the numbers from 1 to 10. Following statement may be helpful to do it.

int x = 1+2+3+4+5+6+7+8+9+10;


This method is perfectly fine as the syntax is right. The answer is also correct. But what's about if we need to calculate first 1000 integers (1-1000 digits).. can we use same statement that we had used for 1-10 digits. yes we can, but its not suitable for computing the sum of these huge numbers. the addition of a very big number of digits will result in a very ugly and boring statement. but programming came in our life to make our life easier, not to bore us. well is here any way through which we can add huge numbers easily. yes there is. C++ Provide us loop, by using loops we can do this task within just few steps: let's analyze it carefully.

As our first integer is 1, is there any other way to find our what's the next integer? Yes, we can add 1 to the integer and get the next integer which is 2. To find the next integer (i.e. 3) we add 1 to the previous integer (i.e. 2) and get the next integer which is 3. so whenever we have to find out the next integer, we have to add 1 to the previous integer and so on. its loop, because the same thing happen again and again. so we don't need to do this task again and again. our computer can do this by itself using loops.

C++ programming language provides the following type of loops to handle looping requirements.



  Loop Types. Description
for Loopfor Loop Execute a Sequence of statements multiple times and abbreviates the code that manages the loop variables. (Read more...)
While LoopWhile loop Repeats a statement or group of statements while a given condition is true. it tests the condition before executing the loop body. (Read more...)
do - while LoopDo - while Loop same like a "while" loop. but its checks the condition at the bottom of the loop. and is guaranteed to execute at least one time. (Read more...)
Nested LoopsWe can use one or more loop inside any another "while", "for", or "do-while" Loop.


Loop Control Statements:

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. C# Provides following control statements.


Control StatementDescription
Break Statement:Terminates the Loop or Switch statement and transfers execution to the statement immediately following the loop or switch. (Read more...)
Continue Statement:Causes the Loop to skip the remainder of its body and immediately retest its condition prior to reiterating. (Read more...)
goto Statement:This statement provide an unconditional jump from the goto to the labeled statement in the same function. (Read more...)
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement