Thursday, 25 September 2014

18:01

COMPUTER FUNDAMENTALS
LAB REPORT #09
Introduction to Loops

 Objectives:
Familiarization with Loops in C++ Programming
1.      for Loop
2.      while Loop
3.      do while loop
4.      nested Loops

Loop:
Loops in any programming language are simply the repetition of execution of sets of statements. There are number of loop types that we can use to introduce repetition

A loop is a control structure that executes a block of code repeatedly until the logical expression of the loop evaluates to false (or zero in C++).

 A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages:
                                                   



Types of loops:
There are three types of loops:
  • while
  • do while
  • for
  • nested
for loop
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 ( init; condition; increment )
{
statement(s);
}


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


While Loop:
A while loop statement repeatedly executes a target statement as long as a given condition is true.


Syntax of while loop
while(condition)
{
statement(s);
}
Example:

#include <iostream>
using namespace std;
int main ()
{
// Local variable declaration:
int a = 10;
// while loop execution
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
return 0;
}


Do while loop
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time
Syntax:
The syntax of a do...while loop in C++ is:
do
{
statement(s);
}while( condition );




Nested Loops
A nested loop is a loop within a loop, an inner loop within the body of an outer one. How this works is that the first pass of the outer loop triggers the inner loop, which executes to completion. Then the second pass of the outer loop triggers the inner loop again. This repeats until the outer loop finishes. Of course, a break within either the inner or outer loop would interrupt this process.
Example:
#include <iostream>
using namespace std;
int main ()
{
int i, j;
for(i=2; i<100; i++) {
for(j=2; j <= (i/j); j++)
if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) cout << i << " is prime\n";
}
return 0;
}

OUTPUT:
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime


Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 All in One. All rights resevered. Designed by Templateism

Back To Top