Upload
austin-foster
View
46
Download
1
Embed Size (px)
DESCRIPTION
Iteration (Loop) part II. Thanachat Thanomkulabut. Outline. For statements Nested loops Break statements Continue statements. Iteration. (repeat). Looping or Iteration in C#. while. do…while. foreach. for. Kinds of Iteration statements. กำหนด จำนวนครั้งที่ทำซ้ำได้. for statement. - PowerPoint PPT Presentation
Citation preview
ITERATION ITERATION (LOOP) (LOOP) PART IIPART II
Thanachat Thanomkulabut
Outline
For statementsNested loopsBreak statementsContinue statements
2
Looping or Iteration in Looping or Iteration in C#C#
3
foreachforeach
whilewhile
forfor
do…whiledo…while
Kinds of Iteration statements
4
กำ��หนดกำ��หนดจำ��นวนจำ��นวน
ครั้��งที่��ที่��ครั้��งที่��ที่��ซ้ำ���ได�ซ้ำ���ได�
for statement5
for for ( [initializers]; [condition]; [runner] )) statement;statement;
For For MultipleMultiple Statements Statements
For For SingleSingle Statement Statement
for ( for ( [initializers]; [condition]; [runner] ) { statement-1;statement-1; statement-2;statement-2; .. .. statement-N;statement-N;}
for statementfor statement6
STARTSTARTSTARTSTART
ENDENDENDEND
conditionconditionconditioncondition
true
false
StatementStatementStatementStatement
runnerrunnerrunnerrunner
initializersinitializersinitializersinitializers
for ( for ( [initializers]; [condition]; [runner] )) statementstatement;;
for statement7
STARTSTARTSTARTSTART
ENDENDENDEND
i <= 4i <= 4i <= 4i <= 4true
false
WriteLine(i)WriteLine(i)
i++i++i++i++
i = 1i = 1i = 1i = 1
static void Main() {
int i;for(i = 1; i <= 4; i++)Console.WriteLine(i);
}
Example of for statementExample of for statement
i
1
TRUE
1Output
2
2
3 344
5
FALSE
For Loop vs While Loop8
static void Main() static void Main() {
int int i;i;
for(for(i=1i=1; ; i<=i<=44; ; i++i++)) Console.WriteLine(i Console.WriteLine(i));;}
int int i=1;i=1; while (while ( i<=i<=4 4 )){{ Console.WriteLine(iConsole.WriteLine(i));; i++i++;;}}
For statement example9
static void Main()
{
int k;
for (k = 0; k <= 4; k++)
Console.Write("A");
Console.Write(k);
}
A A A A AOutpOutp
utut 5
for statement example Write the program to show even
number 2,4,6,...,n
10
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
Write the program to show even number 2,4,6,...,n
11
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; i=0;product = 1; i=0; while (i < n)while (i < n) {{ product = product *x;product = product *x; i = i+1;i = i+1; } } Console.WriteLine(“{0} power {1}={2}”, x,n, product);
Product = 1Product = 1*xProduct= (1*x)*xProduct= (1*x*x)*x
Find n factorial13
int n,x,fact; Console.Write( “Enter value of n : “ ); n = int.Parse( Console.ReadLine( ) ); fact = n;fact = n; x = n-1;x = n-1; while (x >= 1)while (x >= 1) {{ fact = fact*x;fact = fact*x; x = x-1;x = x-1; } } Console.WriteLine(“{0}! = {1}”, n, fact);
Test I : Problem #1: Find
N
i
i1
2
14
static void Main() static void Main() {{ int N=0, SUM=0, i=0;int N=0, SUM=0, i=0; Console.Write(”Please input number: ”);Console.Write(”Please input number: ”); N = int.Parse(Console.ReadLine());N = int.Parse(Console.ReadLine());
for ( for ( ??? ??? ; ; ??? ??? ; ; ??? ??? ) )
SUM = SUM + SUM = SUM + ??? ??? ;; Console.WriteLine(”SUM = {0}”, SUM);Console.WriteLine(”SUM = {0}”, SUM);}}
i =1
5i2
= 1*1 + 2*2 + 3*3 + 4*4 + 5*5
Outline
For statements Nested loops Break statements Continue statements
15
Nested Loops : Example16
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 : Example17
for (int outer = 1; outer <= 4; outer++) {
for (int inner = 1; inner <=3; inner++) {
Console.WriteLine("Outer = {1} & Inner={0}”, inner,outer);
}
}
Nested for loops
Do not use the same runner variable in the outer loop and the inner loop
Nested Loops : Example18
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 = 1
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
Output
Nested Loops : Example19
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 while loops
Nested Loops : Example20
int outer = 1;int outer = 1;
do {do {
int inner = 1;int inner = 1;
do {do {
Console.WriteLine("Outer = Console.WriteLine("Outer = {1}{1} & Inner = & Inner = {0}{0}"" , , inner inner ,,outerouter););
inner = inner +1inner = inner +1
} while (inner < 6);} while (inner < 6);
outer = outer + 1;outer = outer + 1;
inner = 1inner = 1
}while (outer < 5);}while (outer < 5);
Nested do...while loops
Test II : Nested Loops : Example Write the program
to Show output
21
112123123412345
int i,j;for( ; ; ){ for( , , ) {
}
}
Break statement
The break statement terminates the clos est enclosing loop or switch statement in
which it appears
22
Break statement example23
int i,j;for(i=1;i<=10;i++){ Console.WriteLine(i); if(i==5) break;}Console.Write(“final i = {0}”,i);
i
1
1Output
2345final i = 5
2345
Continue Statement24
int i=0,sum=0;do{ i++; if(i%2==0) continue; sum = sum+i;}while(i<10);Console.WriteLine("final sum={0}""final sum={0}",sum);
i
0
sum
01 +1
23 +3
+5
+7
+9
final sum = 25
Output
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?