22.06.2016 Views

Loops_sushma

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

LOOPS<br />

By<br />

Sushma Desemsetty


What is Loop?<br />

<strong>Loops</strong> are used in programming to repeat a specific block until some end condition is met.<br />

There are four kinds loops.They are:<br />

1.for loop<br />

2.while loop<br />

3.do...while loop<br />

4.nested loops


for loop<br />

It's preferred when you know how many iterations you want, either because<br />

you know the exact amount of iterations, or because you have a variable<br />

containing the amount.<br />

Syntax : for (initialization-statement ; condition; increment/decrement)<br />

{<br />

// code<br />

}


for loop<br />

How for loop works?<br />

The initialization statement is executed only once.<br />

Then, the condition is evaluated. If the condition is false , for loop is terminated. But if<br />

the test expression is true , codes inside the body of for loop is executed and update<br />

expression is updated. This process repeats until the test expression is false.


class program<br />

{<br />

static void Main(string[] args)<br />

{<br />

int number = 5;<br />

for(int i = 0; i < number; i++)<br />

Console.WriteLine(i);<br />

Console.ReadLine();<br />

}<br />

}<br />

Example of for loop


while loop<br />

The while loop simply executes a block of code as long as the condition you<br />

give it is true.<br />

Syntax : while (condition)<br />

{<br />

//codes<br />

}


while loop<br />

How while loop works?<br />

The while loop evaluates the condition.<br />

If the condition is true , codes inside the body of while loop is evaluated. Then, again the<br />

condition is evaluated. The process goes on until the condition is false.<br />

When the condition is false, the while loop is terminated.


class Program<br />

{<br />

static void Main(string[] args)<br />

{<br />

int number = 0;<br />

while(number < 5)<br />

{<br />

Console.WriteLine(number);<br />

number = number + 1;<br />

}<br />

Console.ReadLine();<br />

}<br />

}<br />

Example of while loop


do-while loop<br />

The do..while loop is similar to the while loop with one important difference. The body of<br />

do...while loop is executed once, before checking the test expression. Hence, the do...while<br />

loop is executed at least once.<br />

Syntax : do<br />

{<br />

// codes<br />

}<br />

while (condition);


do-while loop<br />

How do-while loop works?<br />

The code block (loop body) inside the braces is executed once.<br />

Then, the condition is evaluated. If the condition is true, the loop body is executed again.<br />

This process goes on until the condition is evaluated to 0 (false).<br />

When the condition is false , the do...while loop is terminated.


class Program<br />

{<br />

static void Main(string[] args)<br />

{<br />

do<br />

{<br />

Console.WriteLine(number);<br />

number = number + 1;<br />

} while(number < 5);<br />

}<br />

}<br />

Example of do-while loop


Nested loops<br />

Loop can be used inside loop in any programming language including C#.<br />

Such loops are known as nested loops.<br />

There are three types of nested loops.They are:<br />

1.Nested for Loop<br />

2.Nested while Loop<br />

3.Nested Do While Loop


Nested for Loop<br />

Syntax :<br />

for (initialization; condition; increment/decrement)<br />

{<br />

for (initialization; condition; increment/decrement)<br />

{<br />

statements;<br />

}<br />

statements;<br />

}


class Program<br />

{<br />

static void Main(string[] args)<br />

{<br />

for (int i = 1; i


Nested while Loop<br />

Syntax :<br />

while (condition)<br />

{<br />

while (condition)<br />

{<br />

statements;<br />

}<br />

statements;<br />

}


int i = 5;<br />

while (i >= 1)<br />

{<br />

int j = 5;<br />

while (j >= I)<br />

{<br />

Console.Write(j);<br />

j--;<br />

}<br />

i--;<br />

Console.WriteLine();<br />

}<br />

Console.Read();<br />

Example of Nested while loop


Nested do-while Loop<br />

Syntax :<br />

do<br />

{<br />

do<br />

{<br />

statements;<br />

}while (condition);<br />

statements;<br />

}while(condition);


Example of Nested do-while loop<br />

int outerLoopNumber = 1;<br />

int sum = 1;<br />

do<br />

{<br />

int innerLoopNumber = 1;<br />

do<br />

{<br />

Console.Write(" {0}", sum);<br />

sum += 1;<br />

innerLoopNumber++;<br />

} while (innerLoopNumber


Methods of the Array Class<br />

The Array class is the base class for all the arrays in C#. It is defined in the System<br />

namespace. The Array class provides various properties and methods to work with arrays.<br />

*Properties Of Array Class<br />

*Functions of Array class


Properties<br />

Length<br />

Rank<br />

Properties Description Example<br />

Returns the length of<br />

array. Returns integer<br />

value.<br />

Returns total number<br />

of items in all the<br />

dimension. Returns<br />

integer value.<br />

int i = arr1.Length;<br />

int i = arr1.Rank;<br />

IsFixedSize<br />

Check whether array is<br />

fixed size or not.<br />

Returns Boolean value<br />

bool i = arr.IsFixedSize;<br />

IsReadOnly<br />

Check whether array is<br />

ReadOnly or not.<br />

Returns Boolean value<br />

bool k =<br />

arr1.IsReadOnly;


Functions of Array Class<br />

Function Description Example<br />

Sort Sort an array Array.Sort(arr);<br />

Clear<br />

Clear an array by removing<br />

all the items<br />

Array.Clear(arr, 0, 3);<br />

GetLength Returns the number of elements arr.GetLength(0);<br />

GetValue<br />

IndexOf<br />

Copy<br />

Returns the value of specified<br />

items<br />

Returns the index position of<br />

value<br />

Copy array elements to another<br />

elements<br />

arr.GetValue(2);<br />

Array.IndexOf(arr,45);<br />

Array.Copy(arr1,arr1,3);


Thank You

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!