iteration (loop) parti thanachat thanomkulabut. consider the following program! using system;...

33
Iteration (Loop) partI Thanachat Thanomkulabut

Upload: jesse-washington

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Iteration (Loop) partIThanachat Thanomkulabut

Consider the following program!

using System;using System;Namespace SimPleExample {Namespace SimPleExample { class SimPleC {class SimPleC { static void Main() {static void Main() {

Console.WriteLine(”Hello, Kitty”);Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”);Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”);Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”);Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”);Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”);Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”);Console.WriteLine(”Hello, Kitty”);

}} }}}}

#Hello, Kitty 7 times#Hello, Kitty 7 times

How to write “Hello, Kitty” 2000 times?How to write “Hello, Kitty” 2000 times?

2

Looping Version

using System;using System;Namespace SimPleLoopExample {Namespace SimPleLoopExample { class SimLoopPleC {class SimLoopPleC { static void Main() {static void Main() { int i;int i;

i = 0;i = 0; while (i<7) {while (i<7) { Console.WriteLine(”Hello, Kitty”);Console.WriteLine(”Hello, Kitty”); i++;i++; }}

}} }}}}

#Hello, Kitty 7 times#Hello, Kitty 7 times

How to write “Hello, Kitty” 2000 times?How to write “Hello, Kitty” 2000 times?

3

Looping or Iteration in C#

foreachforeach

whilewhile

forfor

do…whiledo…whileIterationIteration(repeat)

4

Kinds of Iteration statements5

Outline

while statements Loop controls do...while

6

while statement

while (while (conditioncondition)) statement;statement;

while (while (conditioncondition) {) { statement 1;statement 1; statement 2;statement 2; .. .. .. statement N;statement N;}}

For Multiple StatementsFor Multiple Statements

For Single StatementFor Single Statement

7

Condition

Statement

FALSE

TRUE

while...example

static void Main() {static void Main() { int n = 0;int n = 0; while (n < 3)while (n < 3) { { Console.WriteLine(”Console.WriteLine(”Current value of n: {0}”,n);”,n); n++;n++; } }

}}

n

0

TRUE

Current value of n: 0

Monitor

1

Current value of n: 1

2

Current value of n: 2

3

FALSE

8

How this program works?1. initialize value1. initialize value

2. runner2. runner

3. condition3. condition

while...example

static void Main() {static void Main() { int n = 0;int n = 0; while (n < 3)while (n < 3) Console.WriteLine(”Console.WriteLine(”Current value of n:{{0}”, n);0}”, n); n++;n++;

}}

n

0

TRUE

Current value of n: 0

Monitor

Current value of n: 0

Current value of n: 0

{{

}}Infinite Loop

9

How this program works?1. initialize value1. initialize value

3. condition3. condition

2. runner2. runner

while...example10

static void Main() {static void Main() { int N, SUM;int N, SUM; SUM = 0;SUM = 0; N = 0;N = 0;

while (N >= 0) {while (N >= 0) { Console.Write(”Please input N: ”);Console.Write(”Please input N: ”); N = int.Parse(Console.ReadLine());N = int.Parse(Console.ReadLine()); if (N>=0)if (N>=0) SUM = SUM+N;SUM = SUM+N; }}

Console.WriteLine(“SUM = {0}”, SUM);Console.WriteLine(“SUM = {0}”, SUM);}}

N

0

SUM

0

TRUE

Please input N: 5

Monitor

5

TRUE

5 Please input N: 33 8

Please input N: -1

-1

FALSE

FALSESUM = 8

1. initialize value1. initialize value

2. runner2. runner

3. condition3. condition

while...example11

