0% found this document useful (0 votes)
2 views2 pages

Lab 9 - Loops

The document provides examples of C++ programs demonstrating the use of the break and continue statements within iterative structures. It includes tasks for practice, such as writing programs to count positive and negative numbers, display squares and cubes, and print patterns using nested loops. Additionally, it outlines a task to create a diamond shape using asterisks with minimal output statements.

Uploaded by

Moeez Naqvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Lab 9 - Loops

The document provides examples of C++ programs demonstrating the use of the break and continue statements within iterative structures. It includes tasks for practice, such as writing programs to count positive and negative numbers, display squares and cubes, and print patterns using nested loops. Additionally, it outlines a task to create a diamond shape using asterisks with minimal output statements.

Uploaded by

Moeez Naqvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Iterative Structure

Practice Codes:

The break Statement:

#include <iostream>
using namespace std;

int main()
{
int count; // control variable also used after loop terminates
for ( count = 1; count <= 10; count++ ) // loop 10 times
{
cout << count << " ";
} // end for
cout << "\nBroke out of loop at count = " << count << endl;
return 0;

} // end main

The continue Statement:

#include <iostream>
using namespace std;

int main()
{
for ( int count = 1; count <= 10; count++ ) // loop 10 times
{
if ( count == 5 ) // if count is 5,
cout << count << " ";
} // end for

cout << "\nUsed continue to skip printing 5" << endl;


return 0;
} // end main
TASKS:

1. Practice the examples, discuss in theory lecture and from the Chapter # 5 of text book.
2. Write a C++ program to enter numbers until the user wants to stop and at the end it should display the
count of positive, negative, even, odd and zeros entered.
3. Write a C++ program to display a table showing value of x, the square of x, and the cube of x
from 1 to 50.
4. Use nested loops to print numbers such that the outer loop prints the number of rows and the
inner loop prints the number of columns. With each increasing row, the number of columns
increases. The output looks as follows.
1
12
123
1234
12345
123456
1234567
12345678
123456789

5. Write a program that uses for statements to print the following patterns separately, one below
the other. Use for loops to generate the patterns.

6. (Diamond of Asterisks) Write a program that prints the following diamond shape. You may
use output statements that print a single asterisk (*), a single blank or a single newline.
Maximize your use of repetition (with nested for statements) and minimize the number of
output statements.

You might also like