0% found this document useful (0 votes)
21 views18 pages

While Loop Basics and Examples

The document explains the use of loops, specifically the 'while' loop, in programming to calculate sums and factorials. It provides examples of summing integers up to a specified limit and calculating the factorial of a number. Additionally, it outlines the properties of the 'while' statement and includes flowchart representations for better understanding.

Uploaded by

murtaza786sahito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views18 pages

While Loop Basics and Examples

The document explains the use of loops, specifically the 'while' loop, in programming to calculate sums and factorials. It provides examples of summing integers up to a specified limit and calculating the factorial of a number. Additionally, it outlines the properties of the 'while' statement and includes flowchart representations for better understanding.

Uploaded by

murtaza786sahito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Loop - Repetition

structure
Example

int sum ;
sum = 1+2+3+4+5+……..+10 ;
cout << sum ;
Find the Sum of the first 100
Integer starting from 1

?
while
while ( Logical Expression )
{
statements; :
}
int sum ;
sum = 0 ;
int sum = 0;
( Optional )
Example
int sum , number ;
sum = 0 ;
number = 1 ;
while ( number <= 1000 )
{
sum = sum + number ;
number = number + 1 ;
}
cout << “ The sum of the first 1000 integer starting from 1 is ”
<< sum ;
while (number <=
UpperLimit)
Example
int sum, number , UpperLimit ;
sum = 0 ;
number = 1 ;
cout << “ Please enter the upper limit for which you want the sum
;
cin >> UpperLimi t;
while (number <= UpperLimit)
{
sum = sum + number ;
number = number +1 ;
}
cout << “ The sum of the first ” << UpperLimit << “ integer is ” <
sum ;
if ( number % 2 == 0 )
{
sum = sum +
number ;
number = number + 1
;
}
Example
sum = 0;
number = 1;
cout << “ Please enter the upper limit for which you want the sum ”;
cin >> UpperLimit;
while (number <= UpperLimit)
{
if (number % 2 == 0)
{
sum = sum + number;
number = number + 1;
}
}
cout << “ The sum of all even integer between 1 and ” << UpperLimit <<
“ is” << sum;
2 * ( number / 2 ) ;

?
int Junk ;
Junk = 1 ;
while ( Junk <= UpperLimit ) ( infinite loop ) X
{
sum = sum + number ;
number = number + 1 ;
}
Flow Chart for While
Construct
WHILE Statement

Entry point for WHILE block

Condition is No
While Exit
true?

Process

Exit point for WHILE block


Factorial
Definition

n! = n*(n-1)*(n-2)*(n-3)…………*3*2*1
Example:
Factorial
#include <iostream.h>
main ( )
{
int number ;
int factorial ;
factorial = 1 ;
cout << “Enter the number of Factorial” ;
cin >> number ;
while ( number >= 1 )
{
factorial = factorial * number ;
number = number – 1 ;
}
cout << “Factorial is” << factorial ;
}
Property of While
Statement

It executes zero or more


times

You might also like