0% found this document useful (0 votes)
22 views

Control Structures 204

Control structure of computer programming ||

Uploaded by

taibatlawal0
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Control Structures 204

Control structure of computer programming ||

Uploaded by

taibatlawal0
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Control Structures

A control structure is like a block of programming that analyses variables and


chooses a direction in which to go based on given parameters. The term flow
control details the direction the program takes (which way program control
"flows"). Hence it is the basic decision-making process in computing; It is a
prediction.
Control Structures are just a way to specify flow of control in programs. Any
algorithm or program can be more clear and understood if they use self-contained
modules called as logic or control structures. It basically analyzes and chooses in
which direction a program flows based on certain parameters or conditions. There are
three basic types of logic, or flow of control, known as:
1. Sequence logic, or sequential flow
2. Selection logic, or conditional flow
3. Iteration logic, or repetitive flow

1. Sequential Logic (Sequential Flow)


Sequential logic as the name suggests follows a serial or sequential flow in which
the flow depends on the series of instructions given to the computer. Unless new
instructions are given, the modules are executed in the obvious sequence. The
sequences may be given, by means of numbered steps explicitly. Also, implicitly
follows the order in which modules are written. Most of the processing, even some
complex problems, will generally follow this elementary flow pattern.
Sequential Control flow

2. Selection Logic (Conditional Flow)


Selection Logic simply involves a number of conditions or parameters which
decides one out of several written modules. The structures which use these
type of logic are known as Conditional Structures.
The C++ if statement is the most simple decision-making statement. It is
used to decide whether a certain statement or block of statements will be
executed or not executed based on a certain type of condition. These
structures can be of three types:

1. Single Alternative: This structure has the form:


If (condition) then:
[Module A]
[End of If structure]

Syntax
if(condition)
{
// Statements to execute if
// condition is true
}

// C++ program to illustrate If statement

#include <iostream>
using namespace std;

int main()
{
int i = 10;

if (i < 15) {
cout << "10 is less than 15 \n";
}

cout << "I am Not in if";


}
2. Double Alternative:
The if statement alone tells us that if a condition is true it will execute a block
of statements and if the condition is false it won’t. But what if we want to do
something else if the condition is false. Here comes the C++ else statement.
We can use the else statement with if statement to execute a block of code
when the condition is false.

This structure has the form:


If (Condition), then:
[Module A]
Else:
[Module B]
[End if structure]

Syntax
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}

// C++ program to illustrate if-else statement

#include <iostream>
using namespace std;

int main()
{
int i = 20;
// Check if i is 10
if (i == 10)
cout << "i is 10";

// Since is not 10
// Then execute the else statement
else
cout << "i is 20\n";

cout << "Outside if-else block";

return 0;
}

3. Multiple Alternatives
The if-else-if ladder helps the user decide from among multiple options. The C+
+ if statements are executed from the top down. As soon as one of the conditions
controlling the if is true, the statement associated with that if is executed, and the
rest of the C++ else-if ladder is bypassed. If none of the conditions is true, then
the final statement will be executed.
This structure has the form:
If (condition A), then:
[Module A]
Else if (condition B), then:
[Module B]
..
..
Else if (condition N), then:
[Module N]
[End If structure]
// C++ program to illustrate if-else-if ladder

#include <iostream>
using namespace std;

int main()
{
int i = 20;

// Check if i is 10
if (i == 10)
cout << "i is 10";

// Since i is not 10
// Check if i is 15
else if (i == 15)
cout << "i is 15";

// Since i is not 15
// Check if i is 20
else if (i == 20)
cout << "i is 20";

// If none of the above conditions is true


// Then execute the else statement
else
cout << "i is not present";

return 0;
}

Iteration Logic (Repetitive Flow)


The Iteration logic employs a loop which involves a repeat statement followed by a
module known as the body of a loop.
The two types of these structures are:

 Repeat-For Structure
In C++, for loop is an entry-controlled loop that is used to execute a block of
code repeatedly for the specified range of values. Basically, for loop allows you
to repeat a set of instructions for a specific number of iterations.
for loop is generally preferred over while and do-while loops in case the number
of iterations is known beforehand.
This structure has the form:
 Repeat for i = A to N by I:

 [Module]

 [End of loop]

Here, A is the initial value, N is the end value and I is the increment. The loop
ends when A>B. K increases or decreases according to the positive and negative
value of I respectively.

Syntax of for Loop


The syntax of for loop in C++ is shown below:
 for ( initialization; test condition; updation)
 {
 // body of for loop
 }
// C++ program to illustrate for loop to print numbers from
// 1 to n

#include <iostream>
using namespace std;

int main()
{
// initializing n (value upto which you want to print
// numbers
int n = 5;
int i; // initialization of loop variable
for (i = 1; i <= n; i++) {
cout << i << " ";
}

return 0;
}
Repeat-While Structure
It also uses a condition to control the loop. This structure has the form:
Repeat while condition:
[Module]
[End of Loop]

WHILE and DO-WHILE Loops


A WHILE loop is a process in which a loop is initiated until a condition has been
met. This structure is useful when performing iterative instructions to satisfy a
certain parameter. To illustrate:
Precondition: variable X is equal to 1
WHILE X is not equal to 9
Add 1 to X
This routine will add 1 to X until X is equal to 9, at which point the control
structure will quit and move on to the next instruction. Note that when the
structure quits, it will not execute the Add function: when X is equal to 9, it will
skip over the clause that is attached to the WHILE. This instruction is useful if
a parameter needs to be tested repeatedly before acceptance. In the above
example, if X is already equal to or greater than 9 when the program reaches
the WHILE loop, the program will entirely skip the loop.
DO-WHILE Loops
A DO-WHILE loop is nearly the exact opposite to a WHILE loop. A WHILE
loop initially checks to see if the parameters have been satisfied before
executing an instruction. A DO-WHILE loop executes the instruction before
checking the parameters. To illustrate:
DO Add 1 to X
WHILE X is not equal 9
As you can see, the example differs from the first illustration, where the DO
action is taken before the WHILE. The WHILE is inclusive in the DO. As
such, if the WHILE results in a false (X is equal to 9), the control structure
will break and will not perform another DO. Note that if X is equal to or
greater than 9 prior to entering the DO-WHILE loop, then the loop will
never terminate.
FOR Loops
A FOR loop is an extension of a while loop which iterates for a predetermined
length. A for loop usually has three commands. The first is used to set a starting
point (like x = 0). The second is the end condition (same as in a while loop) and is
run every round. The third is also run every round and is usually used to modify a
value used in the condition block.
FOR X is 0. X is less than 10. add 1 to X.
This loop would be run 10 times (x being 0 to 9) so you won't have to think
about the X variable in the loop, you can just put code there. Here is a while
loop that does the same:
X is 0
WHILE X is less than 10
add 1 to X
Some programming languages also have a for each loop which will be
useful when working with arrays (and collections). A for each loop is an
even more automated while loop that cycles through array's and such.
Example :
FOR EACH student IN students
give a good mark to student

You might also like