Lecture6 C
Lecture6 C
net/publication/349625339
Lecture 6 Loops in C#
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:
All content following this page was uploaded by Tarfa Hamed on 26 February 2021.
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
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
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);
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++;
}
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
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);
while (i <= 7)
{
mark = Convert.ToInt32(Console.ReadLine());
sum += mark;
i++;
}
average = Convert.ToDouble(sum) / 7;
Console.WriteLine("Average = " + average);
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