Making decision in C program

Decision making is an important aspect of programming language that enables the programmer to arrive at a conclusion based on certain data processing, comparisons and conditional situations. In most programming language there are inbuilt functions and language constructs that enable implicit decision making. In other situations, it offers a combination of language constructs such as comparison operators, conditional constructs and looping constructs. C programming language, which is the father of modern programming languages, provides the basic foundation to incorporate the decision-making process. Even the more advanced AI techniques are basically founded on this core programming aspect.

A simple implicit language construct for decision making is scanf function which enables the program to decide on user input.

Below program accepts an integer via scanf and outputs the modulus of the number.

main () {
int n;

printf("Please enter any integer");

scanf("%d", &n);

printf("%d MOD 2 is %d",n,n%2);

}

Relational Operators in C program for decision making

Below are the common relation operators that could be used in a C program to construct decision making structures –

  •  Equal to ‘==’
  •  Greater than ‘>’
  •  Less than ‘<‘
  •  Not equal to ‘!=’
  •  Less than or equal to ‘<=’
  •  Greater than or equal to ‘>=’

Logical Operators in C program for decision making

Below are the common logical operators that could be used in a C program to construct decision making structures –

  • AND  ‘&&’
  •  OR    ‘||’
  •  NOT ‘!’
  •  False 0
  •  True 1

Conditional Operators in C program for decision making

The most common conditional operator used in C is the if – else construct.

eg:

if (var1 > var2){
                  printf("Happy Vacation!");
           }
           else{
                printf("More to go..");
           }

 

switch construct is also used to build conditional statements. An example switch statement is shown below –

#include 
 
int main () {
   
   int signal_color = 'G';

   switch(signal_color) {
      case 'G' :
         printf("Green Signal\n" );
         break;
      case 'Y' :
         printf("Yellow Signal\n" );
         break;
      case 'R' :
         printf("Red signal\n" );
         break;
      
      default :
         printf("High way!\n" );
   }   
   
   return 0;
}

Order of precedence of operators

Order of precedence of operators is important while building decision constructs. Below is the operator’s precedence in C programming –

Type Operator Associativity
Postfix () [] -> . ++ – – Left to right
Unary + – ! ~ ++ – – (type) * & sizeof Right to left
Multiplicative * / % Left to right
Additive + – Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %= >>= <<= &= ^= |= Right to left
Comma , Left to right