Professional Documents
Culture Documents
if Statement
if...else Statement
Nested if...else Statement
Conditional Operator
The if , if...else and nested if...else statement are used to make one-time decisions in C++
Programming, that is, to execute some code/s and ignore some code/s depending upon the
test condition. Without decision making, the program runs in similar way every time.
Decision making is an important feature of every programming language using C++
programming. Before you learn decision making, you should have basic understanding of
relational operators.
C++ if Statement
The if statement checks whether the test condition is true or not. If the test condition is
true, it executes the code/s inside the body of if statement. But it the test condition is
false, it skips the code/s inside the body of if statement.
Working of if Statement
The if keyword is followed by test condition inside parenthesis ( ). If the test condition is
true, the codes inside curly bracket is executed but if test condition is false, the codes
inside curly bracket { } is skipped and control of program goes just below the body of if as
shown in figure above.
Flowchart of if
Output 1
Enter an integer: 5
You entered a positive number: 5
This statement is always executed because it's outside if statement.
Output 2
Enter a number: -5
This statement is always executed because it's outside if statement.
C++ if...else
The if...else executes body of if when the test expression is true and executes the body
of else if test expression is false.
The if statement checks whether the test expression is true or not. If the test condition is
true, it executes the code/s inside the body of if statement. But it the test condition is
false, it executes the code/s inside the body of else .
Flowchart of if...else
Output
Enter an integer: -4
You entered a negative integer: -4
This statement is always executed because it's outside if...else statement.
}
.
.
.
else {
statements to be executed if all test expressions are false;
}
The nested if...else statement has more than one test expression. If the first test
expression is true, it executes the code inside the braces{ } just below it. But if the first
test expression is false, it checks the second test expression. If the second test expression
is true, if executes the code inside the braces{ } just below it. This process continues. If
all the test expression are false, code/s inside else is executed and the control of program
jumps below the nested if...else
Output
Enter an integer: 0
You entered 0.
This statement is always executed because it's outside nested if..else statement.
Conditional/Ternary Operator ?:
Conditional operators are the peculiar case of if...else statement in C++ Programming.
Consider this if..else statement:
if ( a < b ) {
a = b;
}
else {
a = -b;
}
The above code can be written using conditional operator as:
a = (a < b) ? b : -b;
Both codes above check whether a is less than b or not. If a is less than b, value of b is
assigned toa if not, -b is assigned to a.