Decision Making in C

What is a Decision-Making statement?

Decision-making statements determine the program's arc and flow. Because they provide conditions using boolean expressions that are evaluated to a true or false boolean value, they are sometimes referred to as conditional statements. A particular piece of code will run if the condition is true; if the state is false, the block will not run.

The statements below in C can be used to make decisions.

  1. If Statement
  2. If..else statement
  3. if..else-if statement
  4. Nested if statement
  5. Switch statement
  6. Jump Statement

If Statement

The if statement is the most basic and direct expression for making a choice. It is used to decide whether or not to run a specific code section. A block of statements is only performed if a particular condition is met; otherwise, it is not.

Syntax:

if(condition)
{
        //Statement to be executed
        //if condition is true
        Statement 1;
        Statement 2;
        .        .
        .        .
        Statement n;
}

The if statement is the most basic and direct expression for making a choice. It is used to decide whether or not to run a specific code section. A block of statements is only performed if a particular condition is met; otherwise, it is not.

Example:

if(condition)
  Statement 1;
  Statement 2;

If the condition is true, the previous snippet will be considered inside if and executed.

Example:

#include<stdio.h>
int main()
{
  int n=20;
  if(n>10)
  {
      printf("Inside if block\n");
      printf("N is greater than 10\n");
  }
  printf("if block ended\n");
}

Output:

Inside if block
N is greater than 10
if block ended

The statements included inside the if statement is carried out if the condition present in the if statement evaluates to true.

write your code here: Coding Playground

If else

A block of statements will be performed if a condition is true, and they won't be executed if the condition is false, according to the if statement.

What happens, though, if the condition is false and we wish to take a different action? The if-else phrase is used in this situation. We may use the otherwise statement in combination with the if statement to execute a code block when the condition is false.

Syntax:

if(condition)
{
        //Execute this block
        //if condition is true
}
else
{
  //Execute this block
  //if condition is false
}

Example: Program to check if a given number is even or odd

#include<stdio.h>
int main()
{
  int n;
  printf("Enter a number:");
  scanf("%d", &n);

  if(n%2==0)
  {
      printf("Given number is even \n");
  }
  else
  {
      printf("Given number is odd \n");
  } 
}

Output:

Enter a number:11
Given number is odd

write your code here: Coding Playground

If-else-If ladder

In C, if-else-if is used to make decisions when several possibilities are available. The "if" statements are carried out bottom-up. The statement related to that if is performed when one of the conditions governing it is met, and the rest of the ladder is skipped. The last else statement is carried out if none of the requirements are met.

Syntax:

if(condition1) {
  // Executes if condition 1 is true
}
else if (condition2) {
  // Executes if condition 2 is true
}
else if (condition3) {
  // Executes if condition 3 is true
}
...
else {
  // Executes if all conditions become false
}

Example: Check whether an integer is positive, negative, or zero.

#include <stdio.h>

int main() {
  int number;
  scanf("%d",&number);
  if (number > 0) {
      printf("You entered a positive integer\n");
  }
  else if (number < 0) {
      printf("You entered a negative integer\n");
  }
  else {
      printf("You entered 0.\n");
  }
  return 0;
}

#include <iostream>
using namespace std;

int main() {
  int number;
  cin >> number;
  if (number > 0) {
      cout << "You entered a positive integer\n";
  }
  else if (number < 0) {
      cout << "You entered a negative integer\n";
  }
  else {
      cout << "You entered 0.\n";
  }
  return 0;
}

input:-11
Output: You entered a negative integer

If we input -11, the first condition is verified since the value is less than zero. The statement within the else-if is now executed since the following else-if is examined, and the number is found to be less than 0.

write your code here: Coding Playground

Nested if

Suppose statements that are nesting are included within another if statement. We may use an if statement within another if statement in C and when we need to make several judgments, and nested if statements in C are helpful.

Syntax:

if (condition1)
{
  // Executes when condition1 is true
  if (condition2)
  {
    // Executes when condition2 is true
  }
}

Example: In this example, we would first determine whether a number was more than 10 before deciding whether it was greater than 20.

#include<stdio.h>
int main()
{
  int n=21;

  if(n>10)
  {
      printf("Number is greater than 10 \n");

      if(n>20)
      {
          printf("Number is greater than 20 also \n");
      }
  } 
}

write your code here: Coding Playground

Nested if else

In C, nested if else may also be utilized to make decisions. In C, nested if else can be used when a sequence of decisions is necessary.

Syntax:

if(condition1)
{   //execute if condition1 is true
  if(condition2)
  {
    //execute if condition2 is true
      statements
  }
  else
  {
      //execute if condition2 is false
      statements
  }
}
else
{
  //execute if condition1 is false
  statements
}

For Example: in this program, we will determine whether a number is divisible by 10 and whether it is equal to 10 or not.

#include<stdio.h>
int main()
{
  int num=10;
  if(num%10==0)
  {
      if(num==10)
      {
          printf("The number is:%d\n",num);
      }
      else
      {
          printf("The number is divisible by 10 but not 10");
      }
  }
  else
  {
      printf("The number is not divisible by 10");
  }
  return 0;
}

