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

Simple Control Structures: (Statement1 Statement2 Statement3 )

The document discusses simple 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 repeatedly execute code until a condition is no longer true.

Uploaded by

Indhu Mcamphil
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)
41 views5 pages

Simple Control Structures: (Statement1 Statement2 Statement3 )

The document discusses simple 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 repeatedly execute code until a condition is no longer true.

Uploaded by

Indhu Mcamphil
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

Simple control structures

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 statementit 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:
1 if (x == 100)
2 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 { }:
1 if (x == 100)
2{
3 cout<< "x is ";
4 cout<< x;
5}

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:
1 if (x == 100)
2 cout<< "x is 100";
3 else
4 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):
1 if (x > 0)
2 cout<< "x is positive";
3 else if (x < 0)
4 cout<< "x is negative";
5 else
6 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.
The while loop
Its format is
while(expression)statement
and its functionality is simply to repeat statement while the condition set in
expression is true.
For example, we are going to make a program to countdown using a while-loop:
1 // custom countdown using while Enter the starting number > 8
2 #include <iostream> 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
3 using namespace std;
4 int main ()
5 {
6 int n;
7 cout<< "Enter the starting number >
8 ";
9 cin>> n;
10 while (n>0) {
11 cout<< n << ", ";
12 --n;
13 }
14 cout<< "FIRE!\n";
15 return 0;
16 }
17
18
19

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):
1. User assigns a value to n
2. The while condition is checked (n>0). At this point there are two
possibilities:
3. * condition is true: statement is executed (to step 3)
* condition is false: ignore statement and continue after it (to step 5)
4. Execute statement
cout<< n << ", ";--n;
(prints the value of n on the screen and decreases n by 1)
5. End of block. Return automatically to step 2
6. Continue the program right after the block: print FIRE! and end program.
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 while-
loop and our countdown end.
Of course this is such a simple action for our computer that the whole
countdown is performed instantly without any practical delay between numbers.

You might also like