STD511: C# Programming: Control Structures: Repetition
STD511: C# Programming: Control Structures: Repetition
Control Structures:
Repetition
while Loop
count = 0;
Loop
false false
Continuation (count < 100)?
Condition?
true true
Statement(s) Console.WriteLine("Welcome to C#!");
(loop body) count++;
(A) (B)
while Loop
while (loop-continuation-condition)
{
// loop-body;
Statement(s);
}
int count = 0;
while (count < 100)
{
Console.WriteLine("Welcome to C#!");
count++;
}
Exercise
• s to print the numbers 1
Write a C# program
..100.
5
Ending a Loop with a
Sentinel Value
• Often the number of times a loop is executed is not predetermined.
You may use an input value to signify the end of the loop. Such a
value is known as a sentinel value.
6
do-while Loop
Statement(s)
(loop body)
true Loop
Continuation
do { Condition?
false
// Loop
body;
} while (loop-continuation-condition);
Statement(s);
7
for
Loops
In itia l-A ctio n i= 0
Loop
false false
C o n tin u a t io n (i < 1 0 0 )?
C o n d itio n ?
tru e tru e
S tate m e n t( C o n s o l e . Wr i t e L i n
s) (lo o p
body) e(
" W elco m e
A ctio n - A fter-E a c h -Iteratio n to C # " );i + +
(A ) (B )
for
Loops
for (initial-action; loop-continuation-condition;
action-after-each-iteration)
{
// loop
body;
Statement(s);
}
int i;
for (i = 0; i < 100; i++)
{
Console.WriteLine(
"Welcome to C#!");
}
9
Nested Loops
10
public class TestBreak {
break
public static void Main(String[] args)
{ int sum = 0;
int number = 0;
12
Reference
• C# Programming for Absolute Beginners;
Radek Vystavel.
• https://www.geeksforgeeks.org/csharp-progr
amming-language/
• https://docs.microsoft.com/en-us/dotnet/csh
arp/