Paul's Tutorials - logo1-3Loops



Reading

Go here:
Loops
and read through to the end of the section titled "Iteration statements (loops)".

Extra Reading

Companion Video

Practice

Once you feel comfortable with the above content, complete the following practice programs.

Counter

Create a for loop that counts 1 to 101. Count up by 4. Display the number each time it goes through the loop.

Counterintuitive Exit

Create a do-while loop that asks the user if they would like to quit the program. Keep on asking the question until the user says no. If the user does not enter a proper response (yes or no), tell the user they entered an improper answer and ask if they would like to quit again.

Right Triangle

Write a program to print this triangle:

+
++
+++
++++
+++++
++++++
+++++++
++++++++
+++++++++
++++++++++

Don't use ten cout statements; use two nested loops (a loop in a loop) instead. You'll have to use braces around the body of the outer loop if it contains multiple statements:

for(i = 1; i <= 10; i++)
{
	/* multiple statements
 	 * can go in here */
}

(Hint: a string you hand to cout does not have to contain the newline character \n or the newline function endl).

Extra Practice

Rewrite this program to allow the user to pick the height of the triangle, still following the same line pattern. You may want to use an unsigned int in combination with an input check to prevent the user from creating impossible triangles.


← 1-2 If and Else 1-3 Loops 1-4 Switch/Case Statements →