If Statement in C Programming with Example

⇒ For video tutorial you can see below video:


Conditions


Decision Making:
✔ In life, we all have to make decisions. In order to make a decision we weigh out our      options and so do our programs.
✔ Here is the general form of the decision making structures found in C.
✔ For that we have to use Relational  operators.
✔ Now most of the person who are not from the Computer science or not from the computer          field they don’t know about that so I will explain you.
✔ The symbol which we use to decide a things which we want in our program.
✔ In short with the help of relational operator we will make program and make correct                   programs.
✔ So you can see below relational operator and result also:

Here we go for the conditions in c

There are 3 types of conditions in c:
1. if condition – here you can find only 2 answer (Like True or False)
Example : -
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter The Value of A: \n");
scanf("%d",&a);
printf("Enter The Value of B: \n");
scanf("%d",&b);
          if(a>b)
                       {
                                    printf("A is greater than B");
                        }
                else
                        {
                                    printf("B is greater than A");
                         }

getch();
}
Output:
Enter the value of A: 25
Enter the value of B : 40
B is greater than A

2. Else if condition – here you can find more than 2 answer (Like Which day is today so you have 7 answer)

Note :- Else if and switch case work same but both approach and programming way is different.

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter Number: \n");
scanf("%d",&a);
                if(a>0)
                                {
                                                printf("No is Positive");
                                }
                else if(a<0)
                                {
                                                printf("No is Negitive");
                                }
                                else
                                {
                                                printf("No is Zero");
                                }

getch();
}

Output:
Enter Number : 27
No is positive

3. Switch Case - here you can find more than 2 answer (Like Which day is today so you have 7 answer)

Note :- Else if and switch case work same but both approach and programming way is different


Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter Your Choice: \n");
scanf("%d",&a);
switch(a)
{
                case 1: printf("Today is Sunday");
                                                break;
                case 2: printf("Today is Monday");
                                                break;
                case 3: printf("Today is Tuesday");
                                                break;
                case 4: printf("Today is Wednesday");
                                                break;
                case 5: printf("Today is Thursday");
                                                break;                  
                case 6: printf("Today is Friday");
                                                break;
                case 7: printf("Today is Saturday");
                                                break;
                default: printf("Wrong Choice , Please Select No between 1 to 7");                                                           
                                                break;
}
getch();
}

Output:
Enter your choice: 5
Today is Thurssday

Post a Comment

0 Comments