0% found this document useful (0 votes)
103 views

Loops PDF

Loops allow code to be repeated. There are three types of loops: 1. While loops repeat code while a condition is true. 2. For loops repeat code a specified number of times, initializing and incrementing a counter. 3. Do-while loops are similar to while loops but run the code block once before checking the condition.

Uploaded by

Racer Govardhan
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)
103 views

Loops PDF

Loops allow code to be repeated. There are three types of loops: 1. While loops repeat code while a condition is true. 2. For loops repeat code a specified number of times, initializing and incrementing a counter. 3. Do-while loops are similar to while loops but run the code block once before checking the condition.

Uploaded by

Racer Govardhan
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/ 15

www.sakshieducation.

com

LOOPS

REPETITION OR LOOP CONTROL INSTRUCTION


Introduction
If you have to perform an action over and over, often

m
with variations in the details each time, the

co
mechanism which meets this need is the ‘loop’.
The versatility of the computer lies in its ability to

n.
perform a set of instructions repeatedly. This

io
involves repeating some portion of the program
either specified number of times or until a particular condition is being

at
satisfied. This repetitive operation is done through a loop control
uc
instruction.
ed

To repeat a part of program there are three methods,


a. Using WHILE loop.
hi

b. Using FOR loop.


ks

c. Using DO-WHILE loop.


While Loop
a

The while loop can be used if you don’t know how many times a loop must
.s

run. Here is an example:


w

#include <stdio.h>
w

int main()
w

{
int counter, howmuch;
scanf (“%d”,&howmuch);
counter = 0;

 
www.sakshieducation.com
www.sakshieducation.com

while(counter<howmuch)
{
counter++;
printf(“%d\n”,counter);
}
return 0 ;

m
}

co
n.
Let’s take a look at the example: First you must always initialize the

io
counter before the while loop starts (counter = 1). Then the while loop
will run if the variable counter is smaller then the variable “howmuch”.

at
If the input is ten, then 1 through 10 will be printed on the screen. A last
uc
thing you have to remember is to increment the counter inside the loop
(counter++). If you forget this the loop becomes infinitive.
ed

As said before (after the for loop example) it makes a difference if prefix
hi

incrementing (++i) or postfix incrementing (i++) is used with while


ks

loop. Take a look at the following postfix and prefix increment while loop
example:
a
.s

int main()
w

{
w

Int I;
w

I = 0;
while(i++<5)
{

 
www.sakshieducation.com
www.sakshieducation.com

printf(“%d\n”,i);
}
printf(“\n”);
I = 0;
while(++i<5)
{

m
printf(“%d\n”,i);

co
}

n.
return 0;

io
at
}
uc
Output :-
1
ed

2
3
hi

4
ks

5
a

1
.s

2
w

3
w

4
w

• i++ will increment the value of i, but is using the pre-incremented


value to test against < 5. That’s why we get 5 numbers.

 
www.sakshieducation.com
www.sakshieducation.com

++i will increment the value of i, but is using the incremented value
to test against < 5. That’s why we get 4 numbers.

Example 2
Calculation of simple interest:-

m
main ()

co
{

n.
int p,n,count;
float r,si;

io
count = 1;

at
while (count <=3)
uc
{
printf (“\n enter values of p,n,r”);
ed

scanf(“%d%d%f”,&p,&n,&r);
si = p*n*r;
hi

prinf (“simple interest is =%f”,si);


ks

count = count+1;
a

}
.s

}
w

Output:-
w

Enter values of p,n,r 1000,5,13.5


Simple interest is = 675.000000.
w

 
www.sakshieducation.com
www.sakshieducation.com

Flow chart

m
co
n.
io
at
uc
ed
hi
a ks
.s
w
w
w

Figure 4.4.2:-Flow chart for the Example 2

 
www.sakshieducation.com
www.sakshieducation.com

Do –While
The “do while loop” is almost the same as the while loop. The “do while
loop” has the following form:
Syntax:-
do

m
{
Do something;

co
}

n.
while (expression);
Do something first and then test if we have to continue. The result is that

io
the loop always runs once.

Example:-
at
uc
#include <stdio.h>
ed

int main()
{
hi

int counter, howmuch;


ks

scanf (“%d”,&howmuch);
a

counter = 0;
.s

do
w

{
w

counter++;
printf(“%d\n”,counter);
w

}
while(counter<howmuch);

 
www.sakshieducation.com
www.sakshieducation.com

return 0 ;
}

“for” Loop

m
The “for loop” loops from one number to another number and increases

co
by a specified value each time. The “for loop” uses the following
structure:

n.
Syntax:-

io
at
for(initialize counter ; test counter ; increment counter)
uc
{
ed

Do this;

Do this;
hi

Do this;
ks

}
a
.s

Example:-
w

The program for calculating simple interest,


w

main ( )
w

{
int p, n, count ;
float r, si ;

 
www.sakshieducation.com
www.sakshieducation.com

for ( count = 1 ; count <= 3 ; count = count + 1 )


{
printf ( "Enter values of p, n, and r " ) ;
scanf ( "%d %d %f", &p, &n, &r ) ;
si = p * n * r / 100 ;
printf ( "Simple Interest = Rs.%f\n", si ) ;

m
}

co
}

