How to Write Multiple Decisions in C Programming

By Dan Gookin

Not every decision is a clean-cut, yes-or-no proposition. Exceptions happen all the time. The C programming language provides a few ways to deal with those exceptions, allowing you to craft code that executes based on multiple possibilities.

How to make more-complex decisions in C programming

For the either-or type of comparisons, the if keyword has a companion — else. Together, they work like this:

if(condition)
{
  statement(s);
}
else
{
  statement(s);
}

When the condition is true in an if-else structure, the statements belonging to if are executed; otherwise, the statements belonging to else are executed. It’s an either-or type of decision.

An if-else Comparison is an update of sorts to the code shown in A Simple Comparison. The single if structure has been replaced by if-else. When the if comparison is false, the statement belonging to else is executed.

A SIMPLE COMPARISON

#include <stdio.h>
int main()
{
  int a,b;
  a = 6;
  b = a - 2;
  if( a > b)
  {
    printf("%d is greater than %dn",a,b);
  }
  return(0);
}

AN IF-ELSE COMPARISON

#include <stdio.h>
int main()
{
  int a,b;
  a = 6;
  b = a - 2;
  if( a > b)
  {
    printf("%d is greater than %dn",a,b);
  }
  else
  {
    printf("%d is not greater than %dn",a,b);
  }
  return(0);
}

Exercise 1: Type the source code for An if-else Comparison into a new project. Compile and run.

Exercise 2: Modify the source code so that the user gets to input the value of variable b.

Exercise 3: Modify the source code from Get “Is Equal To” into Your Head so that an if-else structure replaces that ugly if-if thing. (Hint: The best solution changes only one line of code.)

GET “IS EQUAL TO” INTO YOUR HEAD

#include <stdio.h>
#define SECRET 17
int main()
{
  int guess;
  printf("Can you guess the secret number: ");
  scanf("%d",&guess);
  if(guess==SECRET)
  {
    puts("You guessed it!");
    return(0);
  }
  if(guess!=SECRET)
  {
    puts("Wrong!");
    return(1);
  }
}

How to add a third option with C

Not every decision made in a program is either-or. Sometimes, you find yourself in need of an either-or-or type of thing. In fact, no word is found in English to describe such a structure, but it exists in C. It looks like this:

if(condition)
{
  statement(s);
}
else if(condition)
{
  statement(s);
}
else
{
  statement(s);
}

When the first condition proves false, the else if statement makes another test. If that condition proves true, its statements are executed. When neither condition is true, the statements belonging to the final else are executed.

Exercise 4: Using the source code from Values Are Compared as a base, create an if-if else-else structure that handles three conditions. The first two conditions are specified, and you need to add the final possibility using a structure similar to the one shown here.

VALUES ARE COMPARED

#include <stdio.h>
int main()
{
  int first,second;
  printf("Input the first value: ");
  scanf("%d",&first);
  printf("Input the second value: ");
  scanf("%d",&second);
  puts("Evaluating...");
  if(first<second)
  {
    printf("%d is less than %dn",first,second);
  }
  if(first>second)
  {
    printf("%d is greater than %dn",first,second);
  }
  return(0);
}

C has no limit on how many else if statements you can add to an if decision process. Your code could show an if, followed by three else-if conditions, and a final else. This process works, though it’s not the best approach.