Control Structures and C++: Conditional Structure: If and Else
Control Structures and C++: Conditional Structure: If and Else
INTRODUCTION
A program is usually not limited to a linear sequence of instructions. During its
process it may bifurcate, repeat code or take decisions. For that purpose, C++ provides
control structures that serve to specify what has to be done by our program, when and under
which circumstances. With the introduction of control structures we are going to have to
introduce a new concept: the compound-statement or block. A block is a group of statements
which are separated by semicolons (;) like all C++ statements, but grouped together in a
block enclosed in braces: { }:
{ statement1; statement2; statement3; }
Most of the control structures that we will see in this section require a generic
statement as part of its syntax. A statement can be either a simple statement (a simple
instruction ending with a semicolon) or a compound statement (several instructions grouped
in a block), like the one just described. In the case that we want the statement to be a simple
statement, we do not need to enclose it in braces ({}). But in the case that we want the
statement to be a compound statement it must be enclosed between braces ({}), forming a
block.
CONDITIONAL STRUCTURE: IF AND ELSE
The if keyword is used to execute a statement or block only if a condition is fulfilled.
Its form is:
if (condition) statement
Where condition is the expression that is being evaluated. If this condition is true,
statement is executed. If it is false, statement is ignored (not executed) and the program
continues right after this conditional structure.
For example, the following code fragment prints x is 100 only if the value stored in
the x variable is indeed 100:
if (x == 100)
1
if (x > 0)
while (n>0) {
3
When the program starts the user is prompted to insert a starting number for the
countdown. Then the while loop begins, if the value entered by the user fulfills the condition
n>0 (that n is greater than zero) the block that follows the condition will be executed and
repeated while the condition (n>0) remains being true. The whole process of the previous
program can be interpreted according to the following script (beginning in main):
When creating a while-loop, we must always consider that it has to end at some point,
therefore we must provide within the block some method to force the condition to become
false at some point, otherwise the loop will continue looping forever. In this case we have
included --n; that decreases the value of the variable that is being evaluated in the condition
(n) by one - this will eventually make the condition (n>0) to become false after a certain
number of loop iterations: to be more specific, when n becomes 0, that is where our whileloop and our countdown end.
CONCLUSION
4
Its functionality is exactly the same as the while loop, except that condition in the dowhile loop is evaluated after the execution of statement instead of before, granting at least
one execution of statement even if condition is never fulfilled. For example, the following
example program echoes any number you enter until you enter 0.
REFERENCES
http://www.cplusplus.com/doc/oldtutorial/control/
http://fahad-cprogramming.blogspot.in/2012/06/introduction-to-control-
structures-in-c.html\
http://www.circuitstoday.com/control-structures-in-c-and-cpp
https://www.hscripts.com/tutorials/cpp/cpp-control-structures.php
http://people.scs.carleton.ca/~dehne/projects/cpp-doc/tutorial/tut2-1.html