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

Download Report

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

Iteration
(Loop) partI
Thanachat Thanomkulabut
Consider the following program!
2
How to write “Hello, Kitty” 2000 times?
using System;
Namespace SimPleExample {
class SimPleC {
static void Main() {
Console.WriteLine(”Hello,
Console.WriteLine(”Hello,
Console.WriteLine(”Hello,
Console.WriteLine(”Hello,
Console.WriteLine(”Hello,
Console.WriteLine(”Hello,
Console.WriteLine(”Hello,
}
}
}
#Hello, Kitty 7 times
Kitty”);
Kitty”);
Kitty”);
Kitty”);
Kitty”);
Kitty”);
Kitty”);
Looping Version
3
How to write “Hello, Kitty” 2000 times?
using System;
Namespace SimPleLoopExample {
class SimLoopPleC {
static void Main() {
int i;
#Hello, Kitty 7 times
i = 0;
while (i<7) {
Console.WriteLine(”Hello, Kitty”);
i++;
}
}
}
}
Looping or Iteration in C#
4
while
foreach
Iteration
(repeat)
for
do…while
Kinds of Iteration statements
5
Iteration
Conditional
iteration
while
do...while
Definite
iteration
For
Outline
6



while statements
Loop controls
do...while
while statement
7
For Single Statement
Condition
TRUE
Statement
FALSE
while (condition)
statement;
while (condition) {
statement 1;
statement 2;
.
.
.
statement N;
}
For Multiple Statements
while...example
8
1. initialize
value
How this program
works?
n
static void Main() {
1
2
3
0
int n = 0;
FALSE 3. condition
while (n < 3)
TRUE
{
Console.WriteLine(”Current value of n: {0}”,n);
n++;
}
}
2. runner
Monitor
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
9
How this 1.
program
works?
initialize value
Current value of n: 0
n
static void Main() {
0
int n = 0;
3. condition
while (n < 3) TRUE
{
Console.WriteLine(”Current value of n:{0}”, n);
n++;
}
}
2. runner
Infinite
Loop
while...example
10
N
SUM
static void Main() {
0
5
-1
0
5
3
8
int N, SUM;
SUM = 0;
1. initialize value
TRUE
N = 0;
FALSE
Monitor
Please input N: 5
Please input N: 3
Please input N: -1
SUM = 8
3. condition
while (N >= 0) {
Console.Write(”Please input N: ”);
N = int.Parse(Console.ReadLine());
if (N>=0) FALSE
TRUE
SUM = SUM+N;
2. runner
}
Console.WriteLine(“SUM = {0}”, SUM);
}
while...example
11
n
x
fact
static void Main()
4 *3*2*1
4
3
2
1
0
{
int n,x,fact;
Console.Write(“Enter value of n : “);
n = int.Parse(Console.ReadLine());
Monitor
fact = n;
1. initialize value
Enter value of n :4
x = n-1; FALSE
while (x >= 1) TRUE3. finish condition
4! = 24
{
fact = fact*x;
x = x-1;
2. runner
}
Console.WriteLine(“{0}! = {1}”,n,fact);
}
Test I
12



Write a program which
Input : n (positive integer number)
Output : summation of 1+2+3+...+n
Test I
13
static void Main()
{
int n ;,x=1 ;,sum=0;
Console.Write(“Enter value of n : ”);
n = int.Parse(Console.ReadLine());
while(x<=n)
{
sum = sum+x;
x++;
}
Console.Write(“sum = {0}”,sum);
}
While Statements
14





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
15



while statements
Loop controls
do...while
Loop Controls
16
Loop
Controls
Counter
Controlled
Sentinel
Controlled
Counter Controlled
17
N
6
2
3
4
5
1
Monitor
1
2
3
4
5
static void Main() {
int N;
FALSE
N = 1;
while ( N <= 5 ) TRUE
{
Console.WriteLine(”{0}”, N);
N++;
}
}
How to display even numbers 1 to J on screen?
Counter Controlled
18

Display only even numbers between 1 to J
static void Main() {
int N, J;
J = int.Parse(Console.ReadLine());
N = 1;
while ( N <= J ) {
if ( N%2==0 )
Console.WriteLine(”{0}”, N);
2
4
6
8
..
.
N++;
}
}
How to display odd numbers between 1 to J on screen?
Loop Controls
19
Loop
Controls
Counter
Controlled
Sentinel
Controlled
Sentinel-Controlled
N
SUM
Monitor
Please input N: 5
-1
0
5
3
0
5
8
Please input N: 3
20
static void Main() {
int N, SUM;
SUM = 0;
TRUE
N = 0;
FALSE
Please input N:-1
SUM = 8
while (N >= 0) {
Console.Write(”Please input N: ”);
N = int.Parse(Console.ReadLine());
if (N>=0) FALSE
TRUE
SUM = SUM+N;
}
Console.WriteLine(“SUM = {0}”, SUM);
}
Outline
21



while statements
Loop controls
do...while
Kinds of Iteration statements
22
Iteration
Conditional
iteration
while
do...while
Definite
iteration
For
do...while statement
23
do{
statement1;
statement2;
statement3;
}while (condition);
Statement1
Statement2
Statement3
true
condition
false
do...while example
24
How this program works? N
static void Main() {
int N = 0;
do{
Console.WriteLine(N);
N++; FALSE
TRUE
}while (N < 3);
}
0
1
2
3
Monitor
0
1
2
do...while example
25
Input your password : Kaset
How this program works?
input
Input your password : bkk
static void Main() {
Input your password : ABCD
bkk
Kaset
ABCD
string input;
Password Correct
do{
Console.Write("Input your password:");
input = Console.ReadLine();
}while(input != "ABCD"); TRUE
FALSE
Console.WriteLine("Password Correct");
}
do...while example
26
How this partial code works?
n
6
x
int n,x=1;
6
7
1
2
3
4
5
Console.Write("Input:");
n = int.Parse(Console.ReadLine());
Console.WriteLine("Factor of {0} is",n);
do{
FALSE
if(n%x==0) TRUE
Console.WriteLine(x);
x++; TRUE
}while(x<=n); FALSE
Monitor
Input :6
Factor of 6 is
1
2
3
6
Test II
27



Write a program
Input : n (negative integer number)
Output : summation of (-1)+(-2)+(-3)
+...+n
Test II
28
static void Main()
{
int n ;,x=-1 ;,sum=0;
Console.Write(“Enter value of n : ”);
n = int.Parse(Console.ReadLine());
do{
sum = sum+x;
x--;
}while(x>=n);
Console.Write(“sum = {0}”,sum);
}
Do..while Statement
29




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
Do..while vs. While
30
Do..while statement
Statement
condition
Statement
true
Statement
Statement
true
While statement
condition
false
Statement
Statement
false
while & do..while more example
31


This program
will reverse
integer input
Example
input : 1502
 output : 2051

number
right_digit
1
1502
150
15
0
2
5
0
1
Monitor
Reverse number is:
205
2
2051
0
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) FALSE
TRUE
{
right_digit = number%10;
Console.Write("{0}",right_digit);
number = number/10;
}
}
while & do..while more example
32


This program
shows odd
n numbers
starting from 1
Example
 input : 5
 output :
13579
n
k
count
5
11
1
9
7
5
3
1
2
3
4
5
6
Monitor
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);
TRUE
FALSE
}
Any question?