Friday, November 9, 2018

Counter Variables

A counter variable keeps track of number of times a particular operation has been performed.
If you wish to display the message "hello world" ten times, then you must have some mechanism by which you can count the number of times the message "hello world" is displayed. To achieve this, you might have to use a variable which keeps track of the number of times the message is displayed  This variable is known as counter variable.
Initially the variable will contain a value 1.Each time before displaying the message "hello world", the value of the counter variable has to be checked. If the value is less than 11,then the value contained in the counter variable is increased by 1and hello world is displayed  The moment the value e exceeds 10 the operation is terminated.

Eg.
Problem:to display hello world 10 times.

Algorithm
The algorithm for above problem will be:
1.Start
2.Set value of counter variable to 1.
3.Check if value of counter variable is less than 11.
3.1if value is less than 11
3.1.1increase counter variable by 1.
3.1.2display "hello world".
3.1.3go back to step 3.
3.2If the value is 11,terminate operation.
4.End



No comments:

Post a Comment