for( ; ; ) Looping Statement



for(;;):-

For is a looping statement, where we want to do some repeated job.

In a program  where we need to create a loop to perform a repeted job of cirtain of time it's called loop. For is a looping statement where we create a loop to prerform a job or group of job in repeted manner.

"For" is conditional statement and which consist of three parts initialization part, condition part, increment or decrement part. All the part of for statement is optional not mandatory.


SYNTAX: 

for(initialization ; condition ; increment or decrements ){

.............................
..............................
}

EXAMPLE:

OUTPUT:

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




                        SAMPLE PROGRAMS OF SOME FOR STATEMENT:


1. QUESTION: Display Your Name 20 times

CODE:

# include <stdio.h>
# include <conio.h>
int main()
{
int i;

printf("Enter how many time you want to display: ");
for(i = 0; i<20; i++)
{
printf("\nMy name is John");

}
getch();
return 0;
}

OUTPUT:-

DISCUSSION:  In the program we are showing the use of a looping statement because without use of any looping statement we need to write the name "John" in the program and then the length of the program will increase and the quality goes down.There are three part of the for statement 1st one is initialization where we initialize a = 0 that means the valuse of variable a is 0 and every iteration the value of i is increasing and check the condition is true or false. The looping is continue until the condition get false.


2. QUESTION:  Display Your Name and your friend's name 20 times.


CODE:

# include <stdio.h>
# include <conio.h>
int main()
{
int i,Num;

printf("Enter how many time you want to display: ");
scanf("%d",& Num);
for(i = 0; i<Num; i++)
{
printf("\nMy name is John\nMy friend's name is David");

}
getch();
return 0;
}

OUTPUT:

DISCUSSION:In the program we are displaying two names or two string. In some certain times, the number is given by the user at run time and the iteration is for the given times.




3.  QUESTION:  Display all the numbers between 0 to 100

CODE:

# include <stdio.h>
# include <conio.h>
int main()
{
int Num = 0;
printf("The numbers are: ");
for(Num = 0;Num<100;Num++)
printf("\t%d",Num);
 getch();
return 0;
}

OUTPUT:-




DISCUSSION: In this program we are displaying the numbers between 0 to 100 with the help of for loop. In this program the initial value of Num variable is 0 and the maximum value is less than 100 which is 99. Our logic is the initial value of Num is 0 and increase by 1 the value of Num by every looping and when the value of Num reach 100 then terminale the loop because the logic is true until <100 so 100 is not less than 100 so simply the logic condition is false that's why the output is 0 to 99.


4.QUESTION: Display all even numbers between 0 to 100

CODE:-

# include <stdio.h>
# include <conio.h>
int main()
{
int Num = 0;
printf("The numbers are: ");
for(Num = 2;Num<100;Num = Num + 2)
printf("\t%d",Num);
 getch();
return 0;
}

OUTPUT:-




DISCUSSION:  In the program we are showing all the even numbers between 0 to 100. This is a simple program where we create a loop which is iterate Num<100 times and the variable 'Num' is increase by 2 for every iteration. The initial value of 'Num' variable is 0, so the value of 'Num' is increased by every iteration 2, 4, 6 and so on and we get out required result.


5.QUESTION: Display all odd numbers between 0 to 100.

CODE: -

# include <stdio.h>
# include <conio.h>
int main()
{
int Num;
printf("The numbers are: ");
for(Num = 1;Num<100;Num = Num + 2)
printf("\t%d",Num);
getch();
return 0;
}

OUTPUT:-





DISCUSSION: In the program we are showing all the odd numbers between 0 to 100. This is a simple program where we create a loop which is iterate Num<100 times and the variable 'Num' is increase by 2 for every iteration. The initial value of 'Num' variable is 1, so the value of 'Num' is increased by every iteration 1, 3, 5 and so on and we get out required result.



6.QUESTION: Display all the numbers between 0 to 100.

CODE:-

# include <stdio.h>
# include <conio.h>
int main()
{
int Num = 0;
printf("The numbers are: ");
for(Num = 1;Num<100;Num = Num + 4)
printf("\t%d",Num);
getch();
return 0;
}

OUTPUT:-



