If
statements are among the most simple and the most easily understood control
statement. However, you shouldn't mistake their simplicity as an indication of
uselessness - if statements are both powerful and frequently used in
contemporary programs written in a variety of programming languages. If
statements follow one of three basic patterns:
1. if
2. if-else
3. if-else ladder
Simple If Statements
A simple if statement either executes a
statement (including a block statement) or not, depending on the outcome of a
test. The test is generally based on the value or values stored one or more
variables. (It's possible to write more simple tests that don't depend on any
variables but they are rarely useful when used with if statements.) As the
values stored in the variables change during program execution, the outcome of
the test also changes, which means that the statements inside the if statement
execute sometimes but not other times. The following flow c
The code
fragments below demonstrate the basic pattern of a simple if statement. The
definition of a statement given previously is applied recursively here. That is
Statement 1 is a single statement but so is if (Test) Statement 1 (the complete
statement ends with a single semicolon). Furthermore, the second and third
examples are also a single if-statement; the braces form a block that the "if"
treats like a single statement (note that a semicolon is not required after the
closing brace). Finally, in the second example, the braces surrounding
"Statement 1" are optional - some programmers always use braces but
that style is not universally followed. Hart illustrates the behavior of a
simple if statement.
if-Statement Variations.
a. An
if-statement containing a single statement
if (Test)
Statement 1;
b. An
if-statement with optional braces
if (Test)
{
Statement 1;
}
c. An
if-statement based on a compound or block statement; the braces are required
if (Test)
{
Statement 1;
Statement 2;
}
Examples
#include
<iostream>
using namespace std;
int main()
{
int x = 10;
if (x > 0)
cout << "it is greater
than\n";
return 0;
}
Output
it
is greater than
If-Else Statements
Whereas a
simple if statement chooses to execute a statement or not, an if-else statement
chooses to execute one of two statements (or blocks of statements) as
illustrated in the figure below.
An if-else
statement: if the test is true, then the statement 1 is executed; if the test
is false, then statement 2 is executed. It is the keyword "else" that
determines which is statement 1 and which is statement 2: statement 1 precedes
"else" and statement 2 follows. As before, braces are optional when a
single statement appears in either the "if" or the "else"
branch.
if-else Statement
Variations.
a.
When
a single statement appears in either the true or false brance, braces are
optional
if (Test)
Statement 1;
else
Statement 2;
b.
Braces
are required whenever more than one statement appears in a branch
if (Test)
{
Statement 1;
.
.
.
Statement n;
}
else
{
Statement 1;
.
.
.
Statement m;
}
Example
#include <iostream>
using namespace
std;
int main()
{
int x = 5;
double y = 3.14;
if (x > y && x != 10)
cout << "true\n";
else
cout << "false\n";
return 0;
}
Output
True
If-Else Ladder
An if-else
ladder does not introduce any additional keywords or symbols. Each new rung of
the ladder is formed by an if-else statement nested inside the else part of the
previous if-else statement. If-else ladders also represent an exception to the
rule of thumb presented earlier to restrict nesting to no more than four or
five levels; the nesting takes place deeply enough to accommodate every
possible branch required by the problem logic.
An if-else
ladder: The first test is evaluated and if it is true then statement 1 is
executed and the ladder ends. If the first test is false, then test 2 is
evaluated and if it is true then statement 2 is executed and the ladder ends.
Simply put, the tests are evaluated from the top to the bottom and the
statement(s) associated with the first true test is/are evaluated. The ladder
ends whenever any test is true and the associated statements execute. The
ladder may optionally end with an else without an if and the associated
statement(s) execute if none of the tests are true. If the ladder does not have
an ending else and none of the tests are true, then no statements in the ladder
execute.
if-else Ladder Variations
1.
if-else
ladder without an ending else; no statements are executed if all of the tests
evaluate to false
if (Test 1)
Statement 1;
else if (Test 2)
Statement 2;
else if (Test 3)
Statement 3;
else if (Test n)
Statement n;
2.
if-else
ladder with an else at the end; statement m executes if all of the tests
evaluate to false
if (Test 1)
Statement 1;
else if (Test 2)
Statement 2;
else if (Test 3)
Statement 3;
else if (Test n)
Statement n;
else
Statement m;
}
3.
An
if-else ladder with block statements
if (Test 1)
{
Statement 1;
Statement 2;
. . . .
Statement j;
}
else if (Test 2)
{
Statement 1;
Statement 2;
. . . .
Statement k;
}
else if (Test 3)
{
Statement 1;
Statement 2;
. . . .
Statement l;
}
else if (Test n)
{
Statement 1;
Statement 2;
. . . .
Statement m;
}
else
{
Statement 1;
Statement 2;
. . . .
Statement n;
}
Example
#include <iostream>
using namespace
std;
int main()
{
int score = 87;
if (score >= 95)
cout << "Grade: A\n";
else if (score >= 90 &&
score < 95)
cout << "Grade: A-\n";
else if (score >= 85 &&
score < 90)
cout << "Grade: B+\n";
else if (score >= 80 &&
score < 85)
cout << "Grade: B\n";
else if (score >= 75 &&
score < 80)
cout << "Grade: B-\n";
else (score < 75)
cout << "Grade: E\n";
return 0;
}
Output