C - Decision Making
C - Decision Making
C - Decision Making
Every programming language including C has decision-making statements to
support conditional logic. C has a number of alternatives to add decision-making in
the code.
Sequential logic
Decision or branching
Repetition or iteration
The decision-making structures control the program flow based on conditions. They
are important tools for designing complex algorithms.
1 if statement
https://www.tutorialspoint.com/cprogramming/c_decision_making.htm 1/8
6/16/24, 11:54 AM C - Decision Making
if...else statement
2 An if statement can be followed by an optional else statement, which
executes when the Boolean expression is false.
nested if statements
3 You can use one if or else-if statement inside another if or else-if
statement(s).
switch statement
4 A switch statement allows a variable to be tested for equality against a
list of values.
If Statement in C Programming
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){
expression;
https://www.tutorialspoint.com/cprogramming/c_decision_making.htm 2/8
6/16/24, 11:54 AM C - Decision Making
...
}
Syntax
if (Boolean expr){
expression;
...
}
else{
expression;
...
}
https://www.tutorialspoint.com/cprogramming/c_decision_making.htm 3/8
6/16/24, 11:54 AM C - Decision Making
You can use one if or else-if statement inside another if or else-if statement(s).
https://www.tutorialspoint.com/cprogramming/c_decision_making.htm 4/8
6/16/24, 11:54 AM C - Decision Making
Syntax
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
As in if statements, you can use one switch statement inside another switch
statement(s).
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.
https://www.tutorialspoint.com/cprogramming/c_decision_making.htm 5/8
6/16/24, 11:54 AM C - Decision Making
An algorithm can also have an 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, whereas the for
statement forms a counted loop.
The loops are also controlled by the Boolean expressions. The 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 the next
iteration.
https://www.tutorialspoint.com/cprogramming/c_decision_making.htm 6/8
6/16/24, 11:54 AM C - Decision Making
goto label;
..
.
label: statement;
With the goto statement, the flow can be directed to any previous step or any
subsequent step.
https://www.tutorialspoint.com/cprogramming/c_decision_making.htm 7/8
6/16/24, 11:54 AM C - Decision Making
https://www.tutorialspoint.com/cprogramming/c_decision_making.htm 8/8