0% found this document useful (0 votes)
99 views5 pages

Control Structures and C++: Conditional Structure: If and Else

This document discusses control structures in C++, including conditional statements like if/else and iteration structures like while loops. It provides examples of using if/else statements to check conditions and while loops to repeat a block of code until a condition is no longer true. Control structures allow a program to bifurcate, repeat code, or make decisions to specify what should be done, when, and under what circumstances. Blocks delimited by curly braces {} group statements together for control structures.

Uploaded by

Boobalan R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views5 pages

Control Structures and C++: Conditional Structure: If and Else

This document discusses control structures in C++, including conditional statements like if/else and iteration structures like while loops. It provides examples of using if/else statements to check conditions and while loops to repeat a block of code until a condition is no longer true. Control structures allow a program to bifurcate, repeat code, or make decisions to specify what should be done, when, and under what circumstances. Blocks delimited by curly braces {} group statements together for control structures.

Uploaded by

Boobalan R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CONTROL STRUCTURES AND C++

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

cout << "x is 100";


If we want more than a single statement to be executed in case that the condition is
true we can specify a block using braces { }:
if (x == 100)
{
cout << "x is ";
cout << x;
}
We can additionally specify what we want to happen if the condition is not fulfilled by
using the keyword else. Its form used in conjunction with if is:
if (condition) statement1 else statement2
FOR EXAMPLE:
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
prints on the screen x is 100 if indeed x has a value of 100, but if it has not -and only
if not- it prints out x is not 100. The if + else structures can be concatenated with the intention
of verifying a range of values. The following example shows its use telling if the value
currently stored in x is positive, negative or none of them (i.e. zero):

if (x > 0)

cout << "x is positive";


else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
Remember that in case that we want more than a single statement to be executed, we
must group them in a block by enclosing them in braces { }.
ITERATION STRUCTURES (LOOPS)
Loops have as purpose to repeat a statement a certain number of times or while a condition is
fulfilled.
WHILE (EXPRESSION) STATEMENT
// custom countdown using while
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;

while (n>0) {
3

cout << n << ", ";


--n;
}
cout << "FIRE!\n";
return 0;
}

Enter the starting number > 8


8, 7, 6, 5, 4, 3, 2, 1, FIRE!

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

You might also like