PLC Session 6

Session 6: Control Structure Statement

 

A. Control Structure

A control structure is a container for a series of function calls, instructions and statements that dictate the flow of control. There are three basic types of control structures such as sequential, selection and iteration. They can be combined in any way to solve a specified problem.

 

B. Sequence control structure

The sequence control structure is the default or overriding structure. The sequence structure indicates instructions are to be executed one statement at a time in the order they occur from top to bottom unless a different control structure dictates otherwise.

The sequence control structure is used everytime we codes the program.

 

C. Selection Control Structure

Before knowing more about selection control structure , we must know about the conditional statement .

Conditional statement has the ability to test a variable against a value and act in one way if the condition is met by the variable or another way if not.  If it is true , the following block of code will be executed , either way it will do nothing .

IF Statement is one of the most used conditional statement . The IF statement lets you execute a sequence of statements conditionally. That is, whether the sequence is executed or not depends on the value of a condition.

condition is any variable or expression that returns a Boolean value (TRUE or FALSE). This condition can be called the control expression . Control expression is an expression that have a value of boolean and placed in parentheses where there is the conditional statement.

The selection control structure allows one set of statements to be executed if a condition is true and  allows another set of actions to be executed if a condition is false and another set of actions is available.

There are two categories of selection control structure : Two-way selector and Multiple-way selectors.

 

  1. Two- Way selector

Two-way selector makes the conditonal statement has an alternative sequence of statements to be done if the first conditional statement has a value of false.

In C++ language , it has the same syntax like the one that has been used in C language . The Syntax is IF ELSE . There is another one called switch case but it is not effective when you only have two condition.

Switch-case

Switch(a) —à value of variable is used to decide the case

{

Case 1 : break;

Case 2: break;

Default : ;

}

The most important difference between switch case and if else is , in the if else control expression , it returns a boolean value , in switch case , it uses the value of variable inside the parentheses to decide which case to be executed.

switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

 

Example in a pseudo-code:

 

IF condition THEN

sequence_of_statements1

ELSE

sequence_of_statements2

END IF;

 

In this case, IF the variable is true, it takes a certain course of action to executedthe sequence of statements 1 and completely skips the ELSE clause. So in oher way ,the sequence of statements in the ELSE clause is executed only if the condition is false or null. Thus, the ELSE clause ensures that a sequence of statements is executed.

In some source code, ELSE   can has the conditional statement too . It will be described in multiple-way selectors.

  1. Multiple-way Selectors

Sometimes you want to select an action from several mutually exclusive alternatives. So we used this kind of  selection control Structure.

The syntax in C++ language is IF ELSE and switch-case.

Just saying that , case in switch case can be added as much as you want that makes it possible to do a multiple way selection.

 

IF condition1 THEN   sequence_of_statements1ELSE IF condition2 THEN   sequence_of_statements2ELSE   sequence_of_statements3END IF; In this code of block ,  IF the first condition is false or null, the ELSE IF clause tests another condition. An IF statement can have any number of ELSEIF clauses; the final ELSE clause is optional. Conditions are evaluated one by one from top to bottom. IF any condition is true, its associated sequence of statements is executed and control passes to the next statement.IF all conditions are false or null, the sequence in the ELSE clause is executed.

 

 

 

NESTED IF

It is an IF inside an IF ! It means that you can use one if or else if statement inside another if or else if statement(s).

 

Example in pseudo-code :

 

IF( boolean_expression 1)

{

// Executes when the boolean expression 1 is true

 

IF(boolean_expression 2)

{

// Executes when the boolean expression 2 is true

}

}

 

Nested if can be used in both two-way selector and multiple-way selector. This kind of IF provided us multiple condition inside one condition .

D. Iterative Statements

An iterative statement is one that causes a statement or collection of statements to be executed 0 or 1 or more times.  Every programming language has included some method of repeating the execution of segments of code. Iteration is the very essence of the power of the computer.

 

Types of controlled loop :

  • Counter Controlled Loop
  • Logically Controlled Loop
  • Iteration Based on Data Structures

 

  1. Counter Controlled Loop

A counting iterative control statement has a variable, called the loop variable, in which the count value is maintained.  This includes some means of specifying the initial and terminal values of the loop variable and the difference between sequential loop variable values, often called the step-size.

 

In C++ languages , the most common one is for statement.

 

Example in C++ languages (but commonly used in this kind of syntax):

for ([Expr_1] ; [Expr_2] ; [Expr_4])

{

Expr_3

}

Expr_1 : Initialization , very thing to be done hen executing the statement and only once . You are not required to put a statement here, as long as a semicolon appears.

 

Expr_2 : the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

 

Expr_3 : After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

 

Expr_4 : After the body of the for loop executes, the flow of control jumps back up to the increment / decrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

 

After expr_4 is executed then the condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

 

Counter-controlled loop happens as the code get executed , it was meant to be executed until it is not returning a true value in expr_2  where in expr_4 , we control the variable until it return false value.

 

 

 

 

 

  1. Logically Controlled Loop

In many cases, collections of statements mist be repeatedly executed, but the repetition control is based on a Boolean expression rather than a counter.  For these situation, a logically controlled loop is convenient.

 

Syntax in C++ language : while , do while .

 

The difference between while  and do while is the validation of condition .

There are two kind of validation of condition : Post Test and Pre Test.

 

With while function, it is checking the condition first ,If it returns true then it will do the following block of the code . In this one , it checks first , that’s why it’s the Pre Test

 

With do while function, it will execute, at least once , the following block of the code then it will check the condition whether it is true or not. In this one , it executes the block first then checks it whether it is true or not , that’s why it’s the Post Test.

 

  1. Iteration Based on Data Structure

Rather than have a counter or Boolean expression control iterations, these loops are controlled by the number of elements in a data structure.

C provides to commands to control how they loop :

  • break — exit form loop and switch
  • continue — skip 1 iteration from loop

Source(s) :

http://www.valid-computing.com/control-structures.html

https://www.reference.com/technology/three-types-control-structures-13a5ca2c10e15fbf

http://virtual.parkland.edu/kcouch/CIS122/Week3/Sequence%20Control%20Structure.htm

http://www.thevbprogrammer.com/Ch05/05-03-SelectionStructure.htm

https://docs.oracle.com/cd/A87860_01/doc/appdev.817/a77069/03_struc.htm

https://opentechschool.github.io/python-beginners/en/conditionals.html

https://www.tutorialspoint.com/cplusplus/cpp_nested_if.htm

https://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm

http://groups.engin.umd.umich.edu/CIS/course.des/cis400/maxim/lectures/chp7.htm

https://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

http://www.tutorialspoint.com/ansi_c/c_control_statements.htm

 

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.