Iteration (Loop) partI

1 / 33

# Iteration (Loop) partI - PowerPoint PPT Presentation

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 - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 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)

Looping or Iteration in C#

while

do…while

foreach

for

Outline
• while statements
• Loop controlled
• 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() {

int N = 0;

while (N < 3)

{

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

N++; }

}

3

1

2

0

3. finish condition

False

TRUE

Monitor

2. runner

Kitty-0

Kitty-1

Kitty-2

while...example

Monitor

Kitty-0

Kitty-0

How this program works?

1. initialize value

Kitty-0

N

static void Main() {

int N = 0;

while (N < 3)

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

N++;

}

0

3. finish condition

TRUE

{

Loop infinite

}

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. finish 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 the program which
• Input : n (positive interger number)
• Output : sumation 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 controlled
• 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 number 1 to J on screen?

N <= J

N%2==0

N++;

Counter Controlled
• Display only even number 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 number 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 controlled
• 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

7

1

6

5

4

3

2

Factor of 6 is

1

2

3

FALSE

TRUE

6

TRUE

FALSE

Test II
• Write the program
• Input : n (negative interger number)
• Output : sumation 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 execute 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;

}

}

while & do..while more example

static void Main()

{ int n, count, k;

Console.Write ("Please enter number of n : ");

k = 1; count = 1;

do{

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

k = k+2;

count = count+1;

}while (count<=n);

}

• This program will show odd number n numbers
• Example
• input : 5
• output : 1 3 5 7 9