2. algorithm - Message

Before we write an algorithm for this process (computer program), it is necessary to create a pseudo code.

Understanding the process:

The process of printing multiple messages, represents an action that is repeated several times in succession.

For the purpose of the program is essential variable "MESSAGE" containing the text of the message, which is entered by the user via the keyboard.

Limitations of the process:

The number of repetitions printing is limited to five, and the process is executed only once.

Steps in pseudo code:

1. Start

2. Display a message on the screen "Enter the text message"

3. Assign the value of the variable MESSAGE, enter value from the keyboard

4. Show MESSAGE

5. Show MESSAGE

6. Show MESSAGE

7. Show MESSAGE

8. Show MESSAGE

9. End

What is immediately apparent is that this method of printing multiple messages is not effective if the number of repetitions is let say a million times, because in that case it was necessary one and the same step to repeat a million times in the program, with amount to just a million commands to print messages !? In order toovercome that, algorithm and the program itself was introduced therm the LOOP.

Loop is a cyclic structure in which part of the process takes place (certain steps that are repeated a number of times - Steps 4,5,6,7 and 8) .For the purposes of loops is necessary counter that contains a numeric value, the number of repetitions of certain steps. In our case it will be COUNTER.

Steps in pseudo code:

1. Start

2. Display a message on the screen "Enter the text message"

3. Assign the value of the variable MESSAGE, enter value from the keyboard

4. COUNTER = 5

6. Show MESSAGE

7. COUNTER = COUNTER - 1

8. If the COUNTER > 0 go to step 6

9. End

Now the pseudo code is different. Variable COUNTER received the value 5, which is the number of repetitions of printing a message on display. Also the number of stepsin this pseudo code is the same as in the previous, the expediency of loops can be seen as it is said in the event that the number of repetitions print a large message, say 1000 times. Also loop cyclic structure as seen from step 4 wher value of COUNTER is set, to the step 8 with setting conditions for LOOP termination. COUNTER after each print job is reduced by one and checked for compliance with the conditions in step eighth. Checking the loop is done because in order to see whether the message will be printed exactly 5 times. This is done by going through the cycle step by step, with manual controll of variable values.

First pass:

COUNTER = 5, Print, COUNTER = 4, the condition COUNTER> 0; Yes; CONTINUE THE LOOP

Second pass:

COUNTER = 4, Print, COUNTER = 3, the condition COUNTER> 0; Yes; CONTINUE THE LOOP

The third passage:

COUNTER = 3, Print, COUNTER = 2, the condition COUNTER> 0; Yes; CONTINUE THE LOOP

The fourth passage:

COUNTER = 2, Print, COUNTER = 1, the condition COUNTER> 0; Yes; CONTINUE THE LOOP

The fifth passage:

COUNTER = 1, Print, COUNTER = 0, the condition COUNTER> 0; Not; END LOOP

What we need to bear in mind is the condition in step 8, which determines how much will be cicles in the loop.

It often happens that an error occurs in a number of passes for one pass, because of error in condition set.

If condition was set to: "If COUNTER = 0 go to step 6 ', for example, then there would be a six cicles through the loop.

Now that we wrote pseudo code, we cross to algorithm writing :

The algorithm itself is understandable when you learn the meaning of the basic elements of the algorithm, see page algorithms.

Also in the algorithm itself LOOP is a lot clearer to understand,

so that in the following examples of algorithms we wont use the pseudo code.

Try to change the algorithm that instead of 5, there is 10 reps printing,

or with a text message print the numerical value of the COUNTER,

lets say MESSAGE = "Hello!" , the result would be:

1. Hello!

2. Hello!

3 ...

4 ...

5. Hello!

All the best,

Author