Quick Index
What Is A Conditional Statement?
Algorithmic Perspective
Everyday Conditions With Actions
If True Code Block
If True Else False
What Is A Conditional Statement?
Recall the definition of
condition
.
A
conditional statement
is a conditional expression followed by a code block that executes
if
the expression is true. Similarly we may have a code block for false.
The next examples will explain.
Algorithmic Perspective
General
If the condition (expression) is true, then code "A" evaluates (runs).
If (CONDITION)
{
//CODE "A"
}
📋
Specific Example
The condition is "a > b".
Thus, if the element "a" is greater than the element "b", then code "A" evaluates (runs).
If (a > b) { //CODE "A" }
Everyday Conditions With Actions
When the
condition
is true we do a specified
action.
Condition
Result
Action
(Code Block
When True)
Is the traffic signal green?
no
Stop
Am I thirsty?
true
Drink water
Is it cold out?
true
Put hat on
Is the price fair?
false
Do not buy
If True Code Block
If the condition expression evaluates to true, we do an action (code block).
Note we show print as the actions here to keep the example short. But in reality we can (and will) put all kinds of code into code these blocks.
Condition
Result
Code Block
Runs
When True
rec.isSquare()
true
Print "Square!"
rec1.canFitInsideOf(rec2)
false
-
If True Else False
The condition is either true
else
it is false. We can add a code block that will run for the
else
case.
Condition
Result
Code Block
Runs
When True
Code Block
Runs
When False
(Else Case)
rec.isSquare()
true
Print "Square!"
-
rec1.canFitInsideOf(rec2)
false
-
print "Nope won't fit"
Coding Fundamentals (Chapter 104 - Conditional Statements)
CHAP103 <
104 - 104 - Conditional Statements
> CHAP105
Chapter
Top
Search
TOC