This document is a lesson on repetition structures (loops) in C# for beginners. It introduces while, for, and do-while loops. The while loop executes a block of code as long as a condition is true. The for loop is preferred when the number of iterations is known. The do-while loop checks the condition after executing the statements, so the body is executed at least once. Examples are provided for each loop type. The document ends with assignments for students to write looping programs.
1. C# FOR BEGINNERS
LESSON 7
MICHEAL OGUNDERO
CONTACT :
EMAIL – OGUNDEROAYODEJI@GMAIL.COM
Control Structures II: Repitition Structures
2. CHECKER!
Just before we go into today’s business
Write a program takes as inputs the name of shapes and its
parameters (Triangle, Circle and Square) then calculate the area
of the selected shape and print.
Hint: If user selects ’’Circle’’ it should ask for it’s radius
3. CHECKER!
Just before we go into today’s business
Write a program takes as inputs:
a, b and c that form a quadratic equation
𝑎𝑥2
+ 𝑏𝑥 + 𝑐 = 0
Your program should be print
• “Real and Distinct roots” if 𝑏2
> 4𝑎𝑐
• “Same roots” if 𝑏2
= 4𝑎𝑐 and
• “Complex Roots” if 𝑏2
< 4𝑎𝑐
4. INTRODUCTION
Looping in programming language is a way to execute a statement or a
set of statements multiple number of times depending on the result of
condition to be evaluated.
The result of the condition should be true to execute statements within
loops.
5. INTRODUCTION
Loops are mainly divided into two categories:
Entry Controlled Loops: are loops in which condition to be tested is present in
beginning of loop body. while loop and for loop are entry controlled loops.
Exit Controlled Loops: are loops in which the testing condition is present at the
end of loop body. do-while is an exit controlled loop.
Note: In Exit Controlled Loops, loop body will be evaluated for at-least one time as
the testing condition is present at the end of loop body.
6. THEWHILE LOOP
You will get a nice listing
of numbers, from 0 to 4.
The number is first
defined as 0, and each time
the code in the loop is
executed, it's incremented
by one.
class Program
{
static void Main(string[] args)
{
int number = 0;
while(number < 5)
{
Console.WriteLine(number);
number = number + 1;
}
Console.ReadLine();
}
}
The while loop simply executes a block of code as long as the condition you give it is true.
8. THE FOR LOOP
This produces the exact
same output, but as you can
see, the for loop is a bit more
compact. It consists of 3
parts - we initialize a variable
for counting, set up a
conditional statement to test
it, and increment the counter
class Program
{
static void Main(string[] args)
{
int number = 5;
for(int i = 0; i < number; i++)
Console.WriteLine(i);
}
}
The for loop is a bit different. It's preferred when you know how many iterations you want, either
because you know the exact amount of iterations, or because you have a variable containing the
amount.
9. THE DO-WHILE LOOP
The output is the same
though - once the number is
more than 5, the loop is
exited.
class Program
{
static void Main(string[] args)
{
int number = 0;
do
{
Console.WriteLine(number);
number = number + 1;
} while(number < 5);
}
}
do while loop is similar to while loop with only difference that it checks the condition after executing
the statements, i.e it will execute the loop body one time for sure because it checks the condition
after executing the statements.
10. ASSIGNMENT
1. Write a program in C# Sharp to display the multiplication table of a given integer
2. Write a C# Sharp program to calculate the factorial of a given number
3. Write a program in C# Sharp to read 10 numbers from keyboard and find their
sum and average
4. Write a C# program that converts feet to meters.The program should display
feet from 3 to 30 in three-foot increments and the corresponding meter
equivalents. Use the relationship of 3.28 feet to a meter.
5. Write a C# Sharp program to find the sum of first 10 odd numbers.
11. REFERENCES
• Visual C# How to Program (6th Edition) (Deitel Series)
• https://www.geeksforgeeks.org/loops-in-c-sharp/
• https://csharp.net-tutorials.com/control-structures/loops/