THIS IS A LECTURE submitted by Engr. Rosanna A. Villapando, a Computer Professor in New Era University and Rizal Technological University, Department of Instrumentation and Control Engineering. (You, too, can have your lectures, readings, researches, articles, etc. posted here. Send them through e-mail to OurHappySchool@yahoo.com.)
LEARNING GUIDE
TOPIC: Understand Program
Control Structure
TASK/UNIT: Learning what is looping
CREDITS: 3 HRS.
TASK/COMPETENCY : Apply Looping Method in Solving Mathematical Problems
INTRODUCTION:
Beginners encounter difficulty in programming, especially when solving mathematical problems. The basic concepts in programming is the used of control structures. Programmers must learn the three control structures used in programming. These three basic structures are for loop, while loop, and do while which can be used to develop any kind of program.
PERFORMANCE OBJECTIVE:
Given an instruction, understand the principles of looping. Your performance must meet the criteria for understanding principles of looping based on the Performance Test.
ENABLING OBJECTIVES:
1. Understand and define what is looping
2. Know the different types of looping method
3. Know how to use looping in programming
4. Apply looping method in solving mathematical problems
METHODOLOGY:
Online Discussion
INFORMATION SHEET
Loops
I. INTRODUCTION
Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming -- many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times. (They may be executing a small number of tasks, but in principle, to produce a list of messages only requires repeating the operation of reading in some data and displaying it.) Now, think about what this means: a loop lets you write a very simple statement to produce a significantly greater result simply by repetition.
Loop consists of two parts:
1.) Body => group of statements which is repeated a number of times
2.) Continuation condition => controls how many times the body is repeated. It is tested to determine whether to repeat the body another time.
Types of Looping Method and How to use them in Programming:
Caveat: before going further, you should understand the concept of C++'s true and false, because it will be necessary when working with loops (the conditions are the same as with if statements). There are three types of loops: for, while, and do..while. Each of them has their specific uses. They are all outlined below.
FOR - for loops are the most useful type. The syntax for a for loop is for (variable initialization; condition; variable update) {Code to execute while the condition is true}
where:
variable initialization (expression1) => usually assigned to the index variable (evaluated just once, at the start of the loop)
condition (expression2) => test for loop continuation (conditional test evaluated at the start of each iteration) variable update (expression3) => usually some modification of the index variable (evaluated at the end of each iteration)
The variable initialization allows you to either declare a variable and give it a value or give a value to an already existing variable. Second, the condition tells the program that while the conditional expression is true the loop should continue to repeat itself. The variable update section is the easiest way for a for loop to handle changing of the variable. It is possible to do things like x++, x = x + 10, or even x = random ( 5 ), and if you really wanted to, you could call other functions that do nothing to the variable but still have a useful effect on the code. Notice that a semicolon separates each of these sections, that is important. Also note that every single one of the sections may be empty, though the semicolons still have to be there. If the condition is empty, it is evaluated as true and the loop will repeat until something else stops it.
Example:
#include <iostream>
using namespace std; // So the program can see cout and endl
int main()
{
// The loop goes while x < 10, and x increases by one every loop
for ( int x = 0; x < 10; x++ ) {
// Keep in mind that the loop condition checks
// the conditional statement before it loops again.
// consequently, when x equals 10 the loop breaks.
// x is updated before the condition is checked.
cout<< x <<endl;
}
cin.get();
}
This program is a very simple example of a for loop. x is set to zero, while x is less than 10 it calls cout<< x <<endl; and it adds 1 to x until the condition is met. Keep in mind also that the variable is incremented after the code in the loop is run for the first time.
WHILE - WHILE loops are very simple. The basic structure is
while (condition) {Code to execute while the condition is true} The true repesents a boolean expression which could be x== 1 or while (x!=7) (x does not equal 7). It can be any combination of boolean statements that are legal. Even, (while x ==5|| v ==7) which says execute the code while x equals five or while v equals 7. Notice that a while loop is the same as a for loop without the initialization and update sections. However, an empty condition is not legal for a while loop as it is with a for loop.
Example:
#include <iostream>
using namespace std; // So we can see cout and endl
int main()
{
int x = 0; // Don't forget to declare variables
while ( x < 10 ) { // While x is less than 10
cout<< x <<endl;
x++; // Update x so the condition can be met eventually
}
cin.get();
}
This was another simple example, but it is longer than the above FOR loop. The easiest way to think of the loop is that when it reaches the brace at the end it jumps back up to the beginning of the loop, which checks the condition again and decides whether to repeat the block another time, or stop and move to the next statement after the block.
DO..WHILE - DO..WHILE loops are useful for things that want to loop at least once. The structure is
do {
} while ( condition );
Notice that the condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. If the condition is true, we jump back to the beginning of the block and execute it again. A do..while loop is basically a reversed while loop. A while loop says "Loop while the condition is true, and execute this block of code", a do..while loop says "Execute this block of code, and loop while the condition is true".
Example:
#include <iostream>
using namespace std;
int main()
{
int x;
x = 0;
do {
// "Hello, world!" is printed at least one time
// even though the condition is false
cout<<"Hello, world!\n";
} while ( x != 0 );
cin.get();
}
Keep in mind that you must include a trailing semi-colon after the while in the above example. A common error is to forget that a do..while loop must be terminated with a semicolon (the other loops should not be terminated with a semicolon, adding to the confusion). Notice that this loop will execute once, because it automatically executes before checking the condition.
REFERENCE:
EXERCISE
Create a c++ program to calculate the mean and standard deviation of a set of numbers entered from the keyboard. The mean of a set of numbers is given as the ∑(numbers)/∕n, where ‘n’ is the number of values summed. The standard deviation is given by the equation (∑ (numbers)2 ∕ n – mean2)1/2
SELF – CHECK
Instruction: Write the letter of the best answer provided for before each number.
_______1. What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run?
A. 10
B. 9
C. 0
D. 1
_______2.
When does the code block following while (x<100) execute?
A. When x is less than one hundred
B. When x is greater than one hundred
C. When x is equal to one hundred
D. While it wishes
_______3.
Which is not a looop structure?
A. for
B. do while
C. while
D. repeat until
______4.
How many times is a do while loop guaranteed to loop?
A. 0
B. Infinitely
C. 1
D. Variable
ANSWER KEY
1. A.
2. A.
3. D.
4. C.
SELF-CHECK
DIRECTION: Evaluate yourself on your ability to understand the principles of Looping by completing the Self-Check. After each items, place X on the box that best describe your accomplishment, if performance element was not applicable, or impossible to accomplish, place an X in its column
|
|
PERFORMANCE LEVEL
|
N/A
|
No
|
Partial
|
Full
|
-
Understand and define what is looping
-
Know the different types of looping method
-
Know how to use looping in programming
-
Apply looping method in solving mathematical problems
|
|
|
|
|
PROMOTE your products, services, business, school, organizations, videos, etc. in OurHappySchool! Have an OHS AD PAGE for a price that even students can afford!