W6-Module-What About Ifs
W6-Module-What About Ifs
So what are we waiting for? Let us continue our exploration of the world of
Computer Programming.
Course Module
Recall
We have enclosed actual code logic inside a code block. This code block
belongs to “int main( )” function. It means everything inside belongs only to
the code block of “int main( )” function.
Variables
Since we are talking about code blocks, we need to discuss various things
about variables and how they are affected by code blocks. We have global
variables and local variables. Global variables are variables declared outside
of the “int main( )” function. They are usable anywhere in the same line of the
declaration, and within the code blocks like the “int main( )” function code
block. Local variables are variables declared within a specific code block and
is accessed and used only within the said code block.
Course Module
Figure 2. Global and Local Variables! Source Code
We only have covered the declaration and retrieval part of variables, but how
about assignment? We can always assign values to our variables as long as
we can retrieve their value. In addition, the value we stored in that variable
stays with the variable even if the code block where it was done is exited.
These scenarios will be clearer once we have written our first non-standard
code block, a conditional.
Introduction to Conditionals
If Statements
Ifs statements are used when we have an undefined conditional expressions,
may it be a single expression or multiple expressions.
Figure 3. If Statement
Course Module
Now, what if we want to handle a scenario wherein our initial if statement
evaluates to false? We can use the If…Else statements.
In Figure 4, we introduced the else statement in line 17. The else code block,
from line 16 to 18, will only be taken in consideration should the if statement
evaluates to false. In this specific example since line 12 is always true, lines
16 to 18 will never be executed.
Again if we modify our line 12 to always evaluate to false (e.g. “if(0) {” ), then
our line 16 to 18 will always be executed. Just remember that the “else”
statements will only be executed if the “if” statements before them are not
executed.
Lastly, how about if we have more than one condition to consider? Then we
can use a compound if statements.
Now let us evaluate our application, in line 13 we evaluate whether the value
of variable “b” is equal to exactly 1. Since this is always true, the value of
variable “a” will be multiplied by 1. Therefore come line 19, the value of
variable “a” is still 123.
If we modify our application and changed the initial value of variable “b” to 2,
and execute the application, we will have:
Line 11 will evaluate to false since variable “b” is not equal to 1.
Line 12 will not be executed.
Line 13 will evaluate to true since variable “b” is equal to 2.
Line 14 will be executed.
Line 15 will not be passed by since line 13 is already executed.
Line 16 will not be executed.
If we modify our application and changed the initial value of variable “b” any
value except 1 and 2 (e.g. “int b = 3”), and execute the application, we will
have:
Course Module
Line 11 will evaluate to false since variable “b” is not equal to 1.
Line 12 will not be executed.
Line 13 will evaluate to false since variable “b” is not equal to 2.
Line 14 will not be executed.
Line 15 will be executed since lines 11 and 13 evaluated to false.
Line 16 will be executed.
Noticed how the value of variable “a” is affected by what the value of var iable
“b” is? This is a simple demonstration of the power of using conditionals.
Switch Statements
For this to make more sense, let us change the value of variable “b” to 3. What
will be the displayed value of variable “a”?
a. 123
b. 246
c. 369
d. 1230
The answer is “C”. The switch statement will jump to “case 3:” since the value
of variable “b” is 3. However since there are no break statements under “case
3:”, the statements under “case 4:” will be executed. Hence, we have variable
“a” multiplied by 3 resulting to the value to be 369.
Nesting of Conditionals
Course Module
Figure 7. Nested Conditionals
Glossary
Case Statements: Part of a SWITCH statement which defines the constant
where the variable-under-parameter is being compared.
Code Block: A series of related statements that are enclosed within curly
brackets/braces.
Conditionals: Code blocks that are executed only when the specified
conditions are met.
Default Statements: Part of a SWITCH statement which defines an
alternative execution should all of the CASE statements fail.
Else If Statements: Part of an IF statement that extends possibilities to cover
other scenarios not under the original IF.
Else Statements: Part of an IF statement that presents an alternative
execution should all of the conditions under the IF and ELSE IF statements
fail.
Global Variables: Variables that are declared outside of the “int main( )”
function that are usable anywhere within the source code.
If Statements: Conditional that execute the statements within if a series of
non-constant conditions are met.
Local Variables: Variables that are declared inside a code block that are only
usable within the code block.
Switch Statements: Conditional that compares one specific variable against
a set of defined constants or values.
Course Module
References and Supplementary Materials
Books and Journals
2014; Learn C++ Programming Language; India; Tutorial Point
Richard Halterman; 2017; Fundamentals of C++ Programming; United States
of America; Southern Adventist University