Friday, July 29, 2016

C Basics - Decision Making Statements in C

By default the instructions in a program are executed sequentially. Many a times, we want a set of instructions to be executed in one situation, and an entirely different set of instructions to be executed in another situation. This kind of situation is dealt in C programs using a decision control instruction.

The general form of a typical decision making structure is,
  • Check condition
  • If condition is true, execute conditional code. If condition is false, skip executing conditional code.
C 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.

A decision control instruction can be implemented in C using:
if statement: An if statement consists of a boolean expression followed by one or more statements.

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

else-if statement: An if statement can be followed by an optional else-if statement, which evaluates another Boolean expression.

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 allows a variable to be tested for equality against a list of values.

Nested switch statement: You can use one switch statement inside another switch statement(s).

Conditional operator statement: It can be used to replace if-else statement.

Related topics:
Overview of Statements in C   |   The if Statement in C   |   The if-else Statement in C   |   The else-if Statement in C   |   Nested if-else Statement in C   |   Forms of if Statement in C   |   Switch Statement in C   |   Conditional Operator Statement in C   |   The null Statement in C

List of topics: C Programming

No comments:

Post a Comment