0% found this document useful (0 votes)
122 views

Iterative Control Structure: Do (Statement ) While (Loop Repetition Condition)

This document discusses iterative control structures and loops in programming. It covers the components of loops, different types of loops like do-while, while, for, and foreach loops. It also discusses nested loops, break and continue statements, and provides examples of iterating through numbers with loops. Finally, it provides practice exercises for readers to try iterating through input values and calculating totals using loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Iterative Control Structure: Do (Statement ) While (Loop Repetition Condition)

This document discusses iterative control structures and loops in programming. It covers the components of loops, different types of loops like do-while, while, for, and foreach loops. It also discusses nested loops, break and continue statements, and provides examples of iterating through numbers with loops. Finally, it provides practice exercises for readers to try iterating through input values and calculating totals using loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Iterative Control Structure

Loop
 A control structure that repeats a group of steps in a program. It defines a block of code
that is repeatedly executed.
 Depending on the kind of loop that you are using, the block of code could be executed a
set number of times or until a certain condition is met.
Loop Components
1. Initialization of variables – setting an initial value of zero before the loop statement is
reached.
2. Condition/testing – (that would evaluate to either true or false) the condition to check is
usually made on the current value of the variable initialized in (1) for it controls the loop
repetition.
3. Body – statements that are repeated in the loop.
4. Updating – a statement inside the body of the loop that updates the value of the
variable that is initialized during each repetition.
Four Types of Loop
1. do-while statement
2. while statement
3. for statement
4. foreach statement
do-while loop
 This loop executes a block of codes as long as the specified condition is true.
 It is a post checked loop because it checks the controlling condition after the code is
executed.
Syntax:
do
{
statement;
} while (loop repetition condition);
Example: Print Hello world five times
Flowchart:
Program:
int c;
c=1; // initialization
do
{
Console.WriteLine("Hello World\n"); //statement
c++; // update
} while (c<=5); //conditional expression
Output:

while loop
 Like do-while, this will execute a block of codes while the given condition is true.
 It is a pre-checked loop because it checks the controlling condition first before the code
is executed.
Syntax:
while (loop repetition condition)
{
statement;
}
Example: Print Hello world five times
Flowchart:
Program:
int c;
c=1; // initialization
while (c <= 5) //conditional expression
{
Console.WriteLine("Hello World\n"); //statement
c++; // update
}

Output:

for Loop
 It is usually used to execute code block for a specified number
 It is important to note that all expressions in the for loop are optional. It means that you
can omit any expression or all of them.
 When you omit all expressions, the for loop behaves slightly similar to the while loop
and do while loop with break statement.
Syntax:
for (initialization expression; loop repetition condition; update expression)
{
Statement/s;
}
Example: Print Hello world five times
Program:
for (int c = 0; c <= 5; c++)
{
Console.WriteLine("Hello World\n"); //statement
}
Output:

Nested loop
 C# allows to use one loop inside another loop.
Syntax:
Nested for loop:
for ( init; condition; increment ) {
for ( init; condition; increment ) {
statement(s);
}
statement(s);
}

Nested while loop

while(condition) {
while(condition) {
statement(s);
}
statement(s);
}

Nested do while loop

do {
statement(s);
do {
statement(s);
}
while( condition );
}
while( condition );

Note: you can have a nested loop using different type of loops.
Example:

Make a program to display multiplication table.


namespace multiplicationTable
{
class Program
{
static void Main(string[] args)
{
int i, j;
// i is for rows and j is for columns
for(i = 1;i<=10;i++){
for(j=1;j<=10;j++){

Console.Write(i*j + "\t");
}
Console.WriteLine();

Console.ReadKey();

}
}
}

Output:
Break and Continue
 Repetition loops can also be altered by break or continue statements.
Break
 Exits from the lowest-level loop in which it occurs.
 used for early exit from the loop, or for exiting a forever loop
Continue
 causes immediate loop iteration, skipping the rest of the loop statements
 jumps to the loop test when used with while or do
 jumps to loop increment, then test when used with for
 does not exit the loop (unless next loop is false)
Iteration Examples:
1. Display all the even numbers from 1 to 50.
Program:
namespace loopExample1

{
class Program
{
static void Main(string[] args)
{
//1. Display all the even numbers from 1 to 50.
for (int i = 1; i <= 50; i++) {
if(i%2==0){
Console.WriteLine(i);
}
}
Console.ReadKey();
}
}
}
Output:
2. Display the sum of all odd numbers from 0 to N.
Program:
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int i=1, N;
N= Convert.ToInt32(Console.ReadLine());
do{
Console.WriteLine(i);
i= i+2; // i+=2
}while(i<=N);
Console.ReadKey();

}
}
}

Output:

3. Display the average of five integers.


Program:
namespace loopExample3
{
class Program
{
static void Main(string[] args)
{
int i=1,num,sum=0;
double ave = 0;
while (i <= 5) {
num = Convert.ToInt32(Console.ReadLine());
sum += num; // sum = sum + num;
i++;
}
//type cast sum since average is double and sum is int
ave = (double)sum / 5;
Console.WriteLine("The average:"+ ave);
Console.ReadKey();

}
}
}
Output:

4. Input N integers and count how many negative, positive and zeroes were entered. At
the end, print the counted values.
Program:
namespace loopExample4
{
class Program
{
static void Main(string[] args)
{
int i,N,num,neg=0,pos=0,zero=0;

i=1;
Console.WriteLine("Input how many integers");
N = Convert.ToInt32(Console.ReadLine());
do{
num = Convert.ToInt32(Console.ReadLine());
if(num > 0){
pos++;
}
else if(num < 0){
neg++;
}
else{
zero++;
}
i++;
}while(i<= N);
Console.WriteLine("No of negative integers:" + neg);
Console.WriteLine("No of positive integers:" + pos);
Console.WriteLine("No of zero integers:" + zero);
Console.ReadKey();

}
}
}
Output:

Practice Exercises
1. Input 10 integers and determine how many are negative, the average of all positive,
how many zeroes were entered, and the sum of all numbers between 20 to 60 that are
divisible by 4. At the end output the counted and accumulated values.
2. Write a program that computes and prints the factorial of N.
Exercises (graded)
Create one folder to save all the following exercise below.

1. The goals scored in a soccer match may be input as a sequence of 1s and 2s terminated
by 0. The number 1 would mean that team 1 made a goal and 2 would mean that team
2 made a goal. Thus the sequence 1 2 1 2 1 1 2 0 means that Team 1 made 4 goals and
team 2 made 3 goals in all. Write a program that would input such sequence and output
how many goals each team made and the result of the match (“Team 1 won”, ”Team 2
won”, “Both tied”). Ensure that inputs are 0,1 and 2 only.
2. Take as input the student’s grades of seven subjects and its corresponding unit taken in
a semester. A student is considered failed if the average grade is less than 3.0. Compute
the student’s General Point Average (GPA) by multiplying the grade per subject by its
corresponding unit, sum up the products and divide the sum by its total units. Print the
computed average grade and whether the student “failed” or “passed”.

You might also like