Iteration (Repetition) Constructs Flashcards
(3 cards)
How to create a while loop?
while (condition)
{
statement block
}
________________________________________________
cout «_space;“\nPlease enter your age : “;
cin»_space; age;
while ( (age <= 0) || (age > 100) )
{
cout «_space;“\nInvalid age.\n”;
cout «_space;“Please Re-enter age : “;
cin»_space; age;
}
How to create a do-while loop?
do
{
statement block
} while (condition);
______________________________________________________
do
{
cout «_space;“\nEnter Number (0 to terminate): “;
cin»_space; number;
if (number != 0)
{
sum = sum + number;
count++;
}
} while(number != 0);
How to create a for loop?
for (initialisation; test condition; re-initialisation)
{
statement block;
}
____________________________________________________________________________
for (count = 0; count <5; count++;)
{
cout «_space;“Hello world”
}