0% found this document useful (0 votes)
6 views25 pages

Loops

loop

Uploaded by

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

Loops

loop

Uploaded by

Bala Abirupa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Loops

Need for loops


• repeat - repeats fixed number of time. Does
not checks or stops w.r.t a condition
• Everything done by repeat is possible by loops
and converse is not true.
• Looping statements:
Repeat, while, do while, for
The while statement
•Form: while (condition) body
1. Evaluate condition
2. If false, execution of statement ends
3. If true, execute body. Body can be a single
statement or a block, in which case all the
statement in the block will be executed.
4. Go back and execute from step 1
• The condition must eventually become false,
otherwise the program will never halt. Not
halting is not acceptable.
While flowchart
Example
int main()
{
int x=2;
while(x>0)
{
x--;
cout<<x<<endl;
}
cout<<"Done."<<endl;
return 0;
}
Flowchart ample
The break statement
• The break keyword is a statement by itself.
• When control reaches break statement, the
execution of ‘while’ statement which contains
it is terminated.
Example of break
Float nextmark, sum=0;
Int count=0;
While(true)
{cin>>nextmark;
If(nextmark<0)
break;
Sum+=nextmark;
Count++;
}cout<<sum/cont<<endl;
}
Explanation
• Condition for breaking = compliment of condition for
continuing loop
While(nextmark>0)
If(nextmark<0), both are compliments

Advantage:
No need of two cin statement
Disadvantage:
• Actual termination condition hides inside the body.
• If statement
• Many programmers dont prefer
The continue statement
• The continue is another single word statement
• If it is encountered in execution:
o The control directly goes to the beginning of
the loop for the next iteration
o Statement from the continue to the end of the
loop body are skipped
Example
Float nextmark, sum=0;
Int count=0;
While(true)
{cin>>nextmark;
If(nextmark>100) continue;
If(nextmark<0)
break;
Sum+=nextmark;
Count++;
}cout<<sum/cont<<endl;
}
Explanation
• If marks greater than 100 is entered then
execution of all other statement in the block is
ignored and control directly goes to ‘while’
statement.
do while
do
{
action 1;
}
while(condition is true)
action2
for statement- need
• Example: Write a program to print a table of cube of
numbers from 1 to 100.
Repeat
Int i=1;
Repeat (100)
{
Cout<<1<<‘ ’<< i*i*i<<endl;
i++
}
For:
For(int i=1; i<=100;i++)
Cout<<i<<‘= ‘<<i*i*i<<endl;
The for statement
• Form: for(initialization;condition;update) body
• Initialization, update: typical assignment (no
semi colon)
• Condition: boolean expression
Execution
• Before the iteration of the loop the
initialization is executed.
• Within each iteration:
o Condition is first tested
o If it fails, the loop execution ends
o If the condition succeeds, then the body is
executed.
o After that the update is executed. Then the next
iteration begins.
Flowchart
for statement - continued
• New variables can be defined in initialization.
These variables are defined inside the loop body,
including condition and update, but not outside.
• Variables defined outside can be used inside,
unless shadowed by new variables.
• Break and continue can be used with natural
interpretation.
• Typical use of ‘for’: a single variable is initialised
and updated, and the condition tests whether it
has reached a certain value. Such a variable is
called the control variable of the ‘for’ statement.
Euclid’s algorithm for GCD
• Greatest common divisor of positive integers
m,n: largest positive integer that divide both
m, n.
• ‘standard method’: factorize m & n and
multiply common factors
• Euclid’s algorithm (2300 years old) is different
and much faster.
• Program based on Euclid’s method will be
much faster than program based on factoring.
Euclid’s algorithm
• Basic observation: If d divided m, n, then d
divides m-n also assuming m>n
Proof: m=ad, n=bd so m-n=(a-b)d
• If d divides m-n and n, then it divides m too.
• Instead of finding GCD (m,n), we might as well
find GCD (n,m-n)
Example
GCD(3977, 943)
=GCD(3977-943, 943) =GCD(3034,943)
=GCD(3034-943,943)=GCD(2091,943)
=GCD(2091-943,943)=GCD(1148,943)
=GCD(1148-943,943)=GCD(205,943)
• 205 is just 3977%943
• So GCD(3977, 943) = GCD(3977%943, 943)
Contd
• Should we guess that GCD(m,n) =
GCD(m%n,n)?
• This is not true if m%n=0, since we have
defined GCD only for positive integers.
Euclid’s theorem: Let m, n>0 be positive
integers. If n divides m then GCD(m,n) =n.
Otherwise GCD(m,n) = GCD(m%n,n)
Example-contd
GCD(3977, 943)
GCD(3977%943)=GCD(205,943)
GCD(205,943%205)=GCD(205,123)
GCD(205%123,123)=GCD(82,123)
GCD(82,123%82)=GCD(82,41)
So GCD = 41 because 41 divides 82
Algorithm
• Input values: M, N which are stored in
variables m, n.
• Details of iteration:
o At the beginning, we have numbers stored in
m, n.
o If n divides m, then we declare n to be the
GCD.
o If n does not divide m, then we know that
GCD(M,N)=GCD(n, m % n)
Program for GCD
{
int m,n;
cin>>m>>n;
While(m%n!=0){
Int nextm=n;
Int nextn=m%n;
m=nextm;
n=nextn;
}
Cout<<n<<endl;
}
// To store n, m%n in m, n we cannot just write m=n and n=m%n

You might also like