Looping

Before we start writing a program using loop please consider below:

Understand the problem domain

identify the steps which, when taken, will satisfy the program's requirements

Identify the data needed to solve the problem, and whether the data is being generated by the program or provided as input

Basically problem solving with computer need 3 major steps:

Input

Process

Output

Use Analysis tools effectively when solving the problem.

 

Basically there are 3 types of loops

1.                  While / While-End loop

2.                  Repeat / Until Loop

3.                  Automatic Counter Loop

 

What is loop

Loop is a repeatitive structure. It will repeat a set of instruction several time for different set if date processing.

Most business problems involve in doing the same task over and over for e.g.

            PAYROLL for employee – where the same process on a different set of data.

 

 

While / While-End

 

Algorithm for While/ while-end loop

            While (Condition)

                  Instruction

                  Instruction

                        :

                        :

             While – end

 

           

The Flowchart for While-while-end loop

 

 

Syntax in C – language for While / while-end loop

 

            while (condition) {

 

                        instruction;

                        instruction;

                                    :

                                    :

            }

 

Repeat / Until Loop

 

Algorithm for Repeat / Until loop

 

Repeat

            Instruction

            Instruction

                        :

                        :

Until (condition)

Flowchart for repeat / until loop

 

 

Syntax in C language for Repeat / until

 

In C the repeat / until loop using the do while keyword. In do while loop the condition will be test at the end of the loop. Instead of false condition will entering the loop, in do while true will make the control will enter the loop until the condition is false then the control will exit the loop.

 

            do {

                        instruction

                        instruction

                                    :

                                    :

                 } while (condition);

 

Differences between while / while – end and repeat / until loop

1.                  While loop will continue the loop as long as the condition is true, but in repeat / until loop it will exit loop when the condition is true.

2.                  The while  loop, the condition is at the beginning, but in repeat until the condition is at the end of the loop.

3.                  In while loop the process might not enter the loop but in repeat / until loop, the process will at least enter the loop once.

 

When does we use the while and repeat / until loop

*           Ussually we will use the both loop when we do not know the number of times the instruction should repeat.

 

Examine the example below:

 

//program 1

#include <stdio.h>

int main (void) {

int num, total;

 

scanf(“%d”,num);

while (num != 0) {

            scanf (“%d”,num);

             total = total + num;

}

   printf (“Total = \n” , total);

  return (0);

}

 

//Program 2

#include <stdio.h>

int main (void) {

int num, total;

 

scanf(“%d”,num);

do {

      scanf (“%d”,num);

      total = total + num;

} while (num != 0) ;

printf (“Total = \n” , total);

return (0);

}

 

 

Automatic conter loop

 

This type of loop, it increament or decreament a variable each time the loop is processed.

The variable is use as a counter, that starts counting at a specific number, and increament or decreament the variable each time the loop is processed.

The set of instruction within the loop repeats until the counter is greater or lesser than an ending number.

 

The algorithm of automatic – counter loop

 

Loop : counter = begin to end step

     Instruction

     Instruction

            :

            :

Loop-end : counter

 

Flowchart for automatic counter loop

 

 

 

 

 

The syntax for a for-loop is

 
      for (expression1; expression2; expression3) {
         statements
      }
     

We noted that each of the expressions in the loop's control statement is completely optional. The semantics of the forloop are:

1.                  evaluate expression1;

2.                  evaluate expression2 - if it is TRUE, then do steps 3 and 4 - otherwise, continue at step 5

3.                  do all of the statements within the body of the for loop

4.                  evaluate expression3 and continue at step 2

5.                  (get here when loop has completed) - carry on execution with the statement immediately past the body of the for loop

When we usually use the automatic counter loop.

We usually use the loop when we want to execute and exact numbers of times of loop.

Consider the program below we are to print number 1 to 10 and add the number.

 

#include <stdio.h>

int main (void) {

 

int i, total;

 

for (i = 1; i < 11; I++) {

   printf(“%d”,i);

   total = total + i;

   }

printf(“%d”,total);

}

 

---
Some interesting problems

 Increament

   It is a task done by adding a constant such as 1 or 2 to a value of a variable.

   For e.g.

                   c = c + 1;

                   or c = c + 2;

                   1 and 2 is adding to a variable constantly.

 

Accumulating

   It is a task to sum a group of numbers. It is similar to increament but the valu will be the value of the total numbers.

   e.g totalsale = totalsale + monthlysale;

 

 

Sentinel

   A sentinel value is a data value that is impossible in the context of the problem. It could br use to indicate to a program  that some special condition has arisen, and this special condition ussually use or intend to exit a loop. It ussually use in while and do while loop.

---
Relational operators

It turns out that virtually all C-language expressions may be used as a conditional expression. For now, we will limit our study only to those involving arithmetic operands and relational operators.

The relational operators are:

==              equality. TRUE when both its operands have the same value NOTE: this operator is not the same as the assignment operator, =

!=               inequality - TRUE when == is FALSE

<                TRUE when the left operand is less than the right operand

<=              TRUE if the left operand is less than or equal to the right operand

>                TRUE when the left operand is greater than the right operand

>=              TRUE when the left operand is greater than or equal to the right operand

 

The semantics (logic of the statement) of the condition statement are to evaluate the conditional expression. Just when the expression is true, the block of statements is executed. Just when the expression is false, the block of statements following the exit the loop.