Module4 Looping Techniques: Part 1 - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Module4 Looping Techniques: Part 1

Description:

{ Console.WriteLine('Hello, Kitty'); Console.WriteLine('Hello, Kitty' ... i ; #Hello, Kitty 7 times. How to write 'Hello, Kitty' 2000 times? ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 24
Provided by: pattarale
Category:

less

Transcript and Presenter's Notes

Title: Module4 Looping Techniques: Part 1


1
Module4 Looping Techniques Part 1
Thanawin Rakthanmanon Email fengtwr_at_ku.ac.th Cre
ate by Aphirak Jansang Computer Engineering
Department Kasetsart University, Bangkok THAILAND
2
Review Previous Lecture
Review
  • Boolean expression
  • if statement
  • nested if statement
  • switch case statement
  • Flowchart

3
Flowchart Symbols Overview
Review
  • Graphical representation

Terminator
Process
Input/output
Condition
Connector
Flow line
4
if statement flowchart
Review
START
statement1 if (condition) statement2
//true else statement3 //false
statement4
statement1
CONDITION
false
true
statement2
statement3
statement4
5
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
6
Looping 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
7
Looping or Iteration in C
while
Iteration
foreach
for
(repeat)
dowhile
8
Outline
  • while statement

9
while statement
For Single Statement
while (condition) statement
while (condition) statement-1
statement-2 . . . statement-N
For Multiple Statements
10
How 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
11
How 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
.
.
.
12
How 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?
13
Looping Type
  • Counter Controlled
  • Sentinel (?????????) Controlled

14
Count Controlled Example 1
  • Display 1 5 on screen

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?
15
Count 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?
16
Quiz1 (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)
17
Answer
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
18
Looping Type
  • Counter Controlled
  • Sentinel (?????????) Controlled

19
Sentinel 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?
20
Problem 1 Display 10 stars
static void Main() int N N 0 while (
) Console.Write()
How to display J stars?
21
Problem 2 Display 3x3 stars
static void Main() int N0, M0 while (
) while ( )
Console.Write()
Console.WriteLine()
How to display JxJ stars?
22
Quiz3
  • Write the program flowchart

int N N 0 while ( )
Console.Write()
23
Answer
int N N 0
int N N 0 while (Nlt10)
Console.Write() N
(Nlt10)
Console.Write() N
Write a Comment
User Comments (0)
About PowerShow.com