Loops

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

C++

Course
for
Beginners
Loops
By eng.Manar ElSheref
We will cover

What’s loops ? Why we need it?


For loop
Loops in C++
While and do while loop
Break and continue

PAGE 2
What’s loops?

Loop statements in C++ execute a certain block of the code or


statement multiple times

*
*
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:

Once the body of the loop has been executed,


we increment or decrement the value of the loop variable in the update expression
. For example, i++;
Let us look at an example of For loop:
#include <iostream>
using namespace std;
int main() Good morning
{ Good morning
for (int i = 0; i < 4; i++) { Good morning
cout << " Good morning \n"; Good morning
}
return 0; }

Statement 1 sets a variable before the loop starts (int i = 0).


Statement 2 defines the condition for the loop to run (i must be less than 4). If the condition is
true, the loop will start over again, if it is false, the loop will end.
Statement 3 increases a value (i++) each time the code block in the loop has been executed.
for (int i = 0; i < 5; i++)
{ 0
cout << i << "\n"; 1
} 2
3
Initialization Test condition counter 4
Try Try Try
i<=0 i<=0 i+=2
i>5 i>5 i=i+10
i==5 i==5
Cin>>range; //control max number to use
for (int i = 0; i <=range; i = i + 1)
{
cout << i << "\n";
}
Cin>>inc; //control counter step
for (int i = 0; i <= 10; i = i + inc)
{
cout << i << "\n";
}
____________________________________
PAGE 9
for (int x = 0; x <= 10; x++)
Nested
{ for loop
cout<<‘this is loop1’<<x<<endl;
for (int y = 0; y <= 10; y++)
{

cout <<‘this is loop2’<<y<<endl;


}
}

PAGE 10
Multiplication
table
for (int tableNo= 1; tableNo <= 12; tableNo++)
{
cout << “this is table of “<<tableNo<<endl;

for (int i = 1; i <= 12; i++)


{
cout << tableNo << " * " << i << " = " << tableNo * i << 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)
{

// statements to execute in the While loop


loop body

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);

In do while loop,we end the body of the loop with a semicolon,


whereas the other two loops do not have any semicolon to end the
body of their loops.
Example

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;
}

In the above-given code, the test condition says I should


be less than 1 (i<1), but still, the loop executes at least
onc, before checking for the condition, hence giving us
the output “Good morning” one time.
C++ break Statement
In this tutorial,
we will learn about the break statement
and its working in loops with the help of examples.
In C++, the break statement terminates the loop when it is
encountered.
The syntax of the break statement is:
break;

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

You might also like