Logical operators



These operators provide logic to our programming , there are basically  3 types of logical operator

And operator
&&
Or operator
||
Not operator
!

We use logical operators with other operators in order to make our programming more efficient
Now to better understand this let us make a program that’s  takes a number called marks as input and display the result based on following conditions

0 to 30 ---------- D
31  to 40 ------- C
41 to 50 --------B
51 to 70 --------A
71 to 100 ------ A+

So our program will look like

main()
{
      int marks;
     
      printf("Pl enter your marks\n");
      scanf("%d",&marks);
     
             if((marks >=71) && (marks <=100))           
              {
               printf("A+");
              }
              
              else  if((marks >=51)&&(marks <=70))
              {
               printf("A");
              }
              
              else if((marks >=41)&&(marks <=50))
              {
                printf("B");
              }
              
               else if((marks >=31)&&(marks <=40))
              {
               printf("C");
              }
             
              else  if((marks >= 0)&&(marks <=30))
              {
               printf("D");
              }
              
              else
              {
               printf("invalid");
              }
                
               getch();
              
              }


We can have combinations of  "and , or , not" to have many results

Or and “and” have same syntax with different symbols but not is slightly different , for example if we have to check if a value is not some value then we write

if(!(variable == value));

No comments:

Post a Comment