C - Decision Making



Every programming language including C has the decision-making statements, to support conditional logic. Use of Decision making in a program adds intelligence to the machine. C has a number of alternatives to add decision making in the code. Any process is a combination of three types of logic −

  • Sequential
  • Decision or branching
  • Repetition or iteration

A computer program is sequential in nature and runs from top to bottom by default. The decision-making statements in C provide an alternative line of execution. You can ask a group of statements to be repeatedly executed till a condition is satisfied.

The decision-making structures control the program flow based on conditions. They are important tools for designing complex algorithms.

These keywords and operators in C are used in decision making statements −

  • if
  • else
  • switch
  • case
  • default
  • goto
  • ?: operator
  • break
  • continue

In programming, as in life, there come situations when we need to make some decisions. Based on these decisions, we decide what should we do next. Similar situations arise in an algorithm also where we need to make some decisions and based on these decisions we will execute the next block of code. The next instruction depends on a Boolean expression. If the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Show below is the general form of a typical decision-making structure found in most of the programming languages.

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 false value.

C programming language provides the following types of decision-making statements.

if statement

The if statement is used for deciding between two paths based on a true or false outcome. It is represented by the following flowchart −

Decision making statements in C

Syntax

if (Boolean expr)
{
   Expr;
   . . .
}

An if statement consists of a boolean expression followed by one or more statements.

if...else statement

The if – else statement offers an alternative path when the condition isn't met.

C if...else statement

Syntax

if (Boolean expr)
{
   Expr;
   . . .
}
else
{
   Expr;
   . . .
}

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

nested if statements

Nested if statements are required to Build intricate decision trees, evaluating multiple nested conditions for nuanced program flow.

nested if statements

You can use one if or else if statement inside another if or else if statement(s).

switch statement

A switch statement Simplifies multi-way Choices by evaluating a single variable against multiple values, executing specific code based on the match. It allows a variable to be tested for equality against a list of values.

switch statement in C

Syntax

switch(expression) {

   case constant-expression  :
      statement(s);
      break; /* optional */
	
   case constant-expression  :
      statement(s);
      break; /* optional */
  
   /* you can have any number of case statements */
   default : /* Optional */
   statement(s);
}

As in if statements, You can use one switch statement inside another switch statement(s).

The ? : Operator

We have covered conditional operator ? : in the previous chapter which can be used to replace if...else statements. It Condenses an if...else statement into a single expression, offering compact and readable code.

It has the following general form −

Exp1 ? Exp2 : Exp3;

Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon.

The value of a ? expression is determined like this −

Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression.

If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.

You can simulate nested if statements with the ? operator. You can use another ternary operator in true and/or false operand of an existing ? operator.

An algorithm can also have a repletion or iteration logic. In C, the while, do – while and for statements are provided to form loops. The loop formed by while and do – while are conditional loops, where as the for statement forms a counted loop. The loops are also controlled by the Boolean expressions. C compiler decides whether the looping block is to be repeated again, based on a condition.

The program flow in a loop is also controlled by different jumping statements. The break and continue keywords cause the loop to terminate or perform next iteration.

break statement

In C, the break statement is used in switch – case construct as well as in a loop. When used inside a loop, it causes the repetition to be abandoned.

c break statement

continue statement

In C, the continue statement causes the conditional test and increment portions of the loop to execute.

C continue statement

goto statement

C also has a goto keyword. You can redirect the program flow to any labelled instruction in the program.

Syntax

The syntax for a goto statement in C is as follows −

goto label;
..
.
label: statement;
C goto statement

With the goto statement, the flow can be directed to any previous step or any subsequent step.

In this chapter, we had a brief overview of the decision making statements in C. In subsequent chapters, we shall each of them in details, with suitable examples.

Advertisements