Published in

Geek Culture

# Decision Making in Programming

have seen the method to get inputs from the user. There might come a time where we need to instruct the computer to make decisions based on the input. For example, in a quiz program, we ask the user the answer and then we need to check whether the user had entered the correct answer. This process of instructing computers to make decisions is known as ‘decision making’ in programming.

# if and else

‘if and else’ statements check whether a condition is true or false. If the condition is true, it executes some particular set of codes and if the condition is false, it executes some other set of codes.

`if (num % 2 == 0){    printf("Number is even");}else{    printf("Number is odd");}`

Here in the parenthesis after ‘if’ we provide the condition which is (num % 2 == 0). The ‘if’ statement checks whether ‘num’ divided by 2 gives the remainder 0. If the condition becomes true, then it will execute the codes between the curly braces, which is the code to display ‘Number is even’. If the condition becomes false, then it will execute the codes between the curly braces after else, which is the code to display ‘Number is odd’.

# if, else and else if

What if we needed to check for more conditions? We can chain the ‘if and else’ statements using the extra ‘else if’ statement.

`if (num > 0) {    printf("Number is positive");}else if (num < 0){    printf("Number is negative");}else{    printf("Number is 0");}`

Here, the ‘if’ statement checks whether ‘num’ is greater than zero. If true, it displays ‘Number is positive’. After that, it checks if ‘num’ is less than 0. If true, it displays ‘Number is negative ’. If both the conditions become false, the ‘else’ statement executes and it displays ‘Number is 0’. We can have as many ‘else if’ statements, as we want, to have in a single ‘if, else and else if’ block.

If there is only a single line of code in the condition, it is not necessary to give the curly braces. It is optional. The code below too shall work as there is only one line of code after ‘if, else and else if ‘ statements.

`if (num > 0)     printf("Number is positive");else if (num < 0)    printf("Number is negative");else    printf("Number is 0");`

`#include <stdio.h>int main(){    float marks;    char grade;     printf("Enter marks: ");    scanf("%f", &marks);        if(marks >= 90)    {        grade = 'A';    }    else if(marks >= 80 && marks < 90)    {        grade = 'B';    }    else if(marks >= 70 && marks < 80)    {        grade = 'C';    }    else if(marks >= 60 && marks < 70)    {        grade = 'D';    }    else if(marks >= 50 && marks < 60)    {        grade = 'E';    }    else     {        grade = 'F';    }                        printf("Your grade is %c", grade);        return 0;}`

Here, after the user enters the marks, the program checks whether each of the conditions in the ‘if and else if ’ statements are true. If a condition becomes true, that particular block of code is executed. If none of the conditions become true, the block of code in the ‘else’ statement is executed. Through these procedures, the program calculates the grade for the entered mark.

# Ternary Operator

The ternary operator can be used as a shortcut for ‘if and else’ statements. It takes three operands. A condition followed by a question mark, then an expression to execute if the condition is true followed by a colon(:), and finally an expression to execute if the condition is false.

`printf("%s", num % 2 == 0 ? "Number is even" : "Number is odd");`

Here, inside printf(), after the format specifier “%s”, we have a ternary operator instead of just a variable. In the operator, there is a condition that checks if ‘num’ divided by 2 gives remainder 0. If the condition becomes true, the expression after the question mark is taken, ie “Number is even” or else, the expression after the colon is taken, ie “Number is odd”.

The grade calculating program can be rewritten with fewer lines of code using the ternary operator.

`#include <stdio.h>int main(){    float marks;    char grade;     printf("Enter marks: ");    scanf("%f", &marks);        grade = marks >= 90 ? 'A' :             marks >= 80 && marks < 90 ? 'B' :            marks >= 70 && marks < 80 ? 'C' :            marks >= 60 && marks < 70 ? 'D' :            marks >= 50 && marks < 50 ? 'E' : 'F';                            printf("Your grade is %c", grade);    return 0;}`

Initially, the condition is ‘marks ≥ 90’. If the condition is true, it executes expression after the question mark and so assigns ‘A’ to the ‘grade’. If the condition is false, it executes the expression after the colon and so checks whether the next condition is true and so on. However, you cannot have multiple lines of codes to be executed if the condition is true or false and so ternary operator can be only used instead of simple ‘if and else’ statements with just a single line of code in each block.

Remember, how we used to print boolean values? The ternary operator can be used to print the boolean values.

`#include <stdio.h>#include <stdbool.h>int main(){    int num1, num2;    bool isEqual;    printf("Enter a number: ");    scanf("%d", &num1);    printf("Enter a number again: ");    scanf("%d", &num2);        isEqual = num1 == num2;    printf("%s", isEqual ? "true" : "false");        return 0;}`

The ternary operator checks whether ‘isEqual’ has the value true or false. If true it prints “true”, else it prints “false”.

Previous => How to Interact With Users in Programming?

--

--

## More from Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).