Title: Module4 Looping Techniques: Part 1
1Module4 Looping Techniques Part 1
Thanawin Rakthanmanon Email fengtwr_at_ku.ac.th Cre
ate by Aphirak Jansang Computer Engineering
Department Kasetsart University, Bangkok THAILAND
2Review Previous Lecture
Review
- Boolean expression
- if statement
- nested if statement
- switch case statement
- Flowchart
3Flowchart Symbols Overview
Review
Terminator
Process
Input/output
Condition
Connector
Flow line
4if statement flowchart
Review
START
statement1 if (condition) statement2
//true else statement3 //false
statement4
statement1
CONDITION
false
true
statement2
statement3
statement4
5Consider 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
6Looping Version
How to write Hello, Kitty 2000 times?
using System Namespace SimPleLoopExample
class SimLoopPleC static void Main()
int i i 0 while (ilt7)
Console.WriteLine(Hello, Kitty)
i
Hello, Kitty 7 times
7Looping or Iteration in C
while
Iteration
foreach
for
(repeat)
dowhile
8Outline
9while statement
For Single Statement
while (condition) statement
while (condition) statement-1
statement-2 . . . statement-N
For Multiple Statements
10How this partial code works?
static void Main() int N N 0 while
(N lt 5) Console.WriteLine(Kitty-0, N)
N
Kitty-0
Kitty-1
Kitty-2
Kitty-3
Kitty-4
11How this partial code works?
static void Main() int N N 0 while
(N lt 5) Console.WriteLine(Kitty-0, N)
N
Kitty-0
Kitty-0
Kitty-0
Kitty-0
.
.
.
12How this partial code works?
static void Main() int N, SUM SUM 0 N
0 while (N gt 0) Console.Write(Please
input N ) N int.Parse(Console.ReadLine()
) if (Ngt0) SUM SUMN
Console.WriteLine(SUM 0, SUM)
How to exit this loop?
13Looping Type
- Counter Controlled
- Sentinel (?????????) Controlled
14Count Controlled Example 1
static void Main() int N N 1 while (
) Console.WriteLine(0, N)
1
2
3
4
5
How to display number 1 J on screen?
15Count Controlled Example 2
- Display only even number between 1 to N
static void Main() int N, J J
int.Parse(Console.ReadLine()) N 1 while (
) if ( )
Console.WriteLine(0, N)
2
4
6
8
.
.
.
How to display odd number between 1 J on screen?
16Quiz1 (from Lecture3)
- Write the program flowchart
if (x lt 5) fx 2x 10 else if ((5 lt x)
(x lt20)) fx xx 10 else
fx xxx 10 Console.WriteLine(f(x)
0, fx)
17Answer
START
statement1 statement2
if (x lt5)
CONDITION1
false
true
statement3
if (5 lt x lt 20)
fx 2x 10
CONDITION2
false
true
statement4
statement5
fx xx 10
fx xxx 10
statement6
Console.WriteLine("f(x) 0", fx)
END
18Looping Type
- Counter Controlled
- Sentinel (?????????) Controlled
19Sentinel Controlled Example 1 (Quiz2)
- Read Input from user stop when user input
negative number
N
static void Main() int N,SUM N 0 SUM
0 while ( ) Console.Write(Inpu
t N ) N int.Parse(Console.ReadLine())
Console.WriteLine(
)
9
3
2
4
-2
if (Ngt0) SUM SUMN
"SUM 0", SUM
How to find summation of all Positive N?
20Problem 1 Display 10 stars
static void Main() int N N 0 while (
) Console.Write()
How to display J stars?
21Problem 2 Display 3x3 stars
static void Main() int N0, M0 while (
) while ( )
Console.Write()
Console.WriteLine()
How to display JxJ stars?
22Quiz3
- Write the program flowchart
int N N 0 while ( )
Console.Write()
23Answer
int N N 0
int N N 0 while (Nlt10)
Console.Write() N
(Nlt10)
Console.Write() N