Output:

The number is:10

write your code here: Coding Playground

Switch Case Statement

As a condensed version of the Nested If-Else statement, often used in C to make decisions, a switch case statement helps programmers avoid creating lengthy chains of if-else-if. A switch-case statement determines the block of code that should be performed by comparing an expression to several circumstances.

switch (expression)  {
  case constant1:
      // code to be executed if the expression equals constant1
      break;
  case constant2:
      // code to be executed if the expression equals constant2
      break;
  case constant3:
      // code to be executed if the expression equals constant3
      break;
      ...
    default:
      // code to be executed if the expression does not match any constants
}

A single evaluation of the expression, which must result in a "constant" value, is performed before it is compared to the values of each case label (constant 1, constant 2, .., constant n).

If a match is discovered for a case label, the code after that label is executed until a break statement is reached or the control flow reaches the switch block's conclusion.

In the absence of a match, the code following default is run.

Example: Program to identify numbers between 1-5

#include <stdio.h>
int main()
{
int num=10;
  switch (num)
  {
      case 1: printf("Number is 1");
              break;
      case 2: printf("Number  is 2");
              break;
      case 3: printf("Number  is 3");
              break;
      case 4: printf("Number  is 4");
              break;
      case 5: printf("Number  is 5");
              break;
      default: printf("Invalid input");
              break;
  }
return 0;
}

#include <iostream>
using namespace std;
int main()
{
int num=3;
  switch (num)
  {
      case 1: cout<<"Number  is 1";
              break;
      case 2: cout<<"Number  is 2";
              break;
      case 3: cout<<"Number  is 3";
              break;
      case 4: cout<<"Number  is 4";
              break;
      case 5: cout<<"Number  is 5";
              break;
      default: cout<<"Invalid input";
              break;
  }
  return 0;
}

write your code here: Coding Playground

Jump Statements

Jump statements cause unconditional jumps to other statements elsewhere in the code. They are mainly employed to break loops and switch statements.

In C, there are four different forms of jump statements.

  • Break
  • Continue
  • Goto
  • Return

The part that follows will go through these leap statements for C decision-making.

Break Statement

When the break statement is used in C, the loop or switch statement is terminated, and control is instantly transferred to the statement before the loop.

Syntax:

break;

Break statements are typically used when we don't know how many times a loop will run and wish to end the loop depending on specific criteria.

Example: Identify any negative values in an array.

#include <stdio.h>
int main()
{
  int arr[] = {5, 6, 0, -3, 3, -2, 1};
  int size = 7; // No of elements in array
  for (int i = 0; i < size; i++)
  {
      if (arr[i] < 0)
      {
          // Array contains a negative value, so break the loop
          printf("Array contains negative value.");
          break;
      }
  }
}

Output:

Array contains negative value.

The C decision-making statement "continue" is the exact reverse of the "break" statement in that it compels the loop to run through its remaining iterations rather than ending it.

The code that follows the continue statement is skipped when the continue statement is run, and controls advance to the next iteration.

Syntax:

continue;

Example: Print out all array values that are not negative.

#include <stdio.h>

int main()
{
  int arr[] = {5, 6, 0, -3, 3, -2, 1};
  int size = 7; // no of elements in array
  for (int i = 0; i < size; i++)
  {
      if (arr[i] < 0)
      {
  // If arr[i] < 0, then skip the current iteration i.e no statements following
          // continue will be executed.
          continue;
      }
      printf("%d ",arr[i]);
  }
}

Output:

5 6 0 3 1

write your code here: Coding Playground

GOTO Statement

By shifting control to another area of the programme, the goto statement may change the typical flow of programme execution. Within a function, you may hop from one place to another by using the goto statement.

Syntax:

goto label;
.               
.              
.               
label:


OR


label
.               
.             
.         
goto label;

Example: Using the goto statement, determine if a number is even or odd and report the result appropriately.

#include <stdio.h>

int main()
{
  int number;
  scanf("%d",&number);
  if (number % 2 == 0)
      goto printeven;
  else
      goto printodd;

printeven:
  printf("Even number");
  return 0;

printodd:
  printf("Odd number");
  return 0;
}

Output:

Input1:
7
Output1:
Odd number

Input2:
8
Output2:
Even number

write your code here: Coding Playground

Return Statement

The return statement ends the execution of a function and returns control to the caller function. It can also indicate the value the function should return. There might be one or more return statements in a function.

Syntax:

return [expression];

#include <stdio.h>

// int return type function to calculate sum
int SUM(int a, int b) {
  int s1 = a + b;
  return s1;
}

// void returns type function to print
void Print(int s2) {
  printf("The sum is %d",s2);
  return;
}

int main() {
  int n1 = 10;
  int n2 = 20;
  int summ = SUM(n1, n2);
  Print(summ);
  return 0;
}

Output:

The sum is 30

write your code here: Coding Playground