Control Structures of Repetition
The ability to specify the repetition of a group of operations is an important programming capability. For example, if we want to calculate the GPA for 5000 students, we can write a single set of instructions which perform this calculation repeatedly them 5000 times.
The repetition of steps in a program is called a loop. The loop body contains the steps to be repeated. C# provides three control statements for specifying repetition :
Do..While Statement
While Statement
For Statement
Do..While Statement
do {
statement 1;
statement 2;
……………..;
……………..;
} while(Boolean expression);
An example 1 of do..while execution
static void Main(string[] args)
{
int count = 1; /* initialize count
do
{
Console.Write("{0} ",count);
count++;
}while (count <= 5);
}
Example 2 : To display Hello World !! on 3 lines
static void Main(string[] args)
{
int line = 0; /* initialized value of line */
do
{
Console.WriteLine("Hello World !!");
++line;
}while (line < 3);
}
Example 3 : To display 1 + 2 + …….. + n For example: if n = 10, 1 + 2 + ….. + 10 is 55 and the final of k is 11.
static void Main(string[] args)
{
int n, k = 1, sum = 0; /* initialized value of k and sum */
Console.Write("Please enter n : ");
n = int.Parse(Console.ReadLine());
do
{
sum = sum+k;
k = k+1;
} while (k<=n);
Console.WriteLine("The value of sum = {0}",sum);
Console.WriteLine("The value of {0} ",k);
}
Example 4 : To change the position from left to right. For example:
Input: 1500
Output: 0051
static void Main(string[] args)
{
int number, right_digit;
Console.Write("Enter your number : ");
number = int.Parse(Console.ReadLine());
Console.Write("Reverse number is : ");
while (number != 0)
{
right_digit = number%10;
Console.Write("{0}",right_digit);
number = number/10;
}
Console.WriteLine();
}
Example 5 : This program displays n odd numbers. For example, if 5 is read to n, the program will display 1 3 5 7 9
static void Main(string[] args)
{
int n, count, k;
Console.Write("Please enter number of n : ");
n = int.Parse(Console.ReadLine());
k = 1; count = 1; /* initialized value of k and count */
do
{
Console.Write("{0} ",k);
k = k+2;
count = count+1;
}while (count<=n);
Console.WriteLine();
}
Example 6 : To find factorial of n, For example 5! = 120
static void Main(string[] args)
{
int n,x,fact;
Console.Write("Enter value of n : ");
n = int.Parse(Console.ReadLine());
fact = n; /* initialized value of fact and x */
x = n-1;
do
{
fact = fact*x;
x = x-1;
} while(x>=1);
Console.WriteLine("{0}! = {1}",n,fact);
}
Example 7 : To find Greatest Common Divisor (GCD) of 2 integer numbers. Fox example: GCD of 54 and 27 is 27 and GCD of 18 and 27 is 9
static void Main(string[] args)
{
int temp, x, y;
Console.Write(“Enter the first integer numbers : “);
int a = int.Parse(Console.ReadLine());
Console.Write(“Enter the first integer numbers : “);
int b = int.Parse(Console.ReadLine());
x = a; y = b;
if (a<b)
{
temp = a;
a = b;
b = temp;
}
if (a%b == 0)
Console.WriteLine(“GCD of {0} and {1} = {2}”,x,y,b);
else
{
do
{
temp = a;
a = b;
b = temp%b;
}while((a%b) != 0);
Console.WriteLine(“GCD of {0} and {1} = {2}”,x,y,b);
}
}
Example 8 :
static void Main(string[] args)
{
bool valid;
double curr_bal, amount = 0; /* initialized value of amount */
curr_bal = 85000.00; /* initialized value of curr_bal */
valid = true;
curr_bal = curr_bal – amount;
do
{
Console.WriteLine(“Current balance = {0}”,curr_bal);
Console.WriteLine(“Enter amount you want to withdraw : “);
amount = double.Parse(Console.ReadLine());
curr_bal = curr_bal – amount;
if (curr_bal < 500)
{
valid = false;
Console.Write(“If you withdraw this amount, “);
Console.WriteLine(“yours balance will be {0}”,curr_bal);
Console.WriteLine(“which is lower than 500”);
Console.WriteLine(“you can not withdraw this amount”);
curr_bal = curr_bal + amount;
}
}while (valid);
Console.WriteLine(curr_bal);
}
Example 9 : To accumulate the input data until -1 is enter, program stop its execution and display output.
static void Main(string[] args)
{
int score, total = 0; /* initialized value of total
Console.Write(“Enter score (or enter -1 to stop) : “);
score = Convert.ToInt32(Console.ReadLine());
do
{
total = total + score;
Console.Write (“Enter score (or enter -1 to stop) : “);
score = Convert.ToInt32(Console.ReadLine());
} while (score != -1);
Console.WriteLine(“Summation of numbers = “ + total);
}
Value initialization
· A local variable is not automatically initialized and thus has no default value.
(When a variable is referenced without its initialized value, do not think that 0 will be assigned to that variable automatically.)
· Lifetime of a local variable is into the block where variable is declared, and disappear after that block is achieved.
· Duplicate name of variables declaration is not allow in the same block.
Sentinel-Controlled Loops
Sentinel คือค่าที่ถูกใช้เพื่อหยุดการทำงานของ Loop เช่นในตัวอย่างที่ 9 ค่า Sentinel คือ -1
do
{
total = total + score;
Console.Write (“Enter score (or enter -1 to stop) : “);
score = Convert.ToInt32(Console.ReadLine());
} while (score != -1);
Do..while Statement : REMARK
· Do..while statement is a conditional iteration statements.
· The loop body within 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 execution of the statements in the loop.
· Statement(s) within do..while must always be executed at least once.
While Loop
while (oolean expression) do
…………………………………..;
หรือ
while (oolean expression) do
{
statement 1;
statement 2;
……………..;
}
Example 1 :
static void Main(string[] args)
{
int count = 1;
while (count <= 5)
{
Console.Write(“{0} “,count);
count++;
}
}
Example 2 : To display Hello World !! on 3 lines.
static void Main(string[] args)
{
int line = 0;
while (line < 3)
{
Console.WriteLine(“Hello World !!”);
++line;
}
}
Example 3 : To display 1 + 2 + …….. + n For example: if n = 10, 1 + 2 + ….. + 10 is 55 and the final of k is 11.
static void Main(string[] args)
{
int n, k = 1, sum = 0; /* initialized value of k and sum */
Console.Write(“Please enter n : “);
n = int.Parse(Console.ReadLine());
while (k<=n)
{
sum = sum+k;
k = k+1;
}
Console.WriteLine(“The value of sum = {0}”,sum);
Console.WriteLine(“The value of {0} “,k);
}
Example 4 : To change the position from left to right. For example:
Input: 1500
Output: 0051
static void Main(string[] args)
{
int number, right_digit;
Console.Write(“Enter your number : “);
number = int.Parse(Console.ReadLine());
Console.Write(“Reverse number is : “);
while (number != 0)
{
right_digit = number%10;
Console.Write(“{0}”,right_digit);
number = number/10;
}
Console.WriteLine();
}
Example 5 : This program displays n odd numbers. For example, if 5 is read to n, the program will display 1 3 5 7 9
static void Main(string[] args)
{
int n, count, k;
Console.Write(“Please enter number of n : “);
n = int.Parse(Console.ReadLine());
k = 1; count = 1; /* initialized value of k and count */
while (count <= n)
{
Console.Write(“{0} “,k);
k = k+2;
count = count+1;
}
Console.WriteLine();
}
Example 6 : To find factorial of n, For example 5! = 120
static void Main(string[] args)
{
int n,x,fact;
Console.Write(“Enter value of n : “);
n = int.Parse(Console.ReadLine());
fact = n; /* initialized value of fact and x */
x = n-1;
while (x >= 1)
{
fact = fact*x;
x = x-1;
}
Console.WriteLine(“{0}! = {1}”,n,fact);
}
Example 7 : To find Greatest Common Divisor (GCD) of 2 integer numbers. Fox example: GCD of 54 and 27 is 27 and GCD of 18 and 27 is 9
static void Main(string[] args)
{
int temp, x, y;
Console.Write(“Enter the first integer numbers : “);
int a = int.Parse(Console.ReadLine());
Console.Write(“Enter the first integer numbers : “);
int b = int.Parse(Console.ReadLine());
x = a; y = b;
if (a<b)
{
temp = a;
a = b;
b = temp;
}
if (a%b == 0)
Console.WriteLine(“GCD of {0} and {1} = {2}”,x,y,b);
else
{
while (a%b != 0)
{
temp = a;
a = b;
b = temp%b;
}
Console.WriteLine(“GCD of {0} and {1} = {2}”,x,y,b);
}
}
Example 8 :
static void Main(string[] args)
{
bool valid;
double curr_bal, amount = 0; /* initialized value of amount */
curr_bal = 85000.00; /* initialized value of curr_bal */
valid = true;
curr_bal = curr_bal – amount;
while (valid)
{
Console.WriteLine(“Current balance = {0}”,curr_bal);
Console.WriteLine(“Enter amount you want to withdraw : “);
amount = double.Parse(Console.ReadLine());
curr_bal = curr_bal – amount;
if (curr_bal < 500)
{
valid = false;
Console.Write(“If you withdraw this amount, “);
Console.WriteLine(“yours balance will be {0}”,curr_bal);
Console.WriteLine(“which is lower than 500”);
Console.WriteLine(“you can not withdraw this amount”);
curr_bal = curr_bal + amount;
}
}
Console.WriteLine(curr_bal);
}
Example 9 :
Write the program to calculate Cable Car Velocity. Assume that the length of cable line is 1000 feet from tower 1 to tower 3 as shown below
Tower 1 Tower 2 Tower 3
0 foot 500 feet 1000 feet
Suppose that the velocity of Cable car depends on its current position on cable line. That is,if the cable car is 30 feet from each tower, velocity of the cable car is calculated according to the 1st formula
Velocity = 2.425 + 0.00175d2 ft/sec ……. 1
Otherwise, it is calculated by the 2nd formula:
Velocity = 0.625 + 0.12d – 0.00025d2 ft/sec ……2
Note: d is the distance of Cable car and the nearest tower.
Write the program to report the velocity of Cable car in every ten feet and the nearest tower.
static void Main(string[] args)
{
double velocity;
int tower, distance, nearest;
distance = 0; /* initialized value of distance */
Console.WriteLine(“Distance Nearest Tower Velocity(ft/sec)”);
while (distance <= 1000)
{
if ((distance >= 0)&& (distance <= 250))
{
tower = 1;
nearest = distance;
}
else if (distance <= 750)
{
tower = 2;
nearest = Math.Abs(distance-500);
}
else
{
tower = 3;
nearest = 1000-distance;
}
if (nearest <= 30)
velocity = 2.425 + 0.00175 * nearest * nearest;
else
velocity = 0.625 + 0.12 * nearest * nearest;Console.WriteLine(“ {0} {1}
{2}”,distance,tower, velocity);
distance = distance + 10;
}
}
Example 10 : To sum the input data until -1 is enter, program stop its execution and display output.
static void Main(string[] args)
{
int score, total = 0; /* initialized value of total */
Console.Write(“Enter score (or enter -1 to stop) : “);
score = Convert.ToInt32(Console.ReadLine());
while (score != -1)
{
total = total + score;
Console.Write (“Enter score (or enter -1 to stop) : “);
score = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine(“Summation of numbers = “ + total);
}
10