What is Decision Making Statements in C?

Welcome guys, in this module we are going to talk about what is Decision making statements in C, the most interesting and important topic of this particular series, till now we were limited to a single linear statement, i.e., printing something in console or taking input and printing the same, but programming is not limited till here only, we can do a various interesting thing and also can build the amazing program.

So, let’s dive into the journey of this amazing module,

What is Decision Making Statements in C?

Each Individual instruction of a program is called a C statement like variable declaration, expressions, and all. They all terminate with a semicolon and execute in the same order as they appear in the program.

But programs are not limited to this linear sequence, during its execution it may repeat segments of code or may take decisions and all. So, for this purpose C provides a Conditional statement or Decision-making statement that specifies what has to be done and when, and under which circumstances.

What is Decision Making Statements in C

We all encounter a situation in real life when we need to make a decision, to arrive at a particular result, so that we can proceed with what to do next. Therefore, sometimes the same situation arises in programming also where we have to make some decision in order to achieve the desired result.

The decision-making statements are:

  • if
  • if-else
  • else-if ladder
  • nested if-else
  • Switch-case

What is an if statement in C?

It is the simplest of all the decision making a statement in C, like if you have mentioned some condition and if that condition results to be true, then the particular block will be executed, otherwise, that block will be skipped. The syntax for declaring the same is:

if (condition)
{ 
  // executes if the condition is true
}

if statement in C

Let’s see one example to get more clearer

#include <stdio.h>
int main( ) {
int n;
scanf("%d", &n);
if (n>5) {
   printf("Yes");
}
return 0;
}

The output of the above program:
If the user enters the number greater than 5, then the particular if block will get executed and will give the desired result, else it will not do anything.

What is the if-else Statement in c?

It evaluates a single or multiple test expression that results in “TRUE” or “FALSE” and the outcome of this decides which set of blocks is going to be executed based on the condition.
When we want to execute a particular block of code if the condition is true and another block of code if the condition is false. The syntax for declaring if-else statement is:

if (condition) {
   // statement
}
else {
   // statement
}

if-else Statement in c

If the condition results to be true, then the statement inside the if block will be executed, and if the condition results will be false, then statements inside the else block will be executed.

Let’s take one simple example to get it clearer, Program to find whether the entered number is greater than 10 or not.

#include <stdio.h>
int main( ) {
int num;
scanf("%d", &num);
if(num>10)
{
   printf("Yes");
}
else {
   ("No");
}
return 0;
}

The output of the above program is:
If the user enters the number 12 as input, the output is Yes, and if the user enters the number 9 as input, the output is No. Since it is checking the condition whether the number is greater than 10 or not in the if block and if the condition results to be true, then the if block will be executed, and if the condition fails then the else block will be executed.

What is an else-if ladder in C?

When we need to list multiple else-if-else statements then that case we have to create an else-if ladder, it is used to check when there is a multiple condition, the syntax for declaring the same is:

if (expression 1)
{
   // When expression 1 is true
}
else if (expression 2)
{
  // When expression 2 is true
}
else if (expression 3)
{
  // When expression 3 is true
}
else {
  // When none of expression is true
}

When the first Boolean condition becomes true then the first block i.e., if the block gets executed and when the first condition becomes false then the second block is checked with the condition. If the second condition becomes true then the second block gets executed and likewise, it goes on, and if none of the conditions matches to be true, then, in that case, the last else block gets executed.

What is nested if-else in C?

Nested if-else simply means if inside another if, Yes C allows you to perform nested if-else. It is used when you have to check some condition with respect to another condition, the syntax for declaring nested if-else in C is:

if (condition-1)
{
// when condition–1 is true
if (condition–2) {
  // when condition–2 is true
}
}

What is the switch-case statement in C?

The Switch expressions are evaluated once and then the value of that expression is compared to the value of each case. If there is a match, then the associated code block is executed, and if no match found then the default block is executed. It is similar to the if-else statement. The syntax for declaring the switch-case statement is:

switch (expression) {
case x:
  // code block
  break;
case y:
  // code block
  break;
default:
  // code block
}

switch-case statement in C

In the above syntax, one expression is provided with the switch statement, and that particular expression is compared with each case, and if the match found with any of the case, then that particular case block will get executed, and if no match found then the default block will be executed. You all must have noticed two keywords i.e., break and default keywords so why that particular keyword is there, let’s see.

What is the Break keyword in switch-case?

When the C program encounters a break keyword, it breaks out or comes out of the switch block. This means that it will stop the execution of more code and case testing inside that particular block. When a match is found, and the job is done, it’s time for a break and comes out. It saves a lot of time in the execution of the program because it “ignores or leaves” the execution of the rest of the code in the switch block.

What is the default Keyword in switch-case?

The default keyword specifies some code to run if there is no match found, it should be used at the last of the switch block and it does not need a break.

Let us take one example demonstrating, switch-case –

#include <stdio.h>
int main( ) {
int num;
scanf("%d", &num);
switch(num)
{
case 1 : printf("Monday");
         break;
case 2 : printf("Tuesday");
         break;
case 3 : printf("Wednesday");
         break;
case 4 : printf("Thursday");
         break;
case 5 : printf("Friday");
         break;
case 6 : printf("Saturday");
         break;
case 7 : printf("Sunday");
         break;
default : printf("Invalid Number");
}
return 0;
}

In the above program, the aim is to give the days of the week on the input of 1 to 7 and if the input is some other number then it will execute the default block.

I hope, you all enjoyed a lot in this particular module and gained a lot of knowledge on the same, must be excited about the upcoming modules.

Until then, keep practicing, enjoy the learning, Happy coding!