Thursday 27 October 2016

IF statement : C Language

If-Else

Conditional operator

Before start writing something about this post I want to ask you a question.

Is computer IQ is greater than human?

What is your answer Yes/NO?

Answer

Computer IQ is zero. If IQ is zero then how can computer work smartly and take decision like human.

It is possible of IF statement which enable your computer to make decision. That is why every programming language support if-else statement.

Syntax

If(condition)
{
                If condition is true
                Then this part will be run
}
Else
{
                If condition is false
                Then this part will be run
}



Void main()
{
                Int age;
                Printf(“Enter your AGE>> “);
Scanf(“%d”,&age)
If(age>18)
                Printf(“\nEligible for vote”);
                Else
                                Printf(“\nNot Eligible for vote”);
}

It was simple example of if-else statement. if age input is greater than 18 then output will be Eligible for vote otherwise Not Eligible for vote.

Before going to dive more you need to know some logical operator.

Boolean Data


If you are making some condition then your result will be either true or false. So Boolean data has two value only true or false.

True

1 or Positive number

False

0 or Negative number

Logical operator


Operator Name
Example
Result
Greater than (>)
5>5
0
Less than (<)
5<5
0
Greater than equal to (>=)
5>=5
1
Less than equal to (<=)
5<=5
1
Not Equal to (!=)
5!=5
0

5!=4
1


&& (And) Operator


Return true if all mention condition will true if any condition is false then result will false.
Example : you are going to apply in government job and checking eligibility on the basis of 
Age>18
Gender=M
Graduation = Pass with 60%

So you have to qualify the every clause to claim eligibility if anything is false then you will not apply for that particular job..

Truth Table


Argument 1
Argument 2
Result
1
1
1
1
0
0
0
1
0
0
0
0


void main()
{
      int age,marks;
      printf("Enter your age and marks>> ");
      scanf("%d%d",&age,&marks);
      if(age>18 && marks>60)
            printf("\nEligible");
      else
            printf("\nNot Eligible");
}

|| (OR) Operator


Return true if any condition become true from specified conditions.

For example you going to attend any exam and invigilator demand your ID proof for entry. So you have number choices of ID proof to show
  • DL
  • Voter Card
  • Adhaar
  • Passport
  • PAN Card etc

If you have any of the mention id previously then you will be allowed. You will be disallowed only one circumstance if you don’t have any id as mention.

True Table


Argument 1
Argument 2
Result
1
1
1
1
0
1
0
1
1
0
0
0

void main()
{
     char ch;
     printf("Enter a character ");
     scanf("%c",&ch);
     if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
          printf("\nVovel");
     else
          printf("\nConsonent");
}

Nested if


Sometimes we make some decision which are further based on some other condition like you want to buy Apple iphone7 then what will be your query to salesman
Do you sale apple phone Y/N
  If yes then do you have Iphone 7 Y/N
     If yes buy otherwise return
You can handle this type of situation by using nested if-else

Syntax

If(conition)
{
                If(condtion)
                                True part
                Else
                False part
}
Else
{
                If(condition)
                                True part
                Else
                                False part
}

Example :  finding greatest number between three number

void main()
{
     int a,b,c;
     printf("Enter Three number>> ");
     scanf("%d%d%d",&a,&b,&c);
     if(a>b)
     {
          if(a>c)
              printf("\n%d",a);
          else
              printf("\n%d",c);

     }
     else
     {
          if(b>c)
              printf("\n%d",b);
          else
              printf("\n%d",c);
     }

}

Some other fact of if statement


Curly Bracket 



If your if and else statement has only one statement then there is no need to curly bracket otherwise mandatory if not , then only first statement just after if or else will be consider being  part of it. Following code will generate error
If(a>b)
                Puts(“Hello “);
                Pus(“You’re eligible”);
Else
                Puts(“Not eligible”);

Error : misplace else

As you read earlier  curly bracket is optional if only one line but in example we have two line for if and we omit curly bracket so compiler will consider first statement and end if body so now else is without if.

Semicolon after if


A=10;
B=20;
If(B<A);
                Puts(“ok”);

Output : ok because semicolon been used after if condition so compiler end if body on that point that is why next statement is not part of if and print every time whether condition is false.

Download PDF

Download C Source Code


No comments:

Post a Comment