Sunday, 21 January 2018

Use Of Logical Operators


Use Of Logical Operators

Use Of Logical Operators
 C allows usage of three logical operators, namely && ,|| and ! These are to be read as 'AND' ,' OR' and 'NOT' respectively.&&, || allows two or more conditions to be combined  in an IF statement. Let us see how they are used in a program.

Example:21

         main()
         {
           int m1,m2,m3,m4,m5,per;
           printf("Enter marks n five subjects");
           scanf("%d%d%d%d%d",&ma,&m2,&m3,&m4,&m5);
           per=(m1+m2+m3+m4+m5)/5;
           if(per>=60)
             printf("First Division");
           if((per>=50)&&(per<60))
             printf("Second Division");
           if((per>=40)&&(per<50))
             printf("Third Division);
           if(per<40)
             printf("fail");
          }



Advantages of this operators: 

1. The matching of the IF's with their corresponding ELSES
gets avoided, since there are no ELSES in this program.

2.In spite of using several conditions, the program doesn't
  creep to the right.

The else if Clause:

           In this case every ELSE is associated with its previous If. The last ELSE goes to work only if all the conditions fail. Even in else if ladder the last ELSE is optional.

Note That  the ELSE IF cause is nothing different. It is just a way of rearranging the else with if that follows it.This would be evident if you look at the  following code:
             
if(i==2)                       |  if(i==2)
printf("With you!.........");  |   printf("With you.....");
else                           |   else if(j==2) 
{                              |   printf(".....All the time");
 if(j==2)                      |
 printf("...... All the time");|


Example:22   /* Else if ladder demo*/
     
           main()
           {
            int m1,m2,m3,m4,m5,per;
            printf("Enter marks n five subjects");
            scanf("%d%d%d%d%d",&ma,&m2,&m3,&m4,&m5);
            per(m1+m2+m3+m4+m5)/5;
            if(per>=60)
            printf("First Division");
            else if(per>=50)
            printf("Second Division");
            else if(per>=40)
            printf("Third Division");
            else
            printf("Fail");
          }

Example:23

        main()
        {
          char gender,ms;
          int age;
          printf("Enter age,gender,Marital Status");
          scanf("%d%c%c",&age,&gender,&ms);
          if((ms=='M')||(ms=='U'&&gndeer=='M'&&age>30)
          ||(ms=='U'&&gender=='F'&&age>25))
          printf("driver is insured");
          else
          printf("driver is not insured");
        }

Explanation:

1. The driver will be insured only if one of the conditions enclosed in parenthesis evaluates to TRUE. 2. For the second pair of parenthesis to evaluate to TRUE, each  condition in the parenthes is separated by '&&'  must evaluate to TRUE.
3. Even if one of the conditions in the second parenthesis evaluates to FALSE, then the whole of the second parenthesis evaluates to FALSE.

    

Related Posts:

  • Introduction To ARRAYS Introduction To ARRAYS  An array is a group of related data items that share a common name.For instance,  we can define an array name Salary to represent a set of salaries of a group of employees. A particular … Read More
  • STANDARD LIBRARY FUNCTIONS STANDARD LIBRARY FUNCTIONS Arithmetic Functions: abs --> Returns the absolute value of an integer. cos --> Calculates cosine. cosh --> Calculates hyperbolic cosine. exp --> Raises the exponential e to the xt… Read More
  • Declaration of Arrays Declaration of Arrays  Like any other variables, arrays must be declared before they are used.The general form of array declaration is type variable-name [size]; The type specifies the type of elements that will be… Read More
  • Parameter Passing Technique Parameter Passing Technique The parameter passing mechanism refers to the actions taken regarding the parameters when a function is invoked. There are two parameter passing techniques available in C. They are: 1) Call B… Read More
  • Function Declaration and prototype Function Declaration and prototype It is one of the most important feature of ANSI C.A function prototype tells the compiler the type of data returned by the function, the number of arguments the function expects to r… Read More

0 comments:

Post a Comment