See discussions, stats, and author profiles for this publication at: https://www.researchgate.
net/publication/349625339
Lecture 6 Loops in C#
Presentation · February 2021
DOI: 10.13140/RG.2.2.18354.32965
CITATIONS READS
0 1,802
1 author:
Tarfa Hamed
University of Mosul
38 PUBLICATIONS 246 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
Using Human-Computer Interaction in Education View project
Advanced Programming in C# View project
All content following this page was uploaded by Tarfa Hamed on 26 February 2021.
The user has requested enhancement of the downloaded file.
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
Department of Computer Science
First Year
Introduction to Programming 1
Loops
Loops can execute a block of code as long as a specified
condition is reached.
Loops can be achieved in C# via three statements:
Loops
for while do while
1. for Loop
When you know exactly how many times you want to loop
through a block of code, use the for loop to repeat that
block:
Syntax
for (initialization; condition; update)
{
// code block to be executed
}
1
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
initialization is executed (one time) before the
execution of the code block.
condition defines the condition for executing the code
block.
update is executed (every time) after the code block has
been executed.
Example
The example below will print the numbers 0 to 4:
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
Example explained
initialization sets a variable before the loop starts
(int i = 0).
condition defines the condition for the loop to run (i
must be less than 5). If the condition is true, the loop will
start over again, if it is false, the loop will end.
update increases a value (i++) each time the code block
in the loop has been executed.
Example 1
This example will only print even values between 0 and 10:
for (int i = 0; i <= 10; i += 2)
{
Console.WriteLine(i);
}
2
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
Example 2
This example will print the numbers from 10 to 1:
for (int i = 10; i > 0; i--)
{
Console.WriteLine(i);
}
Example 3
This example will find and print the sum of the odd numbers
between 1 and 10:
int sum = 0;
for (int i = 1; i < 10; i += 2)
{
sum += i;
}
Console.WriteLine(sum);
Nested for loops
Loops can be nested (i.e., a loop can include another loop
inside)
for (initialization; condition; update)
{
for(initialization; condition; update)
{
// inner loop statements.
3
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
}
// outer loop statements.
}
Example 1
Write a C# program to print the following pattern:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
int n = 3;
for (int i = 1; i <= n; i++) // outer loop
{
for (int j = 1; j <= 10; j++) // inner loop
{
Console.Write( (i * j)+"\t");
}
Console.WriteLine();
}
Example 2
Write a C# program to print the following pattern:
********
********
********
********
4
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
int n = 4;
for (int i = 1; i <= n; i++) // outer loop
{
for (int j = 1; j <= 8; j++) // inner loop
{
Console.Write( "*");
}
Console.WriteLine();
}
Example 3
Write a C# program to print the following pattern:
*
**
***
****
*****
int n = 5;
for (int i = 1; i <= n; i++) // outer loop
{
for (int j = 1; j <= i; j++) // inner loop
{
Console.Write("*");
}
Console.WriteLine();
}
5
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
2. while loop
The while loop loops through a block of code as long as a
specified condition is True:
False
Condition
True
Statements
Syntax
while (condition)
{
// code block to be executed
}
In the example below, the code in the loop will run, over
and over again, as long as a variable (i) is less than 5:
6
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
Example 1
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
Note: Do not forget to increase the variable used in the
condition, otherwise the loop will never end!
Example 2
This C# program asks to input unlimited number of integer
numbers and finds the summation of odd numbers only. The
program ends by entering -999.
int sum = 0;
int num = Convert.ToInt32(Console.ReadLine());
while (num != -999)
{
if (num % 2 != 0)
{
sum += num;
}
num = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine(sum);
7
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
Example 3
This program prints the numbers from 10 to 19:
int a = 10;
while (a < 20)
{
Console.WriteLine(a);
a++;
}
3. The Do/While Loop
The do/while loop is a variant of the while loop.
This loop will execute the code block once, before
checking if the condition is true, then it will repeat the loop
as long as the condition is true.
Syntax
do
{
// code block to be executed
}
while (condition);
8
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
Statements
True
Condition
False
The example below uses a do/while loop.
The loop will always be executed at least once, even if the
condition is false, because the code block is executed
before the condition is tested:
Example 1
int i = 0;
do
{
Console.WriteLine(i);
i++;
} while (i < 5);
Note: Do not forget to increase the variable used in the
condition, otherwise the loop will never end!
9
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
Example 2
double number, sum = 0.0;
// the body of the loop is executed at least
once
do
{
Console.WriteLine("Enter a number: ");
number = Convert.ToDouble(Console.ReadLine());
sum += number;
}while (number != 0.0);
Console.WriteLine("Sum = " + sum);
Examples on Loops in C#
1. Finding xy using for statement.
int x, y;
x = Convert.ToInt32(Console.ReadLine());
y = Convert.ToInt32(Console.ReadLine());
int z = 1;
for(int i=1; i<=y; i++)
{
z *= x;
}
Console.WriteLine(z);
2. Finding the average of 7 marks using while statement
int sum = 0;
int mark; int i=1;
double average;
10
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
while (i <= 7)
{
mark = Convert.ToInt32(Console.ReadLine());
sum += mark;
i++;
}
average = Convert.ToDouble(sum) / 7;
Console.WriteLine("Average = " + average);
3. Finding the maximum value from 100 random values
(positive integers) entered from keyboard using do while
statement.
int i = 1;
int max = -1;
int num;
do{
num = Convert.ToInt32(Console.ReadLine());
i++;
if (num > max)
{
max = num;
}
} while (i <= 5);
Console.WriteLine("Maximum is " + max);
11
Department of Computer Science, College of Computer Science and Mathematics, University of
Mosul, Iraq
Homework:
1. Input 100 random numbers and count the odd and even
numbers using for statement.
2. Input 100 random integer numbers (positive and
negative) and sum the positive and negative numbers
using while statement.
3. Output this series using do while statement:
1 2 4 8 16 ….. 1024
4. Print this pattern using for statement:
1
12
123
1234
12345
5. Sum this series using while statement:
3 5 7 ….99
6. Print this pattern using for statement:
*****
****
***
**
*
7. Calculate this series using while statement:
1 2 3 𝑛
Y=
𝑥 2 + 𝑥 3 + 𝑥 4 + ⋯.+ 𝑥 𝑛 +1
12
View publication stats