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

National Institute of Electronics & Information Technology

Gorakhpur Center
Ministry of Electronics & Information Technology (MeitY), Government of India

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Contents to be covered
• What is Loops:
• While loop, For loop, Do_while loop
• break and continue statements with examples

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Loops

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Loops Contd..

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Loops Contd..

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


While Loop

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example of While Loop
#include<stdio.h>
#include<conio.h>
main()
{
False int i=1;
clrscr();
True while(i<=10)
{
printf("\n%d",i);
i++;
}
getch();
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


For Loop

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example of for Loop
#include<stdio.h>
#include<conio.h>
main()
False {
int i;
True clrscr();
for(i=1;i<=10;i++)
printf("\n%d",i);
getch();
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Do while Loop

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example of do while Loop
#include <stdio.h>
main()
{
int digit = 0;
do
{
printf(“%d”, digit++);
}while(digit<=9);
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Break Statement
 Sometimes, it is necessary to exit immediately from a loop as soon as
the condition is satisfied.
 When break statement is used inside a loop, then it can cause to
terminate from a loop.
 The statements after break statement are skipped.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example of break statement
#include <stdio.h>
main ()
{
int a = 10;
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
if( a > 15)
break;
}
}
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Continue Statement
 Sometimes, it is required to skip a part of a body of loop under specific
conditions.
 So, C supports 'continue' statement to overcome this problem.
 The working structure of 'continue' is similar as that of that break
statement but difference is that it cannot terminate the loop.
 Continue statement simply skips statements and continues next iteration.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example of Continue statment
#include <stdio.h>
main ()
{
int a = 10;
do
{
if( a == 15)
{
a = a + 1;
continue;
}
printf("value of a: %d\n", a);
a++;
} while( a < 20 );
} http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

1. The C code ‘for(;;)’ represents an infinite loop. It can be terminated by ___________


a) break b) exit(0)
c) abort() d) terminate Answer: a

2. Which for loop has range of similar indexes of ‘i’ used in for (i = 0;i < n; i++)?
a) for (i = n; i>0; i–) b) for (i = n; i >= 0; i–)
c) for (i = n-1; i>0; i–) d) for (i = n-1; i>-1; i–) Answer: d
Explanation: None.

3. Which of the following cannot be used as LHS of the expression in for (exp1;exp2; exp3)?
a) variable b) function
c) typedef d) macros Answer: d

4. What is an example of iteration in C?


a) for b) while
c) do-while d) all of the mentioned Answer: d

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

5. Which loop is most suitable to first perform the operation and then test the condition?
a) for loop b) while loop
c) do-while loop d) none of the mentioned Answer: c

6. How many times i value is checked in the following C code?


#include <stdio.h>
int main()
{
int i = 0;
while (i < 3)
i++;
printf("In while loop\n");
}
a) 2 b) 3
c) 4 d) 1 Answer: c

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

7. The keyword ‘break’ cannot be simply used within _________


a) do-while b) if-else
c) for d) while Answer: b

8. Which keyword is used to come out of a loop only for that iteration?
a) break b) continue
c) return d) none of the mentioned Answer: b

9. What will be the output of the following C code?


#include <stdio.h>
int main()
{
printf("before continue ");
continue;
printf("after continue\n");
}
a) Before continue after continue b) Before continue
c) After continue d) Compile time error Answer: d
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

10. What will be the output of the following C code?


#include <stdio.h>
void main()
{
int i = 0;
for (i = 0;i < 5; i++)
if (i < 4)
{
printf("Hello");
break;
}
}
a) Hello is printed 5 times b) Hello is printed 4 times
c) Hello d) Hello is printed 3 times Answer: c

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Assignments
 Program to print numbers between 1 to 10 by using while and for loop.
 Program to print even and odds numbers between two values input by user.
 Program to check whether a number is prime or not.
 Program to print all prime numbers between two values input by user.
 Program to print Fibonacci Series upto 10 terms.
 Program to check whether a number is palindrome or not.
 Program to check whether a number is Armstrong or not.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


References

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Thank You
Any Query

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia

You might also like