Loops
Loops
Loops
Course
for
Beginners
Loops
By eng.Manar ElSheref
We will cover
PAGE 2
What’s loops?
*
*
cout<< “*”<<endl;
*
cout<<“*”<<endl; reduce
* cout<< “*”<<endl;
. Time and
*
. compiling
* Repeat this line 100 times
.
*
.
.
100 times!!!!!!
.
100 times!!!!!!
Why we need it?
mainly used to reduce the length of the code by executing the same
function multiple times and reducing the code’s redundancy
PAGE 5
Syntax:
for(initialization expression; test expression; update expression)
{
// statements to execute in the loop body
}
Initialization Expression:
Here we initialize the loop variable to a particular value. For example, int i=1;
Test Expression:
Here, we write the test condition.
If the condition is met and returns true,
we execute the loop body and update the loop variable. Otherwise,
we exit the For loop.
An example for test expression is i <= 5;
Update Expression:
PAGE 10
Multiplication
table
for (int tableNo= 1; tableNo <= 12; tableNo++)
{
cout << “this is table of “<<tableNo<<endl;
PAGE 11
While the loop is also an entry-controlled loop, we verify the
condition specified before running the loop.
The difference is that we use For loops when we know the
number of times the body of the loop needs to run, whereas we While
use while loops in circumstances when beforehand we do not
know the precise number of times the body of the loop needs to Loop
run.
The execution of the loop is terminated based on the test
condition.
Syntax:
initialization expression;
while (test_expression)
{
update_expression;
}
Example
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
PAGE 14
Do while loop is an exit controlled loop meaning the test condition is
verified after the execution
of the loop, at the end of the body of the loop. Hence, the body
executes at least once,
regardless of the test condition, whether it is true or false. In a while
loop,
Do While
the condition is tested beforehand, whereas in a do loop,
the condition is verified at the finish of the loop’s body.
Syntax:
Loop
initialization expression;
do { // statements to execute in the loop body
update_expression; }
while (test_expression);
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
PAGE 16
#include <iostream>
using namespace std;
int main()
{
int i = 2; // initialization expression
do {
cout << " Good morning\n";
i++; // update expression
} while (i < 1); // test expression
return 0;
}
PAGE 18
C++ break Statement
Example 1 Example 2
int main() int main()
{ {
for (int i = 1; i <= 5; i++) int number; int sum = 0;
{ // break condition while (true)
if (i == 3) { // take input from the user
{ cout << "Enter a number: ";
break; cin >> number;
} // break condition
cout << i << endl; if (number < 0)
} {
return 0; break;
} } // add all positive numbers
sum += number;
} // display the sum
cout << "The sum is " << sum << endl;
return 0; } PAGE 19
C++ continue Statement
In this tutorial,
we will learn about the continue statement
and its working with loops with the help of examples.
In computer programming,
the continue statement is used to skip the current iteration of the
loop and the control of the program goes to the next iteration.
The syntax of the continue statement is:
continue;
PAGE 20
C++ continue Statement
Example 1 Example 2
int main() int main()
{ {
for (int i = 1; i <= 5; i++) int sum = 0; int number = 0;
{ // condition to continue while (number >= 0) { // add all positive numbers
if (i == 3) sum += number; // take input from the user
{ cout << "Enter a number: ";
continue; cin >> number; // continue condition
} if (number > 50)
cout << i << endl; {
} cout << "The number is greater than 50 and won't be calculated." << endl;
return 0; number = 0; // the value of number is made 0 again
} continue;
}
} // display the sum
cout << "The sum is " << sum << endl;
return 0; }
PAGE 21
End of Session
PAGE 22