n.
Flow chart:-

io
at
uc
ed
hi
a ks
.s
w
w
w

Figure 4.4.4:- Flow chart for the above example

If this program is compared with the one written using while, it can be
seen that the three steps—initialization, testing and incrementation

 
www.sakshieducation.com
www.sakshieducation.com

required for the loop construct have now been incorporated in the for
statement.

Let us now examine how the for statement gets executed:


• When for statement is executed for the first time, the value of count
is set to an initial value 1.

m
• Now the condition count <= 3 is tested. Since count is 1 the
condition is satisfied and the body of the loop is executed for the first

co
time.

n.
• Upon reaching the closing brace of for, control is sent back to the for

io
statement, where the value of count gets incremented by 1.
• Again the test is performed to check whether the new value of count
exceeds 3.
at
uc
• If the value of count is still within the range 1 to 3, the statements
within the braces of for are executed again.
ed

• The body of the for loop continues to get executed till count doesn’t
hi

exceed the final value 3.


ks

• When count reaches the value 4 the control exits from the loop and
is transferred to the statement (if any) immediately after the body of
a

for.
.s
w

Note
w

Difference between for loop and while loop


w

While loop
Used for looping until a condition is satisfied and when it is unsure how
many times the code should be in loop.

 
www.sakshieducation.com
www.sakshieducation.com

For loop
Used for looping until a condition is satisfied but it is used when you
know how many times the code needs to be in loop.

TEST

m
1. What will be output when you will execute following c code?

co
#define PRINT printf("Siddhartha");printf(" Singh");

n.
#include<stdio.h>

io
void main(){

at
int x=1;
if(x--)
uc
PRINT
ed

else
printf("welcome");
hi

}
ks

a. siddhartha Singh
b. siddhartha
a

c. welcome
.s

d. compilation error.
w

2. What will be output when you will execute following c code?


w
w

#define True 5==5


#include<stdio.h>
void main(){
if(.001-0.1f)
10 

 
www.sakshieducation.com
www.sakshieducation.com

printf("Hello");
else if(True)
printf("IIIT");
else
printf("HYDERABAD");
}

m
OPTIONS

co
A. Hello
B. IIIT

n.
C. Hyderabad

io
D. warning: condition is always true

at
E. warning :unreachable code
3. What is the output of the following code?
uc
#include<stdio.h>
void main()
ed

{
if(sizeof(void))
printf ("Hello");
hi

else
ks

printf("IIIT");
}
OPTIONS:-
a
.s

a) Hello
w

b) IIIT
w

c) Warning, condition is always false.


w

d) Compilation error

3. Find the output of the given program below ?

#include <stdio.h>
11 

 
www.sakshieducation.com
www.sakshieducation.com

void main()
{
int ch;
printf("enter a value btw 1 to 2:");
scanf("%d", &ch);
switch (ch, ch + 1,ch+3,ch+5)
{
case 1:

m
printf("1\n");
break;

co
case 2:
printf("2");

n.
break;
case 6:

io
printf("6");
break;

at
default:
printf("choice not valid");
uc
}
}
Options :-
ed

(a) 1
(b) 2
hi

(c) 1 2
(d) 6
ks

(e) Compilation error


a

4.
.s

4 What will be output when you will execute following c code?


#include<stdio.h>
w

void main(){
int check=2;
w

switch(check){
case 1: printf("A");
w

case 2: printf(" B");


case 3: printf(" C");
default: printf(" c");
}
}
12 

 
www.sakshieducation.com
www.sakshieducation.com

Options:
a. A
b. B
c. ABC
d. none of these
5. What will be output when you will execute following c code?

m
#include<stdio.h>
void main(){

co
int const X=0;
switch(5/4/3){

n.
case X: printf("Clinton");
break;

io
case X+1:printf("Gandhi");
break;

at
case X+2:printf("Gates");
break;
uc
default: printf("Brown");
}
}
ed

a. Clinton
b. Gandhi
hi

c. Gates
d. compilation error
ks

6 What will be output of following c code?


a
.s

#include<stdio.h>
int main(){
w

int x=011,i;
w

for(i=0;i<x;i+=3){
w

printf("Start ");
continue;
printf("End");

13 

 
www.sakshieducation.com
www.sakshieducation.com

}
return 0;
}

OPTIONS:-

m
a. start

co
b. start end
c. startstartend

n.
d. startstartstart

io
7. What will be output of following c code?

at
#include<stdio.h>
uc
int i=40;
extern int i;
ed

int main(){
do{
hi

printf("%d",i++);
ks

}
a

while(5,4,3,2,1,0);
.s

return 0;
w

}
w

Answer ?
w

14 

 
www.sakshieducation.com
www.sakshieducation.com

8.Write a program to print the following below pattern

1
2 3
4 5 6
7 8 9 10
9. Write a program to find whether the given number is
Armstrong or not?

m
10. Write a program to find whether a given number is

co
palindrome or not.?

n.
io
at
uc
ed
hi
a ks
.s
w
w
w

15 

 
www.sakshieducation.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy