fb popup

/* remove if it's exist in your template */

For Loops

For loop:-
 
for(initialisation;condition;increment or decrement)

{

............................;

statements

....................;

}
Note:-
     "for loop" should not end with semi colon.
It doesn't mean that it will not get executed but the statements inside the loop won't execute.

Important to know:

Here in for loop the variable is initialised and it checks the condition whether it is true or not.If it is true the statements within the loop is executed other wise the compiler goes to the next statement after the for loop.

If condition proves to be true :- The increment operator will increase/decrease the value of a variable only after executing all the statements within the for loop and before going to the next iteration.

The increment operator need not be either for example:- i++,i--,++i,--i which are unary operators. They may be i=i+2,i=i-4 etc depending on the need of our program.

 EX:-BASIC:-
#include<stdio.h>

main()

{

int i;

for(i=0;i<=5;i++)

{

printf("%d",i);

}

}

OUTPUT:-012345

Explanation:-Here in the above program

1.  i is initialized as an integer variable.

2.  Then i is assigned to zero in for loop and checks whether the variable i is <=5.As it is true then it goes inside the loop.

3. Then the print statement is executed and prints the value of i.

4.The increment operator which is in the 3rd part of for loop is now activated and increments the i value by 1.

5.Now i becomes 2 and again checks whether i <=5 and as it is true then it goes inside the loop and this process goes on until the i value is 5

6.During the value of i i.e when it is 5 then the print statement is executed and it again increments i value by 1 which makes i as 6.

7.Now the condition proves to be false and comes out of the loop and goes to the next statement in the main if any.

So what happened is that the increment operator will come into the picture only after executing all the statements of the loop and before it goes for next iteration.  

To print prime numbers. 
#include<stdio.h>
main()
{
int i,n,j,k;
printf("Enter the number of prime numbers\t");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
k=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
k++;
}
if(k==2)
printf("%d\n",i);

}
}

Explanation:-
1. i,j,n,k are initialised as integer variables.

2.It asks the user to enter the number of prime numbers of his/her choice.

3.The main idea is that it takes the value from i and checks the remainder of i with each and every value from j to i whether it is zero or not and if it is zero then it increments k. 

4. If the number is prime then it should go with only 2 values which causes k to increment 2 times and if k is equals to 2 then it prints that particular number other wise it will go for checking another value.

5.Like wise it prints all prime numbers from 1 to n.

For Loop Program


Output:- for the first 30 numbers.

For Loop Program output


 In the above program I used 2 "for loops" at a time.We can also do that if our program needs it and we call that as nested loop.

 nested loop means having "for loop within for loop".

 The Explanation of the above program will be discussed in detail in the later pages.

In nested for loop first the inner loop gets executed if the outer loop values to be true with the condition.Then it continues to iterate until the outer loop's condition is false.

 Infinite loop:-
#include<stdio.h>

{

int i;

for(i=0; ;i++)

printf("%d",i);

}
Here I haven't used any curly braces because if there is only one line inside for loop then there is no need of using braces and it is our wish to use it or not.

 Explanation:-
1.)Initializes i as integer variable.

2.)assigns i to zero.

3.)prints value of i that is 0. and increments value of i by 1 and now i becomes 1 and then as there is no condition then it means it takes as true.Then again it prints the value of i and again increments i by 1.So, now i becomes 2.and so on until infinite times as there is no condition to evaluate and terminate the program at a certain value.


Output of the above program is printing 1,2,3,4 and so on to infinite i.e it will never stop printing the numbers.

You try this as I cannot show you the output in C-free.

To stop that press pause/break on your keypad.

No comments:

Post a Comment