Home » » Simple Looping ( For )

Simple Looping ( For )

Written By Unknown on Sabtu, 21 Januari 2012 | 22.30

Lab RPL .
Below is a program that will print out the current value of the loop counter. It wiill ask you a user to enter a number ( less than 50 but positive ) which will be how many times you want to let the loop run.
This is a good way to view how a loop will run .

//for loop to print out index
#include <iostream>
void main ()
{
int times;
cout<<"How Many Times ? " ";
cin>>times ;
if (times< 0 | | times >= 50) exit (1) ;
for ( int i = 1; i <= times;i++)
{
cout<< i <<endl;
}
return 0
}

As you will observe , the program prints out each number on a separate line.
when dealing with loops , it is possible to have loops nested inside one another. the rule about this is that the inner must loop will be ised first, then the one above that and the one above that one etc . .

An Example of this is when we are perhaps trying to print out a table of some kind. Here, let's print out a simple square of stars using two for loops.

0 komentar:

Posting Komentar