Iterative Sentences C
Iterative Sentences C
1. Objectives
At the end of this activity, you will be able to properly use the while sentence to generate
repetition in the execution of a sequence of sentences.
2. Motivation
Until now, each sentence in our program is executed only once. Execution starts at the first
sentence of the main program, and after it, the computer executes the second sentence which is
just in the bellow line, and so forth to the end. Only in case of finding a conditional sentence (if or
switch) the execution of an instruction would not happen. But, in any case, we always follow
forward and we never come back executing a sentence which had been executed before.
Using iterative sentences allow us to iterate a group of sentences, it means, to execute several
times the same group of sentences meanwhile a specific condition is true. These groups of
sentences are also called loops and the sentences that we repeat inside the loop are called the
body of the loop.
3. While sentence
In the C# language there are several types of iterative sentences, however we only will study the
while sentence, because it is the most standard and we could find it in all programming languages
with very similar syntax.
Syntax
If the body of the loop has only 1 sentence, then curly brackets are not necessary.
Example
We want to do a program that prints on the screen an integer number that will be the double of
the number that a user types on the keyboard. In order to end the program, the user will
introduce a 0 to indicate no more calculations.
while (num != 0)
{
Console.WriteLine("The double of " + num + " is " + 2 * num);
num = Convert.ToInt32(Console.ReadLine());
}
Another example
A very common situation occurs when we know in advance the number of turns that has to be
done by the loop. So, we will use a variable that counts the number of turns, and the loop
condition will check the predicted limit of the variable in each turn.
Look the next loop that writes the multiplication table of number 7:
The counter variable starts on 0 and after each turn of the loop it will increase its value on 1. The
loop ends when the counter variable is equal to 11 (it is the first time when its value does not fulfil
the loop condition).
while (num != 0) ;
{
Console.WriteLine("The double of " + num + " is " + 2 * num);
num = Convert.ToInt32(Console.ReadLine());
}
You could think this loop looks like the first example presented in this paper, but if you look
carefully at it, you will notice there is a semicolon after the while condition, in the first line. It
means the loop ends just there, so there are no instructions to execute. This loop does not finish
at all because num never changes. Therefore, be careful with semicolons.
num = Convert.ToInt32(Console.ReadLine());
Remember that loop must end when num is 0 or the double of the last read number is bigger than
100. Can you see the mistakes?
One iteration more or one iteration less
This kind of mistake occurs if we do not consider the first and/or the last turn of the loop. These
cases are special and we have to treat them properly.
For example, next program tries to calculate the power yx:
y = Convert.ToInt32(Console.ReadLine());
x = Convert.ToInt32(Console.ReadLine());
if (x == 0)
Console.WriteLine("The power is 1");
else
{
res = y;
i = 0;
while (i <= x)
{
res = res * y;
i = i + 1;
}
Console.WriteLine("The power is " + res);
}
}
Write the program in the computer. Does it work? Why? Write it correctly and verify that it
works.
num = Convert.ToInt32(Console.ReadLine());
original = num;
while (num / 10 != 0)
{
if (num % 10 > MAX)
cont++;
num = num / 10;
}
Exercise 4: Write a program that calculates the sum of all numbers between 1 and 100 which are
multiple of 5 or 7. To verify that a number X is multiple of another number Y you only have to
check if the rest of the integer division of X divided by Y is 0.
Exercise 5: Write and test in the computer a program that reads a sequence of integers entered
by the keyboard. The program will sum values each time they are introduced, until the sum will be
bigger than a pre-established number by means of a constant of the program (just like we defined
constant MAX in the previous example). When it happens, the program prints on the screen the
value of the sum and it finishes.