We have finished learning about the basic programming structure which is sequential and selection(if, else, switch). Now, we are going to learn the next one which is repetition or looping. It is the most important element in programming which allow you to repeat the coding as many time as you(programmer) wish or also as many time as the users wish. The repetition save less time to code a program since it can help repeat the same command over and over again without having to write the same command repeatedly. You just need to tell the program to repeat it. Three type of syntax in C++ that can perform the looping is for, while and do...while. Lets learn the first one which is the for statement.
Now, always remember that the for statement need to be coded in the following manner:
for(int variable=0 ; variable < xx ; variable++)
{
//write your coding here
}
As you already know, you can replace variable, the looping variable with any word or character that you want and you must initiate it with a value (in the example and for this basic learning, lets initiate it with 0). Any looping variable should be declared with the data type int. xx should be replaced with the number of repetition that you want the coding inside the for statement to loop and I will explain later what is the purpose of variable++
Confused?
Alright lets imagine you code it like this:
for(int i=0;i<3;i++)
{
cout<<"repeating ";
}
Now, thats mean the variable "i" initial value is 0 and it will repeat the coding inside the for statement 3 times. "i<3" meaning that the coding inside the for statement(from the opening of the curly bracket until the closing of the curly bracket) will be repeated while the "i" value is still less than 3. How does the variable "i" becomes 3? Well, the "i++" command is the one that will do the trick(remember variable++?). It will increase the value of "i" by 1 each time the coding is being executed and when the variable "i" reach 3, the looping will stop itself since the condition to loop is "i" must less than 3.
P/S: "i++" is also equal to "i=i+1"
The output for the coding above is produced as below:
repeating repeating repeating
The command cout<<"repeating"; is executed 3 times.
Try compile this full coding using Microsoft Visual C++ 6.0
#include <iostream.h >
void main()
{
for(int i=0;i<3;i++)
{
cout<<"repeating ";
}
}
THINK ABOUT IT
What happen when you want to execute the code 100 times?
Do you prefer using for(int i=0;i<100;i++)?? OR you are much prefer to waste your time and your computer memory by typing the line cout<<"repeating " 100 times.
Try experiment with the syntax of the for statement to see different result. Be comfortable and swift with it.
Things you should try
-What happen when you initiate the variable "i" with 2?
-What happen when you change the looping condition with "i>4"?
-What happen when you change the increment with "i--" or "i+2"?
-And what happen when you declare the looping variable with data type "double" or "float" etc. ?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment