Tuesday, 7 June 2016

Introduction to Loops


Introduction:


Loop is basically a continues repetition of something , they are very important part of programming to be a good programmer one must have a good grip on loop logic building.So to develop and enhance the logic building of loop one must practice on patterns.

There are Three Types of loops:
1) For Loop
2) While
3) Do-While

1)For loop:

For loop loops are finite loop limits are defined in it.

Syntax:

1
2
3
4
for(int i=0/*from where the loop should start*/;i<10/*condition for the loop loop will keep on running until this condition satisifies*/;i++/*how much loop should increment value well in this case there is an increment if one but it can be two three and so on.*/)
{
//body of loop( what you want to to inside loop)
}



2)While Loop:

While loop is an infinite loop all you need to provide is condition,it can work as a finite loop too.

Syntax:

1
2
3
4
5
while(i<10/*condition ,loop will keep o running until condition returns false*/)
{
//body of the loop you may write the increment of loop here e.g.
i++;
}

3)Do while

It works same like while loop the only difference is that in" while loop" condition is checked first then the working in body of loop works ,while in "Do while" body of loop works first and condition is checked in the end.This makes loop little bit complicated but good news is that beginners won't be using this loop frequently.

Syntax:


1
2
3
4
5
6
do
{
//body of the loop you may write the increment of loop here e.g.
i++;
}
while(i<10/*condition ,loop will keep o running until condition returns false*/);

Examples:

Q)Print N Numbers Using loop.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include<stdio.h>
int main()
{
 
 int numbers;
 printf("Enter up to which number you want to Print: ");
 scanf("%d",&numbers);//Takes Input of up to where numbers should be printed
 for(int i=1;i<=numbers;i++)
 {/*Loop Will first satisfy the condition lets suppose we input 10 it we compare the condition initially as i=1 so value
 of i=1 so 1<=10 answer is yes loop will work and print the value 1 now in next step it will incremnt the value by 1
 so now value of i=2 ,2<=10 answer is yes it will move in body and will print the value i.e. 2. This will keep on running
 until value of i becomes 11 11<=10 answer is No so condition fails and program will come out of loop.*/
  printf("%d\n",i);
 }
 
 
}

Q) Print N Even Numbers using loop.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
int main()
{
 
 int numbers;
 int even=2;
 printf("Enter Number of Even Numbers to print: ");
 scanf("%d",&numbers);//Takes Input of up to where numbers should be printed
 int i=1;
       
 while(i<=numbers)
 {/*Loop Will first satisfy the condition lets suppose we input 10 it we compare the condition initially as i=1 so value
 of i=1 so 1<=10 answer is yes loop will work and print the value 2 now in next step it will increment the value of i by 1
 so now value of i=2 and will increment the value of even by 2 so now value of even is 4,2<=10 answer is yes 
 it will move in body and will print the value i.e. 4. This will keep on running until value of i becomes 11 11<=10 
 answer is No so condition fails and program will come out of loop.*/
    
  printf("%d\n",even);
  even=even+2;
  i++;
 }
 
} 


Now Lets Do it using do while.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
int main()
{
 
 int numbers;
 int even=2;
 printf("Enter up to which number you want to Print: ");
 scanf("%d",&numbers);//Takes Input of up to where numbers should be printed
 int i=1;
 do
 {/*Lets assume input value is 3,Loop Will first print the value 2 now in next step it will increment the value of i by 1
 so now value of i=2 and will increment the value of even by 2 so now value of even is 4 now will check the condition 2<=3 answer is yes
        so it will move up ,in next step it will again print the value of even i.e. 4 and increment the value  of even by 2 and value of i by 1 
 now again check the condition 3<=3 answer is yes will again move up it will continue to run until the condition fails i.e 4<=3 and answer
        will be NO and program will come out of loop*/
  printf("%d\n",even);
  even=even+2;
  i++;
 }
 while(i<=numbers);

 
}





We want you to practice and keep on practicing these steps and we hope that you have understood all the steps we mentioned in this article. Give us your feedback by clicking here or comment down below. Thanks for reading!


Assignments:
You have an assignment! Download this document and solve the questions for your practice. Send us your solution .c or .cpp files through The QuickSEnd down below.



The QuickSEnd:


No comments:

Post a Comment