Loops Control Instruction
In this there are mainly three types of loops that are given below:
1. while loop
2.do while loop
3.for loop
π₯ 1.while loop
In this while loop syntax is
while(a<5) //() in this bracket condition checks and after that if it true the code executed .
{
printf("Value of the number"); //this code execute after the condition become true under of bracket
}
#include <stdio.h>
int main()
{
int a;
printf("Enter the value of a\n");
scanf("%d", &a);
while (a < 10)
// int a = 2;
// while (a > 1) ---> This two line make this program to infinite loop
{
printf("%d\n", a);
a++;
}
return 0;
}
π₯ 2.do while loop
In do while loop there is some different concept it run the code first and after that it check the condition means the code execute atleast one time if condition is not true
do
{
printf("Value of the number") ;
}
while(Condition or a < 5)
#include <stdio.h>
int main()
{
int a, b;
printf("Enter the number to start\n");
scanf("%d", &a);
printf("Enter the number to end\n");
scanf("%d", &b);
do
{
printf("Your number is %d\n", a);
a++;
} while (a <= b);
return 0;
}
π₯ 3.for loop
for loop is like the all thing are organized just you have to put the values and get the result
for(initialize; test condition; increment or decrement )
{
printf("Value of the number");
}
#include <stdio.h>
int main()
{
int a, i;
printf("Enter the number to start");
scanf("%d", &i);
printf("Enter the number of times to print");
scanf("%d", &a);
// for (---> this is initilize area<---- i or i=0; ---> this is test or contional check area<---- i or i<5; ---> this is increament or decreamnet area<---- i++ or i--)
for (i = 0; i < a; i++)
{
printf("Your Number is= %d\n", i);
}
return 0;
}
// Both are of same tyoe of programm bu from different methods
#include <stdio.h>
int main()
{
int Number, i;
printf("\n Please Enter any Integer Value : ");
scanf("%d", &Number);
printf("\n List of Natural Numbers from 1 to %d are \n", Number);
for (i = 1; i <= Number; i++)
{
printf(" %d \t", i);
}
return 0;
}
In this chapter there is two more concepts that is
π₯π₯1.Break;
#include <stdio.h>
int main()
{
int a = 0;
do
{
printf("print the number: %d\n", a);
a++;
if (a > 3)
{
break;
}
} while (a < 5);
return 0;
}
π₯π₯2.Continue;
#include <stdio.h>
int main()
{
int a = 0;
// printf("Enter the number");
// scanf("%d", &a);
while (a < 20)
{
if (a > 7)
{
continue;
}
else
{
printf("Value of A: %d\n", a);
}
a++;
}
return 0;
}
Other Program for loops
π₯
//Here we learned to use the while loop with if conditional statement
#include <stdio.h>
int main()
{
int i = 0;
while (i <= 20)
{
if (i > 9)
{
printf("%d\n", i);
}
i++;
}
return 0;
}
π₯π₯π₯π₯π₯ increment and decreamnt explanation bonous
#include <stdio.h>
int main()
{
int a;
scanf("%d", &a);
printf("The value of a is %d\n", a);
printf("The value of a is %d\n", ++a); // a++ ---> it means pehle print kare fir increament kare
printf("The value of a is %d\n", a++); // ++a ---> it means pehle increamne etkare fir print kare
printf("The value of a is %d\n", ++a);
return 0;
}
π₯
#include <stdio.h>
int main()
{
int i;
printf("Enter the number to print in reverse");
scanf("%d", &i);
// for (---> this is initilize area<---- i or i=0; ---> this is test or contional check area<---- i or i<5; ---> this is increament or decreamnet area<---- i++ or i--)
for (i; i; i--)
{
printf("Your Number is: %d\n", i);
}
return 0;
}
Comments
Post a Comment