- C Programming Tutorial
- C - Home
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Constants
- C - Literals
- C - Escape sequences
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - While loop
- C - Functions
- C - Main Functions
- C - Return Statement
- C - Scope Rules
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Pointers
- C - Pointer Arithmetics
- C - Passing Pointers to Functions
- C - Strings
- C - Array of Strings
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Pointers to Structures
- C - Self-Referential Structures
- C - Nested Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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 −
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.
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.
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.
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.
continue statement
In C, the continue statement causes the conditional test and increment portions of the loop to execute.
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;
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.