The document discusses conditional statements in programming. Conditional statements check an expression and execute code depending on whether the expression is true or false. The main conditional statements are IF statements and IF-ELSE statements. An IF statement executes code if the expression is true, while an IF-ELSE statement executes the IF code if true and the ELSE code if false. Examples are provided to illustrate how to use IF and IF-ELSE statements to check conditions and produce different outputs.
2. Conditional Statement
Conditional statement are statements that check
an expression then may or may not execute a
statement or group of statements depending on
the result of the condition.
3. The IF statement
The general form of the if statement is:
if (expression)
statement;
where:
if is a reserve word in Turbo C
expression is relational or boolean expression that evaluates
to a TRUE (1) or FALSE (0) value
statement may either be a single Turbo C statement or a
block of Turbo C statements.
4. The IF statement
The general form of the if statement with block
of statements is:
if (expression)
{
statement_sequence;
}
In an if statement, if the expression evaluates to TRUE(1),
the statement or the block statements that forms the target of
the if statement will be executed. Otherwise, the program
will ignore the statement or the block of statements.
5. The IF statement
Note:
Never place a semicolon after the expression in
an if statement.
6. Example 1
Write a program that will output
“Congratulations you PASSED!” if the student’s
grade is greater than or equal to 60.
7.
8.
9. Example 2
Write a program that will ask for a price. If the
price is greater than 1000, compute a 10%
discount from the original price. Display the
computed discount.
10.
11.
12. The IF-ELSE statement
The general form of the if-else statement is:
if (expression)
statement_1;
else
statement_2;
13. The IF-ELSE statement
where:
if is a reserve word in Turbo C
expression is relational or boolean expression
that evaluates to a TRUE (1) or FALSE (0)
value
statement_1 and statement_2 may either be a
single Turbo C statement or a block of Turbo C
statements.
14. The IF-ELSE statement
The general form of the if-else statement with
block of statements is:
if (expression)
{
statement_sequence;
}
else
{
statement_sequence;
}
15. The IF-ELSE statement
If an if-else statement, if the expression is
TRUE (1), the statement or block of statements
after the if statement will be executed;
otherwise, the statement or block of statements
in the else statement will be executed.
16. The IF-ELSE statement
Note:
Only the code associated with the if or the code
that is associated with the else executes, never
both.
17. Example 3
Write a program that will output
“Congratulations you PASSED!” if the student’s
grade is greater than or equal to 60. Otherwise
output, “Sorry you FAILED!”