static void Main(){

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 x fact

Enter value of n :

Monitor

4

4 43

TRUE

*32 *21 *10

FALSE

4! = 24

1. initialize value1. initialize value

2. runner2. runner

3. finish condition3. finish condition

Test I12

Write a program which Input : n (positive integer number) Output : summation of 1+2+3+...+n

Test I13

static void Main(){

 

}

Console.Write(“Enter value of n : ”);n = int.Parse(Console.ReadLine());

int n

;,x=1;,sum=0;

while(x<=n){

}

sum = sum+x;x++;

Console.Write(“sum = {0}”,sum);

While Statements14

Since the condition is checked before the execution of the loop body

While loop is also called a pre-conditional loop.

As long as the entry condition is still true, the while loop will be repeated

Zero loop will be executed if the entry condition is false at the beginning

while statements are conditional iteration statements which execute the loop body only if the entry condition is true

Outline

while statements Loop controls do...while

15

Loop Controls16

Counter Controlled17

static void Main() {static void Main() { int N;int N; N = 1;N = 1; while ( ) while ( ) {{ Console.WriteLine(”{0}”, N);Console.WriteLine(”{0}”, N);

}}}}

N <= 5N <= 5

N++;N++;

N

1

TRUE

1Monitor

2 23

3

4

4

5

5

6

FALSE

How to display even numbers 1 to J on screen?How to display even numbers 1 to J on screen?

Counter Controlled18

Display only even numbers between 1 to J

static void Main() {static void Main() { int N, J;int N, J; J = int.Parse(Console.ReadLine());J = int.Parse(Console.ReadLine()); N = 1;N = 1; while ( ) {while ( ) { if ( )if ( ) Console.WriteLine(”{0}”, N);Console.WriteLine(”{0}”, N);

}}}}

N <= JN <= J

N++;N++;

22446688

..

How to display odd numbers between 1 to J on screen?How to display odd numbers between 1 to J on screen?

N%2==0N%2==0 ....

Loop Controls19

Sentinel-Controlled20

static void Main() {static void Main() { int N, SUM;int N, SUM; SUM = 0;SUM = 0; N = 0;N = 0;

while (N >= 0) {while (N >= 0) { Console.Write(”Please input N: ”);Console.Write(”Please input N: ”); N = int.Parse(Console.ReadLine());N = int.Parse(Console.ReadLine()); if (N>=0)if (N>=0) SUM = SUM+N;SUM = SUM+N; }}

Console.WriteLine(“SUM = {0}”, SUM);Console.WriteLine(“SUM = {0}”, SUM);}}

N

0

SUM

0

TRUE

Please input N: 5

Monitor

5

TRUE

5 Please input N: 33 8

Please input N: -1

-1

FALSE

FALSESUM = 8

Outline

while statements Loop controls do...while

21

Kinds of Iteration statements22

do...while statement23

do{do{ statement1;statement1; statement2;statement2; statement3;statement3;}while (}while (conditioncondition););

conditiontrue

false

Statement3

Statement2

Statement1

do...while example24

static void Main() {static void Main() {

int N = 0;int N = 0;

do{ do{

Console.WriteLine(N);Console.WriteLine(N);

N++;N++;

}while (N < 3); }while (N < 3);

}}

How this program works? N

0 0Monitor

1

TRUE

12

2

3

FALSE

do...while example25

static void Main() {static void Main() {

string input;string input;

do{ do{

Console.Write("Input your Console.Write("Input your

password:");password:");

input = Console.ReadLine();input = Console.ReadLine();

}while(input != "ABCD");}while(input != "ABCD");

Console.WriteLine("Password Console.WriteLine("Password

Correct");Correct");

}}

How this program works?input

Input your password : Kaset

Kaset

TRUE

Input your password : bkk

bkkInput your password : ABCD

ABCD

FALSE

Password Correct

do...while example26

int n,x=1;int n,x=1;

Console.Write("Input:"); Console.Write("Input:");

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

Console.WriteLine("Factor of {0} Console.WriteLine("Factor of {0}

is",n);is",n);

do{do{

if(n%x==0)if(n%x==0)

Console.WriteLine(x);Console.WriteLine(x);

x++;x++;

}while(x<=n); }while(x<=n);

How this partial code works?

FALSETRUE

n x

1Input :

Monitor

6

Factor of 6 is6

TRUE

1

2

2

3

3

4

FALSE

56

6

7

Test II27

Write a program Input : n (negative integer number) Output : summation of (-1)+(-2)+(-3)

+...+n

Test II28

static void Main(){

 

}

Console.Write(“Enter value of n : ”);n = int.Parse(Console.ReadLine());

int n

;,x=-1

;,sum=0;

do{

}while(x>=n);

sum = sum+x;x--;

Console.Write(“sum = {0}”,sum);

Do..while Statement29

The loop body of a do..while statement will be executed first before the exit condition is checked

Do..while loop is also called a post-conditional loop because the condition is checked after the loop body

Do..while statement is a conditional iteration statements

A do..while statement will be executed at least once

30

Do..while vs. While

false

true

Statementcondition

true

false

Statement

Statement

Statement

Do..while statement While statement

Statement

Statement

condition

while & do..while more example31

static void Main(){ int number, right_digit;  Console.Write("Enter your number : "); number = int.Parse(Console.ReadLine()); Console.Write("Reverse number is: "); while (number != 0) {

right_digit = number%10;Console.Write("{0}",right_digit);number = number/10;

} }

This program will reverse integer input

Example input : 1502 output : 2051

number right_digit

1502

FALSETRUE

215015 0510

Monitor2202052051

1

Reverse number is:

while & do..while more example32

This program shows odd n numbers starting from 1

Example input : 5 output :

1 3 5 7 9

static void Main(){ int n, count, k;  Console.Write ("Please enter number of odd numbers: "); n = int.Parse(Console.ReadLine()); k = 1; count = 1; do{

Console.Write("{0} ",k);k = k+2;count = count+1;

}while (count<=n); }

k count

Monitor

1 1 TRUEFALSE3579

1

2

3

3

5

4

7

5

9

11 6

n

5

Any question?