0% found this document useful (0 votes)
19 views15 pages

Looping Techniques: Ali Shakir Alahmed

Uploaded by

Laween Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views15 pages

Looping Techniques: Ali Shakir Alahmed

Uploaded by

Laween Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Duhok Polytechnic University

Shekhan Technical Institute


Information Technology
Department

Looping Techniques

Ali Shakir Alahmed

2022 –
2023
Looping or Iteration in C#

while

for Iteration foreach

do…while

2
while statement

while
(condition)
statement;

3
Display 1 to 5 on screen
static void Main() {
int i;
i = 1;
while (i <= 5) { while version
Console.WriteLine(“{0}”, i);
i++;
}
}

static void Main() {


int i; for version
for (i=1; i<=5; i++) {
Console.WriteLine(“{0}”, i);
}
} 4
Examples
int i = 1;
while (i <= 10)
{
Console.WriteLine("THE VALUE FOR i :" + i);
i++;
}
Out put 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10
_____________________________________
int i = 1;
while (i<=10)
{
Console.WriteLine("THE VALUE FOR i :" + i);
i+=2;
}
Out put 1 , 3 , 5 , 7 , 9
5
Examples
int i = 10;
while (i >= 1)
{
Console.WriteLine("THE VALUE FOR i :" + i);
i--;
}
Out put 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
_____________________________________
int i = 10;
while (i >= 1)
{
Console.WriteLine("THE VALUE FOR i :" + i);
i-=2;
}
Out put 10, 8 , 6 , 4 , 2
6
Factorial Number: C# Program
 N! = N * (N-1) * (N-2) * (N-3) *...* 1
Example 5! = 5*4*3*2*1
static void Main() {
int N, FACT=1;
FACT = 1;
Console.Write(”Please input number: ”);
N = int.Parse(Console.ReadLine());

while ( ??????
N > 0 )
{
FACT = ??????
FACT * N ;
????
N-- ;
};
Console.WriteLine(”The factorial is {0}.”,FACT);
} 7
Control Loops: example
static void Main() {
int N, SUM=0;

Console.Write(”Enter number or -1 to quit”);


N = int.Parse(Console.ReadLine());

while (N != -1) {
SUM = SUM+N;
Console.Write(”Enter number or -1 to
quit”);
N = int.Parse(Console.ReadLine());

Console.WriteLine(“The sum is {0}.”, SUM);


}
8
Write a program in C# to find and display multiples of three (3) for
the numbers (3 to 27) and then add them?

9
Nested while Statements Example:

Output:
Line 1 : 0123456789
Line 2 : 0123456789
Line 3 : 0123456789
do…while statement
For Single Statement do {
statement;
} while
(condition);
For Multiple Statements
do
{
statement-1;
statement-2;
.
statement-N;
} while
(condition); 11
do…while statement
Do while :
int i = 1;
do
{
Console.WriteLine(i);
i++;
}
while (i <= 10);
Nested do while Statements Example:
int i = 1;
do
{
Console.WriteLine("line=" + i);
int j = 0;
do
{
Console.WriteLine(j);
j++; Output:
} Line 1 : 0123456789
while (j < 10); Line 2 : 0123456789
Line 3 : 0123456789
i++;
}
while (i < 4);
Console.ReadKey();
Control Loops: example
static void Main() {
int N, SUM=0;

do
{
Console.Write(”Enter number or -1 to quit”);
N = int.Parse(Console.ReadLine());
SUM = SUM+N;

} while (N != -1);

Console.WriteLine(“The sum is {0}.”, SUM);


}

14

You might also like