Loops Handouts
Loops Handouts
C# for Loop
The for keyword indicates a loop in C#. The for loop executes a block of statements repeatedly
until the specified condition returns false.
for(variable initialization; condition; steps)
{
//execute this code block as long as condition is satisfied
}
for (A ; B; C)
{
D;
}
contain an initialization block (A), condition (B), body (D) and updating commands for the loop variables (C).
It consists of an initialization part for the counter (in the pattern int i = 0), a Boolean condition (i < 10), an
expression for updating the counter (i++, it might be i-- or for instance, i = i + 3) and body of the loop.
Since none of the listed elements of the for-loops is mandatory, we can skip them all and we will get an infinite
loop:
It is executed only once, just before entering the loop. Usually the initialization block is used to declare the
counter-variable (also called a loop variable) and to set its initial value. This variable is "visible" and can be used
only within the loop. In the initialization block is possible to declare and initialize more than one variable.
The condition (loop condition) is evaluated once before each iteration of the loop. For result true the loop’s
body is executed, for result false it is skipped and the loop ends (the program continues immediately after the
last line of the loop’s body).
}
The Body of the Loop
The body of the loop contains a block with source code. The loop variables, declared in the
initialization block of the loop are available in it.
For-Loop – Example
Here is a complete example of a for-loop:
Here is another, more complicated example of a for-loop, in which we have two variables i and sum, that
initially have the value of 1, but we update them consecutively at each iteration of the loop:
Operator "break"
The break operator is used for prematurely exiting the loop, before it has completed its execution in a natural
way. When the loop reaches the break operator it is terminated and the program’s execution continues from
the line immediately after the loop’s body. A loop’s termination with the break operator can only be done from
its body, during an iteration of the loop. When break is executed the code in the loop’s body after it is skipped
and not executed. We will demonstrate exiting from loop with break with an example.
for(int a=10;a<20;a++)
{
if (a > 15) {
/* terminate the loop using break statement */
break;
}
Console.WriteLine("value of a: {0}", a);
}
Operator "continue"
The continue operator stops the current iteration of the inner loop, without terminating the loop.
Calculate the sum of all odd integers in the range [1…n], which are not divisible by 7 by using the for-loop:
First we initialize the loop’s variable with a value of 1 as this is the first odd integer within
the range [1…n]. After each iteration of the loop we check if i has not yet exceeded n (i
<= n). In the expression for updating the variable we increase it by 2 in order to pass only
through the odd numbers. Inside the loop body we check whether the current number is
divisible by 7. If so we call the operator continue, which skips the rest of the loop’s body (it
skips adding the current number to the sum). If the number is not divisible by seven, it
continues with updating of the sum with the current number.
The result of the example for n = 11 is as follows:
While Loops
One of the simplest and most commonly used loops is while.
In the while loop, first of all the Boolean expression is calculated and if it is true the
sequence of operations in the body of the loop is executed. Then again the input condition is
checked and if it is true again the body of the loop is executed. All this is repeated again
and again until at some point the conditional expression returns value false. At this
point the loop stops and the program continues to the next line, immediately after the body
of the loop.
The body of the while loop may not be executed even once if in the beginning the condition of the cycle returns
false. If the condition of the cycle is never broken the loop will be executed indefinitely.
We use the variable divider to store the value of a potential divisor of the number. First
we initialize it with 2 (the smallest possible divider). The variable maxDivider is the
maximum possible divisor, which is equal to the square root of the number. If we have a
divisor bigger than √num, then num should also have another divisor smaller than √num and
that’s why it’s useless to check the numbers bigger than √num. This way we reduce the
number of loop iterations.
For the result we use a Boolean variable called prime. Initially, its value is true. While passing through the loop,
if it turns out that the number has a divisor, the value of prime will become false.
Do-While Loops
The do-while loop is similar to the while loop, but it checks the condition after each execution of its loop
body. This type of loops is called loops with condition at the end (post-test loop). A do-while loop looks like
this:
Initially the loop body is executed. Then its condition is checked. If it is true, the loop’s body is repeated, otherwise
the loop ends. This logic is repeated until the condition of the loop is broken. The body of the loop is executed at
least once. If the loop’s condition is constantly true, the loop never ends.
1. Write a program that prints on the console the numbers from 1 to N. The number N
should be read from the standard input.
2. Write a program that prints on the console the numbers from 1 to N, which are not
divisible by 3 and 7 simultaneously. The number N should be read from the standard
input.
3. Write a program that reads from the console a series of integers and prints the smallest
and largest of them.