UNIT 3 Chpt 1
Decision Making Within A Program through Conditions
Decision making structures require that the programmer specifies one or more
conditions to be evaluated or tested by the program, along with a statement or
statements to be executed 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.
Sr.No Statement & Description
.
1 if statement
An if statement consists of a boolean expression followed by one or more
statements.
2 if...else statement
An if statement can be followed by an optional else statement, which
executes when the Boolean expression is false.
3 nested if statements
You can use one if or else if statement inside another if or else
if statement(s).
4 switch statement
A switch statement allows a variable to be tested for equality against a list of
values.
5 nested switch 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 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.
Relational Operators:
Operato Description Example
r
== Checks if the values of two operands are equal or not. If yes, (A == B)
then the condition becomes true. is not
true.
!= Checks if the values of two operands are equal or not. If the (A != B)
values are not equal, then the condition becomes true. is true.
> Checks if the value of left operand is greater than the value of (A > B)
right operand. If yes, then the condition becomes true. is not
true.
< Checks if the value of left operand is less than the value of right (A < B)
operand. If yes, then the condition becomes true. is true.
>= Checks if the value of left operand is greater than or equal to (A >= B)
the value of right operand. If yes, then the condition becomes is not
true. true.
<= Checks if the value of left operand is less than or equal to the (A <= B)
value of right operand. If yes, then the condition becomes true. is true.
Logical Connectives/ operators
Operato Description Example
r
&& Called Logical AND operator. If both the operands are non-zero, (A && B)
then the condition becomes true. is false.
|| Called Logical OR Operator. If any of the two operands is non- (A || B) is
zero, then the condition becomes true. true.
! Called Logical NOT Operator. It is used to reverse the logical !(A &&
state of its operand. If a condition is true, then Logical NOT B) is
operator will make it false. true.
C if...else Statement
In this tutorial, you will learn about if statement (including if...else and nested
if..else) in C programming with the help of examples.
C if Statement
The syntax of the if statement in C programming is:
if (test expression)
{
// statements to be executed if the test expression is true
}
How if statement works?
The if statement evaluates the test expression inside the parenthesis () .
If the test expression is evaluated to true, statements inside the body
of if are executed.
If the test expression is evaluated to false, statements inside the body
of if are not executed.
To learn more about when test expression is evaluated to true (non-zero
value) and false (0), check relational and logical operators.
Example 1: if statement
// Program to display a number if it is negative
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
Output 1
Enter an integer: -2
You entered -2.
The if statement is easy.
When the user enters -2, the test expression number<0 is evaluated to true.
Hence, You entered -2 is displayed on the screen.
Output 2
Enter an integer: 5
The if statement is easy.
When the user enters 5, the test expression number<0 is evaluated to false and
the statement inside the body of if is not executed
C if...else Statement
The if statement may have an optional else block. The syntax of
the if..else statement is:
if (test expression) {
// statements to be executed if the test expression is true
}
else {
// statements to be executed if the test expression is false
}
How if...else statement works?
If the test expression is evaluated to true,
statements inside the body of if are executed.
statements inside the body of else are skipped from execution.
If the test expression is evaluated to false,
statements inside the body of else are executed
statements inside the body of if are skipped from execution.
Example 2: if...else statement
// Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
Output
Enter an integer: 7
7 is an odd integer.
When the user enters 7, the test expression number%2==0 is evaluated to false.
Hence, the statement inside the body of else is executed.
C if...else Ladder
The if...else statement executes two different codes depending upon
whether the test expression is true or false. Sometimes, a choice has to be
made from more than 2 possibilities.
The if...else ladder allows you to check between multiple test expressions and
execute different statements.
Syntax of if...else Ladder
if (test expression1) {
// statement(s)
}
else if(test expression2) {
// statement(s)
}
else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
Example 3: C if...else Ladder
// Program to relate two integers using =, > or < symbol
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
Output
Enter two integers: 12
23
Result: 12 < 23
Nested if...else
It is possible to include an if...else statement inside the body of
another if...else statement.
Example 4: Nested if...else
This program given below relates two integers using either < , > and = similar
to the if...else ladder's example. However, we will use a
nested if...else statement to solve this problem.
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2) {
if (number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
else {
printf("Result: %d > %d", number1, number2);
}
}
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
If the body of an if...else statement has only one statement, you do not need
to use brackets {} .
For example, this code
if (a > b) {
print("Hello");
}
print("Hi");
is equivalent to
if (a > b)
print("Hello");
print("Hi");