Categories: C

Decision-Making in C

Decision-making in C, as the name suggests, is a way of performing different sets of actions depending on various circumstances.

Shown below is the general form of a regular decision-making structure found in most of the programming languages −

Here, a particular condition statement is tested by the program, which returns true/false accordingly and executes further instructions.

C has three major decision-making instructions-

  • if statement
  • if-else statement
  • switch statement

note: C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as a false value.

The if statement :

C uses the if keyword to implement the decision control instruction.

Syntax:

if(condition)
{
  //Statement executed if condition is true
}
Key points:
  • The keyword if tells the compiler that what follows is a decision control instruction
  • The condition following the if is always enclosed within a pair of parentheses.
  • If the condition is true, then the statement is executed.
  • If the condition is not true, then the statement is not executed; instead, the program skips past it.
  • Generally, a condition is expressed using relational operators. For instance:
Expressionis true if
x==yx is equal to y
x !=yx is not equal to y
x>yx is greater than y
x<yx is less than y
x>=yx is greater than or equal to y
x<=yx is less than or equal to y
Example:
#include <stdio.h>

int main() {
    int num;
    printf("Enter a number:");
    scanf("%d",&num);
    if(num<10)
    {
        printf("Number is less than 10");
    }
    
    return 0;
}
Input : 3
Output:Number is less than 10
Input :21
Output: 

hence, we can see that no Result is displayed when the input is greater than 10.

Note: also read about Format specifiers in C & Escape Sequence in C.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

5 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

5 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

11 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

11 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

11 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

12 months ago