Decision -
These are required when we have two options to go forward to. We basically want our program to run a specific function when the condition is true else the other function. These can be done by various conditions in C language such as if, else ; do, while ; while etc.
Each function can be used in all situations but some operators work better in specific conditions or we can say more easy to use.
- If - Else statement
syntax -if (condition)
{
first situation
}
else
{
second situation
}
In this if the condition is satisfied the if loop works and if it is not satisfied then the else loop works .
for example -
#include <stdio.h>
int main()
{ int a;
scanf("%d",&a);
if (a<=10)
{ printf("The no is less than or equal to 10"); }
else
{ printf("The no is greater than 10"); }
return 0;
}
- do while
syntax -do
{
statements
}
while ( conditions );
for example -
#include <stdio.h>
int main()
{ int a;
do
{ printf("The no is %d\n",a);
a=a+1; }
while (a<=10);
return 0;
}
output -
The no is 1
The no is 2 ... upto
The no is 10
- while loop
syntax -
while (condition)
{
statements
}
for example -
#include <stdio.h>
int main()
{ int a;
while (a<=10)
{ printf("%d,",a);
a=a+2; }
return 0;
}
output -
0,2,4,6,8,10,
- For loop
syntax -
for(intialization, condition , increment )
{
statements
}
It will run the condition from initial value to the condition specified and the statements will be repeatedly printed.
for example -
#include <stdio.h>
int main()
{ int a;
for (a=1;a<=5;a++)
{
printf("%d,",a);
}
return 0;
}
output -
1,2,3,4,5,
0 Comments