# Iteration (Loop) partI - PowerPoint PPT Presentation

1 / 33

Iteration (Loop) partI. Thanachat Thanomkulabut. Consider the following program!. How to write “Hello, Kitty” 2000 times?. using System; Namespace SimPleExample { class SimPleC { static void Main() { Console.WriteLine(”Hello, Kitty”); Console.WriteLine(”Hello, Kitty”);

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Iteration (Loop) partI

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

## Iteration (Loop) partI

Thanachat Thanomkulabut

### Consider the following program!

How to write “Hello, Kitty” 2000 times?

using System;

Namespace SimPleExample {

class SimPleC {

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”);

}

}

}

#Hello, Kitty 7 times

### Looping Version

How to write “Hello, Kitty” 2000 times?

using System;

Namespace SimPleLoopExample {

class SimLoopPleC {

static void Main() {

int i;

i = 0;

while (i<7) {

Console.WriteLine(”Hello, Kitty”);

i++;

}

}

}

}

#Hello, Kitty 7 times

Iteration

(repeat)

while

do…while

foreach

for

### Outline

• while statements

• Loop controls

• do...while

### while statement

For Single Statement

while (condition)

statement;

while (condition) {

statement 1;

statement 2;

.

.

.

statement N;

}

FALSE

Condition

TRUE

Statement

For Multiple Statements

### while...example

1. initialize value

How this program works?

n

static void Main() {

intn = 0;

while (n < 3)

{

Console.WriteLine(”Current value of n: {0}”,n);

n++; }

}

3

1

2

0

3. condition

FALSE

TRUE

Monitor

2. runner

Current value of n: 0

Current value of n: 1

Current value of n: 2

### while...example

Monitor

Current value of n: 0

Current value of n: 0

How this program works?

1. initialize value

Current value of n: 0

n

static void Main() {

intn = 0;

while (n < 3)

Console.WriteLine(”Current value of n:{0}”, n);

n++;

}

0

3. condition

TRUE

{

Infinite Loop

}

2. runner

### while...example

Monitor

SUM

N

5

static void Main() {

int N, SUM;

SUM = 0;

N = 0;

while (N >= 0) {

if (N>=0)

SUM = SUM+N;

}

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

}

5

0

-1

3

0

5

8

3

-1

1. initialize value

TRUE

SUM = 8

FALSE

3. condition

FALSE

TRUE

2. runner

### while...example

static void Main()

{

int n,x,fact;

Console.Write(“Enter value of n : “);

fact = n;

x = n-1;

while (x >= 1)

{

fact = fact*x;

x = x-1;

}

Console.WriteLine(“{0}! = {1}”,n,fact);

}

n

x

fact

4

4

0

3

2

1

*3

*2

*1

Monitor

1. initialize value

Enter value of n :

4

FALSE

TRUE

3. finish condition

4! = 24

2. runner

### Test I

• Write a program which

• Input : n (positive integer number)

• Output : summation of 1+2+3+...+n

### Test I

static void Main()

{

}

int n

;

,x=1

;

,sum=0;

Console.Write(“Enter value of n : ”);

while(x<=n)

{

}

sum = sum+x;

x++;

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

### While Statements

• 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

N <= 5

N++;

### Counter Controlled

Monitor

N

1

2

4

3

6

5

1

2

static void Main() {

int N;

N = 1;

while ( )

{

Console.WriteLine(”{0}”, N);

}

}

3

FALSE

4

TRUE

5

How to display even numbers 1 to J on screen?

N <= J

N%2==0

N++;

### Counter Controlled

• Display only even numbers between 1 to J

static void Main() {

int N, J;

N = 1;

while ( ) {

if ( )

Console.WriteLine(”{0}”, N);

}

}

2

4

6

8

.

.

.

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

### Sentinel-Controlled

Monitor

SUM

N

5

static void Main() {

int N, SUM;

SUM = 0;

N = 0;

while (N >= 0) {

if (N>=0)

SUM = SUM+N;

}

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

}

0

5

0

5

3

-1

8

3

-1

TRUE

SUM = 8

FALSE

FALSE

TRUE

### Outline

• while statements

• Loop controls

• do...while

Statement3

Statement2

Statement1

### do...while statement

do{

statement1;

statement2;

statement3;

}while (condition);

true

condition

false

### do...while example

How this program works?

N

Monitor

0

1

2

3

0

static void Main() {

int N = 0;

do{

Console.WriteLine(N);

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

}

1

2

FALSE

TRUE

### do...while example

Kaset

How this program works?

bkk

input

static void Main() {

string input; do{

}while(input != "ABCD");

ABCD

bkk

Kaset

ABCD

TRUE

FALSE

### do...while example

How this partial code works?

Monitor

n

x

Input :

6

int n,x=1; Console.Write("Input:"); n = int.Parse(Console.ReadLine());

Console.WriteLine("Factor of {0} is",n);

do{

if(n%x==0)

Console.WriteLine(x);

x++;

}while(x<=n);

6

6

7

1

5

3

2

4

Factor of 6 is

1

2

3

FALSE

TRUE

6

TRUE

FALSE

### Test II

• Write a program

• Input : n (negative integer number)

• Output : summation of (-1)+(-2)+(-3) +...+n

### Test II

static void Main()

{

}

int n

;

,x=-1

;

,sum=0;

Console.Write(“Enter value of n : ”);

do{

}while(x>=n);

sum = sum+x;

x--;

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

### Do..while Statement

• 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

Statement

Statement

Statement

### Do..while vs. While

Do..while statement

While statement

false

condition

true

Statement

Statement

true

condition

Statement

false

### while & do..while more example

• This program will reverse integer input

• Example

• input : 1502

• output : 2051

static void Main()

{int number, right_digit;

Console.Write("Reverse number is: ");

while (number != 0)

{right_digit = number%10;

Console.Write("{0}",right_digit);

number = number/10;

}

}

TRUE

FALSE

number

right_digit

1

5

0

2

1

0

15

1502

150

Monitor

Reverse number is:

2051

2

205

20

### while & do..while more example

• 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: ");

k = 1; count = 1;

do{

Console.Write("{0} ",k);

k = k+2;

count = count+1;

}while (count<=n);

}

n

k

count

TRUE

FALSE

5

5

9

3

11

1

7

2

1

4

5

6

3

Monitor

1

3

5

9

7