# for statement

Shared by:
Categories
Tags
-
Stats
views:
1
posted:
2/2/2013
language:
Unknown
pages:
18
Document Sample

```							Looping Techniques:
Part 2

1
Looping or Iteration in C#
while

for      Iteration     foreach

do…while

2
Outline
   do…while statement
   for statement

3
while statement

while (condition)
statement;

4
do…while statement
For Single Statement    do {
statement;
} while (condition);
do
{
statement-1;
statement-2;
.
.
statement-N;
For Multiple Statements } while (condition);
5
Sentinel Loops: example
static void Main() {
int N, SUM;
SUM = 0;
N = 0;                                 1st Time
Console.Write(”Enter number or -1 to quit”);

while (N != -1) {
SUM = SUM+N;
Console.Write(”Enter number or -1 to quit”);
}

Console.WriteLine(“The sum is {0}.”, SUM);
}

6
Sentinel Loops: do…while Version
static void Main() {
int N, SUM;
SUM = 0;
N = 0;

do
{
Console.Write(”Enter number or -1 to quit”);
if (N!= -1)
SUM = SUM+N;
} while (N != -1);

Console.WriteLine(”The sum is {0}.”, SUM);
}

7
Factorial Number: C# Program
static void Main() {
int N, FACT;
FACT = 1;
Find N!
while (N>0)
{
FACT = FACT * N;
N--;
};

Console.WriteLine(”The factorial is {0}.”,FACT);
}

8
Outline
   do…while statement
   for statement

9
for statement
For Single Statement
for ( [initializers]; [expression]; [iterators] )
statement;

For Multiple Statements
for ( [initializers]; [expression]; [iterators] ) {
statement-1;
statement-2;
.
.
statement-N;
}
10
for statement
START

initializers

for ( [initializers]; [expression]; [iterators] )                  false
statement;                                       expression

true
Statement

iterators

END
11
Display 1 to 5 on screen
static void Main() {
int i;
i = 1;
while (i <= 5) {                   while version
Console.WriteLine(“{0}”, i);
i++;
}
}

static void Main() {
int i;
for (i=1; i<=5; i++) {               for version
Console.WriteLine(“{0}”, i);
}
}
12
N
Problem #1: Find            i
i 1
2

static void Main() {
int N=0, SUM=0, i=0;

for ( i = 1   ; i <= N   ;   i++          )
SUM = SUM + i*i;

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

}

13
Problem #2 – Display 10 stars
**********
static void Main() {
int i=0;

for ( i = 0   ; i < 10    ;   i++   )
Console.Write(”*”);

}

How to display N stars?
14
Problem #3 – Display 3x3 stars
*** static void Main() {
***   int N=0, M=0;
***
for ( M = 0   ; M<3   ;   M++   ) {

for ( N = 0   N<3     N++   ) {
Console.Write(”*”);
}
Console.WriteLine();

}

}
How to display NxN stars?
15
Problem #4 – Triangle shape#1
*** static void Main() {
**    int N=0, M=0;
*
for ( M = 3   ; M>0   ;   M--   ) {

for ( N = 0 ; N < M ; N++   ) {
Console.Write(”*”);
}
Console.WriteLine();

}

}

16
Problem #5: Find         2n

static void Main() {
int N=0, SUM=1, i=0;

for ( i = 1   ; i <= N   ;   i++   )
SUM = SUM * 2;

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

}

17
Questions?

18

```
Related docs
Other docs by shitingting
Oklahoma
pg_0013
Weekly Currencies Overview 8212005
Chattot_4thMIT_1_
ihale ile ilgili döküman
Parks NC
APEX 2008 - S1P1 Allan Dawson
2012-13 FA Checklist-Fleer
F062275