You are on page 1of 17

Vidhyadeep institute of

management and
technology,anita.
Sub:- Computer programming and utilization

Name:-

Enrollment no:-

Patel Foram

140940107030

Kumbhani Nidhi

140940107020

Guide by :- Ass.Prof Hitesh A Ravani


Ass.Prof Bhavin N Patel
2004 Pearson Addison-Wesley. All rights reserved

5-1

Content

2004 Pearson Addison-Wesley. All rights reserved

5-2

Repetition Statements
Repetition statements allow us to execute a
statement multiple times

Often they are referred to as loops


Like conditional statements, they are controlled by
boolean expressions

Java has three kinds of repetition statements:


the while loop
the do loop
the for loop

2004 Pearson Addison-Wesley. All rights reserved

5-3

The while Statement


A while statement has the following syntax:
while ( condition ){
Body of the loop
}
Statement-x;
If the condition is true, the statement is
executed
Then the condition is evaluated again, and if it is
still true, the statement is executed again

The statement is executed repeatedly until the


condition becomes false
2004 Pearson Addison-Wesley. All rights reserved

5-4

Logic of a while Loop

condition
evaluated
true

false

statement

2004 Pearson Addison-Wesley. All rights reserved

5-5

The while Statement


An example of a while statement:
int count = 1;
while (count <= 5){
System.out.println (count);
count++;
}

If the condition of a while loop is false initially, the


statement is never executed

Therefore, the body of a while loop will execute


zero or more times
2004 Pearson Addison-Wesley. All rights reserved

5-6

An example while loop:#include <stdio.h>

Output:-

#include<conio.h>

How many number:3

Void main()

enter any number:4

enter any number:5


int n,a,b,i=1,sum=o;

enter any number:6

printf(how many number\n);

sum=15

scanf(%d,&n);
while(i<=n)
{printf(enter the value of n\n);
scanf(%d,&a);
sum+=a;

i+=1
}
Printf( sum=%d,sum\n);
}
2004 Pearson Addison-Wesley. All rights reserved

5-7

The do-while Statement


A do-while statement (also called a do loop) has
the following syntax:
Do
{
Body of the loop
}while ( condition );
Statement-x;
The statement is executed once initially, and then
the condition is evaluated

The statement is executed repeatedly until the


condition becomes false
2004 Pearson Addison-Wesley. All rights reserved

5-8

Logic of a do-while Loop

statement
true
condition
evaluated
false

2004 Pearson Addison-Wesley. All rights reserved

5-9

An example of do-while loop


include<stdio.h>

#include<conio.h>

Output:-

Void main()

enter a number 123

reverse of the number 123 is 321


int num,xnum,temp,rev=0;
printf(enter a number\n);
scanf(%d,&num);

xnum=num;
do
{ temp=xnum%10;
rev=(rev*10)+temp;

xnum=xnum/10;}
while(xnum!=0);
printf(reverse of the number%d is %d,num,rev);
}
2004 Pearson Addison-Wesley. All rights reserved

5-10

Comparing while and do


The while Loop

The do Loop

statement

condition
evaluated
true
statement

2004 Pearson Addison-Wesley. All rights reserved

true
false

condition
evaluated
false

5-11

The for Statement


A for statement has the following syntax:
The initialization
is executed once
before the loop begins

The statement is
executed until the
condition becomes false

for ( initialization ; condition ; increment ){


Body of the loop
}
The increment portion is executed at
the end of each iteration

2004 Pearson Addison-Wesley. All rights reserved

5-12

Logic of a for loop


initialization
condition
evaluated
true

false

statement
increment

2004 Pearson Addison-Wesley. All rights reserved

5-13

The for Statement


A for loop is functionally equivalent to the
following while loop structure:
initialization;
while ( condition ){
statement;
increment;
}

-The initialization section can be used to declare a


variable.
-Therefore, the body of a for loop will execute
zero or more times.

2004 Pearson Addison-Wesley. All rights reserved

5-14

An example for for loop


#include<stdio.h>
Output:#include<conio.h>
enter any two num
Void main()
20
{
12
int a,b,I,sub;
subtration of two num=8
printf(enter any two num\n);
scanf(%d%d,&a,&b);
for(i=1;i<=b;i++)
{ a--;
printf(subtraction of two numbers=%d,a);
}

2004 Pearson Addison-Wesley. All rights reserved

5-15

Nested for Loops


Syntax:For(i=1;i,<5;i++)
{
for(j=1;j<5;j++)
{
body of loop
}
}

2004 Pearson Addison-Wesley. All rights reserved

5-16

2004 Pearson Addison-Wesley. All rights reserved

5-17