ITERATION (LOOP) PART II Thanachat Thanomkulabut Outline For statements Nested loops Break statements Continue statements  Looping or Iteration in C# while foreach Iteration (repeat) for do…while.

Download Report

Transcript ITERATION (LOOP) PART II Thanachat Thanomkulabut Outline For statements Nested loops Break statements Continue statements  Looping or Iteration in C# while foreach Iteration (repeat) for do…while.

ITERATION
(LOOP)
PART II
Thanachat Thanomkulabut
Outline
2
For statements
Nested loops
Break statements
Continue statements

Looping or Iteration in C#
3
while
foreach
Iteration
(repeat)
for
do…while
Kinds of Iteration statements
4
กำหนด
จำนวนครั้งที่
ทำซ้ ำได้
Iteration
Conditional
iteration
while
do...while
Definite
iteration
For
for statement
5
For Single Statement
for ( [initializers]; [condition]; [runner] )
statement;
For Multiple Statements
for ( [initializers]; [condition]; [runner] )
{
statement-1;
statement-2;
.
.
statement-N;
}
for statement
START
initializers
6
for ( [initializers]; [condition]; [runner] )
statement;
condition
true
Statement
runner
END
false
for statement
START
i=1
7
i <= 4
false
true
Example of for statement
static void Main()
{
int i;
FALSE
TRUE
for(i = 1; i <= 4; i++)
Console.WriteLine(i);
}
i
1
2
3
4
5
WriteLine(i)
i++
Output
1
2
3
4
END
For Loop vs While Loop
8
static void Main()
{
int i;
for(i=1; i<=4; i++)
Console.WriteLine(i);
}
int i=1;
while ( i<=4 )
{
Console.WriteLine(i);
i++;
}
For statement example
9
static void Main()
{
int k;
for (k = 0; k <= 4; k++)
Console.Write("A");
Console.Write(k);
}
Output
A A A A A5
for statement example
10

Write the program to show even number
2,4,6,...,n
static void Main()
{
int k,n;
Console.Write(“Input n :”);
n = int.Parse(Console.ReadLine());
for ( k = 1 ; k <= n ; k++ )
{
if(k%2==0)
Console.WriteLine(k);
}
}
IMPROVE
11

Write the program to show even number 2,4,6,...,n
static void Main()
{
int k,n;
Console.Write(“Input n :”);
n = int.Parse(Console.ReadLine());
for ( k = 2 ; k <= n ; k=k+2 )
{
Console.WriteLine(k);
}
}
x power n(คูณสะสม )
12
int i,n,x, product;
Console.Write( “Enter value of x : “ );
x = int.Parse( Console.ReadLine( ) );
Console.Write( “Enter value of n : “ );
n = int.Parse( Console.ReadLine( ) );
Product = 1
Product = 1*x
Product= (1*x)*x
Product= (1*x*x)*x
product = 1; i=0;
while (i < n)
{
product = product *x;
i = i+1;
}
Console.WriteLine(“{0} power {1}={2}”, x,n, product);
Find n factorial
13
int n,x,fact;
Console.Write( “Enter value of n : “ );
n = int.Parse( Console.ReadLine( ) );
fact = n;
x = n-1;
while (x >= 1)
{
fact = fact*x;
x = x-1;
}
Console.WriteLine(“{0}! = {1}”, n, fact);
N
i
Test I : Problem #1: Find
14
2
i 1
5
i2
= 1*1 + 2*2 + 3*3 + 4*4 + 5*5
i =1
static void Main()
{
int N=0, SUM=0, i=0;
Console.Write(”Please input number: ”);
N = int.Parse(Console.ReadLine());
for (
???
SUM = SUM +
;
???
;
???
)
??? ;
Console.WriteLine(”SUM = {0}”, SUM);
}
Outline
15




For statements
Nested loops
Break statements
Continue statements
Nested Loops : Example
16
for (int outer = 1; outer <= 4; outer++)
{
Console.WriteLine("Outer = {1} & Inner={0}”, 1, outer);
Console.WriteLine("Outer = {1} & Inner={0}”, 2, outer);
Console.WriteLine("Outer = {1} & Inner={0}”, 3, outer);
}
Nested Loops : Example
17
Nested for loops
 Do not use the same runner variable in the outer loop
and the inner loop
for (int outer = 1; outer <= 4; outer++) {
for (int inner = 1; inner <=3; inner++) {
Console.WriteLine("Outer = {1} & Inner={0}”, inner,outer);
}
}
Nested Loops : Example
Output
18
Outer = 1 & Inner = 1
for (int outer = 1; outer <= 4; outer++)
{
for (int inner = 1; inner <=3; inner++)
{
Console.WriteLine("Outer = {1} & Inner = {0}" , inner ,outer);
}
}
Outer = 1 & Inner = 2
Outer = 1 & Inner = 3
Outer = 2 & Inner = 1
Outer = 2 & Inner = 2
Outer = 2 & Inner = 3
Outer = 3 & Inner = 1
Outer = 3 & Inner = 2
Outer = 3 & Inner = 3
Outer = 4 & Inner = 1
Outer = 4 & Inner = 2
Outer = 4 & Inner = 3
Nested Loops : Example
19
Nested while loops
int outer = 1;
while (outer <= 4) {
int inner = 1 ;
while (inner <= 3) {
Console.WriteLine("Outer = {1} & Inner = {0}" , inner ,outer);
inner = inner +1;
}
outer = outer +1;
}
Nested Loops : Example
20
Nested do...while loops
int outer = 1;
do {
int inner = 1;
do {
Console.WriteLine("Outer = {1} & Inner = {0}" , inner ,outer);
inner = inner +1
} while (inner < 6);
outer = outer + 1;
inner = 1
}while (outer < 5);
Test II : Nested Loops : Example
21

Write the program to
Show output
1
12
123
1234
12345
int i,j;
for(
{
for(
{
}
}
;
;
,
)
,
)
Break statement
22

The break statement terminates the closest enclosing
loop or switch statement in which it appears
Break statement example
23
int i,j;
for(i=1;i<=10;i++)
{
Console.WriteLine(i);
if(i==5)
break;
}
Console.Write(“final i = {0}”,i);
i
1
5
4
3
2
Output
1
2
3
4
5
final i = 5
Continue Statement
24
i
sum
0
1
3
2
0 +1 +3 +5 +7+9
Output
final sum = 25
int i=0,sum=0;
do{
i++;
if(i%2==0)
continue;
sum = sum+i;
}while(i<10);
Console.WriteLine("final sum={0}",sum);
Continue statement example
25
int i=0,sum=0;
for(i=1;i<=10;i++ )
{
if(i%2==0)
continue;
sum = sum+i;
}
Console.WriteLine("final sum={0}",sum);
When program control reaches a continue statement, it transfers to the third
section within the for loop parentheses (i++). Once this is complete, control
transfers to the boolean expression for evaluation in the next iteration.
ANY
QUESTION?