0% found this document useful (0 votes)
9 views

C++ Loops Preentation

c++ loops

Uploaded by

danyalkhattak739
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C++ Loops Preentation

c++ loops

Uploaded by

danyalkhattak739
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

TOPIC:

Loops and its Types


Group members
HADIA INAM (5112124006)
HUZAIFA FAIZAN (5112124032)
ZAHID JAMIL (5112124011)
MOHIB UL HASSAN (5112124015)
Introduction
Overview:
Importance of loops
Loops types
Loops:
what is loop?
A loop statement allows us to execute a statement or group
of statements multiple times..
There are some common loops:
 For loop
 While loop
 Do-while-loop
 Nested loop
For Loop (Overview)
 Definition: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:
for (initialization; condition; increment)
{
// code block to be executed
}
Example:
(int i = 1; i <= 5; i++)
{
cout << i << " ";
}
cout<<endl;
For loop example;

#include <iostream>
using namespace std;
int main ()
{
// for loop execution
for( int a = 1; a < =20; a ++)
{
cout << "value of a: " << a << endl;
}
return 0;
Practice Exercise:

Write a program to print the numbers


in reverse order(15 to 1).
Write a program to calculate the sum
of first 50 natural numbers.
.
Example code:
 First example code.

Second example code.


While Loop:
Definition and Examples
 Definition:
A while loop is a control flow statement that repeatedly executes a
block of code as long as a given condition remains true.
Syntax:
while (condition)
{
//Code to be executed
}
Example:
int i = 0;
while (i < 10) {
cout << "i: " << i << endl; // Code to be executed
i++ ; // Increment
}
Example of while loop:
#include <iostream>
using namespace std;
int main ()
{
int a=2;
while( a <=50 )
{
cout << "value of a: " << a << endl;
a=a+2;
}
return 0;
}
Practice Exercise:

Write a program to calculate the sum of


all numbers from 1 to 100 using a while
loop.
Write a program to print the numbers
from 1 to 10 in reverse order using only a
while loop.
Example code;
First example code

Second example code.


Do-While Loop (Overview)
Definition:
Similar to a while loop, but the condition is evaluated after the
execution of the loop body.
Syntax:
do
{
// code block to be executed
} while (condition);
Example;
int i = 0;
do
{
cout << i << "\n"; i++;
} while (i < 5);
Example of do while loop:
#include<iostream>
using namespace std;
int main ()
{
int a = 1;
do
{
cout<< a << endl;
a = a + 1;
}
while( a < =20 );
return 0;
}
Write a program to sum of first 50 natural numbers.
Practice Question:

Write a program to sum of first 50


natural numbers.
Example code:
Nested Loops (Overview)
Definition:
A loop inside another loop.
Syntax:
for (int i = 0; i < 3; i++)
{ for (int j = 0; j < 3; j++)
{
// code block to be executed
}
}
Example;
for (int i = 1; i <= 3; i++)
{ for (int j = 1; j <= 3; j++)
{
cout << i << "," << j << "\n";
}
}
Example of nested loop:
void main() {
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<”*”;
}
cout<<endl; }
getch();
}
Practice question:

Write a C++ program that will print the following


pattern.
*****
****
***
**
*
Example code:
Thanks

You might also like