Computer Science Project

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 12

COMPUTER SCIENCE

PROJECT

THE LOOPS
The LOOPS (also called ITERATION STATEMENTS )
allow a set of instructions to be performed repeatedly
until a certain condition is fulfilled.

C++ provides three kinds of loops : 1.) FOR loop.


2.) WHILE loop.
3.) DO-WHILE loop.

Parts of a loop
1.) Initialization expression - Before entering a loop, its
control variable must be initialized. The initialization
of the control variable take place under initialization
expression. The initialization expression give the loop
variable their first value. The initialization expression
is executed only once, in the beginning of the loop.
2.) Test expression - The text is an expression whose
truth value decides whether the Loop-body will be
executed or not. If the test expression evaluates to
be true i.e 1, the loop-body gets executed , otherwise
the loop is terminated.

In an Entry-controlled loop, the test-expression is


evaluated before entering into a loop whereas in an exitcontrolled loop, the test-expression is evaluated before
exiting from the loop. In C++, the FOR loop and the while
loop are entry-controlled loops and do-while loop is exitcontrolled loop.
3.) Update expression - The update expression change the
value of loop variable. The update expression is executed;
at the end of the loop after the loop-body is executed.
4.) The body of the loop - The statements that are
executed repeatedly form the body of the loop. In an
entry controlled loop, first the text-expression is
evaluated and if it is nonzero, the body of the loop is
executed; if the text expression evaluates to be zero,

the loop is terminated. In an exit-controlled loop,


the body of the loop is executed first and then the
test-expression is evaluated. If it evaluates to be
zero, the loop is terminated, otherwise repeated.

THE FOR LOOP


The for loop is the easiest to understand of the C++
loops.
SYNTAX
for( initialization expression; test-expression; update expression)

Body of the loop;

Working of FOR loop :


for( i=1; i<=10; i++)
cout<<i<< ;

1.) Firstly initialization expression is executed (i.e i=1) .


2.) Then, the test-expression is evaluated (i<=10), which is
true
3.) since the test-expression is true, the body-of-the-loop ie
cout<<i<< ; is executed which prints the current value of
i.
4.) After executing the loop body, the update expression ie i++
is executed which increments the value of i.
5.) After the update expression is executed, the testexpression is again evaluated. If it is true, the sequence is
repeated from step no 3, otherwise the loop terminates.
OUTPUT OF THE ABOVE CODE :
1 2 3 4 5 6 7 8 9 10

PROGRAM TO PRINT FIRST n NATURAL NUMBERS


AND PRINT THEIR SUM
#include<iostream.h> // header file
#include<conio.h>
void main()
{ clrscr();
int i, sum, n;
cout<<\n how many natural numbers ? ;
cin>>n;
for (i=1,sum=0; i<=n; i++) // for loop
{
cout<<\n <<i;
sum= sum+i;
}
cout<<\n<<sum of the first <<n<< natural numbers is <<sum<<\n;
getch();
}

THE WHILE LOOP


The while loop in C++ is an entry-controlled loop.
SYNTAX
while (expression)
loop-body

where the loop body may contain a single statement, compound


statement or an empty statement. The loop iterates while
the expression evaluates to true. When the expression
becomes false, the program control passes to the line after
the loop-body code.
In a while loop control variable should be initialized before
the loop begins as an uninitialized variable can be used in an expression.
The loop variable should be updated inside the body of the loop.

PROGRAM TO CALCULATE THE FACTORIAL OF AN


INTEGER
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr():
unsigned long i, num, fact=1;
cout<<\n Enter integer ;
cin>>num;
i=num;
While (num)
{ fact= fact*num;
--num;
}
cout<<The factorial of <<i<< is <<fact<<\n;
getch();
}

Program to calculate and print the sum of even and odd


integers of first n natural numbers
#include<iostream.h>
#include<conio.h>
Void main()
{ clrscr();
int n, sumeven=0,sumodd=0,ctr=1;
Cout<<\n upto which natural number ;
Cin>>n;
While (ctr<=n)
{ if( ctr%2==0)
sumeven+=ctr;
else
sumodd+=ctr;
Ctr++;
Cout<<\n sum of even and odd integers are <<sumeven<< <<sumodd<<respectively;
Getch();
}

THE DO-WHILE LOOP


Unlike the for and while loops, the do-while loop is an exitcontrolled loop i.e it evaluates its test expression at the
bottom of the loop after executing its loop-body
statements. This means that the do-while loop always
executes at least once.
In the other two loops for and while, the
test-expression is evaluated at the beginning of the loop i.e
before executing the loop body. If the test-expression
evaluates to false for the first time itself, the loop is never
executed. But in some situations, it is wanted that the loop
body is executed at least once, no matter what the initial
state of the test-expression is. In such cases, the do-while
loop is the obvious choice.

SYNTAX :
{

do

statement;
} while (test-expression);
PROGRAM TO PRINT ALL THE UPPERCASE LETTERS
#include<iostream.h>
#include<conio.h>
Void main()
{ clrscr();
Char ch=A ;
Do
{
Cout<<\n<<ch;
Ch++;
}while (ch <= Z) ;
Getch();
}

You might also like