CONTROL STRUCTURES 2
CONTROL STRUCTURES [2]
Types of loop
1.while()
SYNTAX:
while(condition)
{
//code
}
2.do while()
SYNTAX:
do
{
//code
}
while(condition)
3.for()
SYNTAX:
for(initialization ; condition ; increment/decrement)
{
//code
}
Example for for() loop:
Types of loop
1.while()
SYNTAX:
while(condition)
{
//code
}
2.do while()
SYNTAX:
do
{
//code
}
while(condition)
3.for()
SYNTAX:
for(initialization ; condition ; increment/decrement)
{
//code
}
Example for for() loop:
Write a
program to print following output using for loops in C++.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
CODE:
#include <iostream>
using namespace std;
void numpat(int n)
{
cout << "Write a program to print following output using for
loops in C++.\n\n\n";
int num = 1;
for (int i=0; i<n; i++)
{
for (int j=0; j<=i; j++ )
cout << num << " ";
num = num + 1;
cout << endl;
}
}
int main()
{
int n = 5;
numpat(n);
return 0;
}
OUTPUT:
0 Comments