Lecture 7
Lecture 7
For Loop:
A for loop is a repetition control structure that allows you to efficiently write a
loop that needs to execute a specific number of times.
Syntax
The syntax of a for loop in C++ is:
for (init; condition; increment or decrement)
{
statement(s);
}
1
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
Flow Diagram
2
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
3
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
Nested Loops
A loop can be nested inside of another loop. C++ allows at least 256 levels
of nesting.
Syntax
The syntax for a nested for loop statement in C++ is as follows:
for (init; condition; increment)
{
for (init; condition; increment)
{
statement(s);
}
statement(s); // you can put more statements.
}
4
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int i, j;
for(i=2;i<100;i++)
{
for(j=2;j<=(i/2);j++)
if(i%j==0)
break; // if factor found, not prime
if(j>(i/2))
cout << i << " is prime\n";
}
system("pause");
return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int i, n;
cout << "Enter a number: ";
cin >> n;
for(i=2;i<=(n/2);i++)
if(n%i==0)
{
cout<<n<<" : is not prime!"<<endl;
system("pause");
exit(1);
}
cout<<n<<"is prime!"<<endl;
system("pause");
return 0;
}
5
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
1- Break statement:
The break statement has the following two usages in C++:
- When the break statement is encountered inside a loop, the loop is
immediately terminated and program control resumes at the next
statement following the loop.
- It can be used to terminate a case in the switch statement (covered in
the next chapter).
If you are using nested loops (i.e., one loop inside another loop), the break
statement will stop the execution of the innermost loop and start executing the
next line of code after the block.
Syntax
The syntax for a break statement in C++ is as follows:
break;
Flow Diagram
6
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
Example:
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int a = 1;
for(;;)
{
if(a > 15)
break;
cout << "value of a: " << a << endl;
a++;
}
system("pause");
return 0;
}
Example:
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int a = 1;
do
{
cout << "value of a: " << a << endl;
a = a + 1;
if(a > 15)
{
break;
}
}while(a<20);
system("pause");
return 0;
}
7
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
2- continue statement:
The continue statement works somewhat like the break statement. Instead
of forcing termination, however, continue forces the next iteration of the loop
to take place, skipping any code in between.
For the for loop, continue causes the conditional test and increment portions
of the loop to execute. For the while and do...while loops, program control
passes to the conditional tests.
Syntax
The syntax of a continue statement in C++ is:
continue;
Flow Diagram
8
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
Example:
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int a = 1;
while(1)
{
if(a <= 15)
{
cout << "value of a: " << a << endl;
a++;
}
cout<<"GO!"<<endl;
break;
}
system("pause");
return 0;
}
Output:
In the example above, we have seen that the output is only one value.
To correct this program, we will use (continue statement) as follows:
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int a = 1;
while(1)
{
if(a <= 15)
{
cout << "value of a: " << a << endl;
a++;
continue;
}
cout<<"GO!"<<endl;
break;
}
system("pause");
return 0;
}
9
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
3- Goto Statement
A goto statement provides an unconditional jump from the goto to a labeled
statement in the same function.
NOTE: Use of goto statement is highly discouraged because it makes difficult
to trace the control flow of a program, making the program hard to understand
and hard to modify. Any program that uses a goto can be rewritten so that it
doesn't need the goto.
Syntax
The syntax of a goto statement in C++ is:
goto label;
..
.
label: statement;
Flow Diagram
10
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
Example:
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int a = 1;
Loop:
while(1)
{
if(a <= 15)
{
cout << "value of a: " << a << endl;
a++;
goto Loop;
}
cout<<"GO!"<<endl;
break;
}
system("pause");
return 0;
}
11
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
Examples:
1- Write a program to calculate the factorial of a number:
n!=n*(n-1) *(n-2) *(n-3)*…..*1
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int n,k,fact;
cout << "\nEnter an integer number :";
cin >> n;
if(n==0)
cout << "\nThe factorial of 0 is 1";
else if(n>0)
{
for(fact=1,k=n;k>=1;k--)
fact*=k;
cout << "\nThe factorial of" << n << "is: "<< fact
<<endl;
}
else
cout << "\nError Data Entry";
system("pause");
return 0;
}
(H.W)
2- Rewrite the previous program by using another method.
3- Write a program to print 5 stars as follows:
a- ***** (H.W) b- *
*
*
*
*
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int i;
for(i=1;i<=5;i++)
cout << "*";
cout<<endl;
system("pause");
return 0;
}
12
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
13
Programming in C++ / 1st Stage Diyala University - College of Engineering
Prepared by: Asst. lect. Ahmed Mahmood Computer Engineering Department
Lecture 7
(H.W)
7- Write a program to print the following form:
14