DISCUSSION: In the program we are showing a series of numbers between 0 to 100 which is the equal interval of 4. This is a simple program where we create a loop which is iterate Num<100 times and the variable 'Num' is increase by 4 for every iteration. The initial value of 'Num' variable is 1, so the value of 'Num' is increased by every iteration 1, 5, 9 and so on and we get out required result.



7. QUESTION: Display all the result of summation of 0 to 100 numbers.


CODE:-

# include <stdio.h>
# include <conio.h>
int main()
{
int Num = 0,Sum=0;
printf("The result is: ");
for(Num = 0;Num<=100;Num++)
{
Sum = Sum + Num;
}
printf("\t%d",Sum);
getch();
return 0;
}

OUTPUT:-




DISCUSSION:Here in the program we are showing the result of summation of 0 to 100 with the help of for statement. In the program we using two variables one is "Num" which is used  as counter and "Sum" is a variable where we store the value of summation, on every iteration we add Sum with Num and store the result in Sum.


8.   QUESTION: Summation of all value between two given value.

CODE:-

# include <stdio.h>
# include <conio.h>
int main()
{
int Num1,Num2,Sum=0;
printf("Enter 1st number: ");
scanf("%d",&Num1);
printf("Enter 2nd number: ");
scanf("%d",&Num2);
for(;Num1<=Num2;Num1++)
Sum += Num1;

printf("The result of the summation is: %d",Sum);
getch();
return 0;

}

OUTPUT:-


DISCUSSION:In the program we are showing the result of summation the given values. In the program we are performing the summation of all the numbers between the given numbers. Here in the program we enter 10 and 20 so we get the result of the summation of all the numbers between 10 and 20 including 10 and 20, and we get our required result.


9.  QUESTION: Check the given number is prime or not.


CODE:-

# include <stdio.h>
# include <conio.h>
int main()
{
int Prime,i,Flag=0;
printf("Enter your number:");
scanf("%d",&Prime);
for(i=2;i<Prime;i++)
{
if(Prime%i==0)
{
Flag=1;
break;
}

}

if(Flag==1)
printf("NOT PRIME ");
else
printf("PRIME");

getch();
return 0;


}

OUTPUT:-








DISCUSSION:This is a program to find the given number is prime or not, prime number means a number is divisible by 1 and the same number it self. To chck a prime number we need to check from 2 to last less than one. If the number is not divisible by any number except 1 and the same number then the number is called prime number.

Here in the program the input number is 53 and base on the logic of prime number checking we start to check by deviding  2 to 52(less than 53)




10.QUESTION: Check how many prime number in between the two given numbers.

CODE:-

# include <stdio.h>
# include <conio.h>
int main()
{
int Num1,Num2,i,Flag=0;

printf("Enter your Minimum:");
scanf("%d",&Num1);
printf("Enter your Maximum:");
scanf("%d",&Num2);

for(;Num1<=Num2;Num1++)
{

for(i=2;i<Num1;i++)
{
if(Num1%i==0)
{
Flag=1;
break;
}

}


if(Flag==1)
printf("\n%d = NOT PRIME ",Num1);
else
printf("\n%d = PRIME",Num1);
Flag = 0;
}
getch();
return 0;


}

OUTPUT:-



DISCUSSION: Here in the program we input two range one is minimum range and another is maximum range and find the all numbers between the range are prime or not. To find the prime we have to check the number is divisible by any number from 2 to less than one from the checked number.



11. QUESTION: Write a program to display the ASCII value of all capital letters.

CODE:-
# include <stdio.h>
# include <conio.h>
int main()
{
char ch;
printf("ASCII VALUE OF THE CHARACTERS: ");
for(ch='A';ch<='Z';ch++)
{
printf("\n\t%i is %c",(int)ch,ch);
}
getch();
return 0;
}


OUTPUT:-




DISCUSSION: In this program we are showing the ASCII value of all capital letters. in this program we take a character variable 'ch' and the initial value of ch is 'A' and we are making a loop which is increased by one and every iteration the value of ch is increased by 1 and we know that the ASCII value of 'A' is 65 and if we assign ch = 'A' + 1 then the value of ch is 'B'. In this way we get our required result.






















Translate