Loops PDF
Loops PDF
com
LOOPS
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
The while loop can be used if you don’t know how many times a loop must
.s
#include <stdio.h>
w
int main()
w
{
int counter, howmuch;
scanf (“%d”,&howmuch);
counter = 0;
1
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
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)
{
2
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
3
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
count = count+1;
a
}
.s
}
w
Output:-
w
4
www.sakshieducation.com
www.sakshieducation.com
Flow chart
m
co
n.
io
at
uc
ed
hi
a ks
.s
w
w
w
5
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
scanf (“%d”,&howmuch);
a
counter = 0;
.s
do
w
{
w
counter++;
printf(“%d\n”,counter);
w
}
while(counter<howmuch);
6
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
main ( )
w
{
int p, n, count ;
float r, si ;
7
www.sakshieducation.com
www.sakshieducation.com
m
}
co
}
n.
Flow chart:-
io
at
uc
ed
hi
a ks
.s
w
w
w
If this program is compared with the one written using while, it can be
seen that the three steps—initialization, testing and incrementation
8
www.sakshieducation.com
www.sakshieducation.com
required for the loop construct have now been incorporated in the for
statement.
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
• 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
While loop
Used for looping until a condition is satisfied and when it is unsure how
many times the code should be in loop.
9
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
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
d) Compilation error
#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
4.
.s
void main(){
int check=2;
w
switch(check){
case 1: printf("A");
w
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
#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
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