Loops in c language

TanmayModi3 526 views 23 slides Jul 25, 2019
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

C for Loop
Loops are used in programming to execute a block of code repeatedly until a specified condition is met. In this tutorial, you will learn to create for loop in C programming.

C programming has three types of loops:

for loop
while loop
do...while loop


Slide Content

Loops in C

Definition In any programming language, loops are used to execute a set of statements repeatedly until a particular condition is satisfied . The  loops in C language  are used to execute a block of code or a part of the program several times. In other words, it iterates a code or group of code many times.

Why use loops in C language? Suppose that you have to print table of 2, then you need to write 10 lines of code. By using the loop statement, you can do it by 2 or 3 lines of code only . Advantage of loops in C 1) It  saves code . 2) It helps to traverse the elements of array.

How it works? A sequence of statements are executed until a specified condition is true. This sequence of statements to be executed is kept inside the curly braces { } known as the  Loop body . After every execution of loop body, condition is verified, and if it is found to be  true  the loop body is executed again. When the condition check returns  false , the loop body is not executed.

Types of Loops in C There are 3 types of loops in C language: while  loop for  loop do-while  loop

While Loop while  loop can be addressed as an  entry control  loop. It is completed in 3 steps: Variable initialization.( e.g int x=0; ) condition( e.g while( x<=10) ) Variable increment or decrement ( x++ or x-- or x=x+2 ) Syntax:

Flowchart:

Example Program to print first 10 natural numbers

Program to print table for the given number using while loop #include < stdio.h >     #include < conio.h >     void main(){     int   i =1,number=0;   clrscr ();        printf ("Enter a number: ");   scanf ("% d",&number );      while( i <=10 ) {    printf ("%d \n",(number* i ));   i ++;   }      getch ();     }     

Infinitive while loop in C If you pass 1 as a expression in while loop, it will run infinite number of times.

For Loop for  loop is used to execute a set of statements repeatedly until a particular condition is satisfied. we can say it an  open ended loop.   Syntax:

In  for  loop we have exactly two semicolons, one after initialization and second after condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator.  for  loop can have only one  condition . The  for  loop is executed as follows: It first evaluates the initialization code. Then it checks the condition expression. If it is true, it executes the for-loop body. Then it evaluate the increment/decrement condition and again follows from step 2. When the condition expression becomes false, it exits the loop.

Flowchart:

Example: Program to print first n natural numbers

Print table for the given number using for loop # include < stdio.h >     #include < conio.h >     void main(){     int   i =1,number=0;   clrscr ();        printf ("Enter a number: ");   scanf ("% d",&number );      for( i =1;i<=10;i ++) {      printf ("%d \n",(number* i ));   }      getch ();     }    

Nested For loop We can also have nested  for  loops, i.e one  for  loop inside another  for  loop.  Syntax:

Example: Program to print half pyramid of numbers

Infinitive for loop in C If you don't initialize any variable, check condition and increment or decrement variable in for loop, it is known as infinitive for loop. In other words, if you place 2 semicolons in for loop, it is known as infinitive for loop . If you run this program, you will see above statement infinite times.

Do While Loop In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of  do-while  loop.  do  statement evaluates the body of the loop first and at the end, the condition is checked using  while  statement. It means that for at least one time ,the body of the loop will be executed, even though the starting condition inside while is initialized to false.   Syntax:

Flowchart:

Example: Program to print first ten multiples of 5.

Program to print table for the given number using do while loop #include < stdio.h >     #include < conio.h >     void main(){     int   i =1,number=0;   clrscr ();        printf ("Enter a number: ");   scanf ("% d",&number );      do{   printf ("%d \n",(number* i ));   i ++;   } while( i <=10);        getch ();     }    

Infinitive do while loop If you pass 1 as a expression in do while loop, it will run infinite number of times .