2. INTRODUCTION
Selection, unconditional branching and Iterative
statements are a control structure. These control
structure are control statement.
Control Statements are select and repeat control
flow whereas statements in FORRTRAN I were based
directly on IBM 704 Hardware.
The IBM 704 ,introduced by IBM in 1954,was the
first mass-produced computer with floating-point
arithmetic hardware and it is the manual of
operation states. The type 704 Electronic Data-
processing Machine is a large-scale, high-speed
electronic calculator controlled by an internally
3. CONTROL STRUCTURE
Design question should a control structure have multiple entries?
Structure programming:
structure of the program text should be help us for undersetting
what the program does?
Flow chart control into the program as a result we can find out
syntactic structure of the program text like single entry or exit
4. SECTION STATEMENTS
A Section statements provides that means of choosing between two or
more paths of execution.
Two way selectors
General form:
If control expression(condition)
Then clause
Else clause
ALGOL 60:
If (Boolean expr)
Then statement(then clause)
Else statement(then clause)
NOTE: the statement could be single or compound
5. SELECTORS OF NESTING
Which if gets the else?
Static semantics rule in java, c , c++ and c# else matches with nearest if.
Example of java:
If (sum == 0)
If (count == 0)
Result = 0;
Else result = 1;
To force an alternative semantics, compound statements may be used like
If (sum == 0) {
If (count == 0)
Result = 0; }
Else result = 1;
NOTE: every requires that all then and else clauses to be compound.
6. SELECTION STATEMENT:MULTIPLE WAY
Allow selection of one of any number with statement or group statement like switch statement
Switch (expression){
Case const_expr_1: statement 1;
Case const_expr_2: statement 2;
-----------
Case const_expr_n: statement n;{Default:statement n+1]
}
Example:
Switch(rana){
Case 1:
Case2:odd+= 1; sumodd +=rana; break;
Case2:even+=1;sumeven +=rana;break; }
7. MULTIPLE WAY SECTION FOR IF STATEMENT
Multiple way can be appear two way sector from direct extension like if and
else in the following way:
If…………………
then……………………..
Elseif………..
then…………………..
Elseif………………
Then…………………………
Else……….
End if
8. ITERATIVE STATEMENTS
The repeated execution of a statement or compound statement is accomplished either by iterative and
the general statement are(A)Where is the control mechanism in the loop.(B)How is iteration controlled?
Object iteration of Php: PHP 5 provides a way for objects to be defined. so it is possible to iterate
through a list of items. for example a foreach statement.
<?php
class MyClass{
public $var1 = 'value 1';
public $var2 = 'value 2';
public $var3 = 'value 3';
protected $protected = 'protected var';
private $private = 'private var';
function iterateVisible() {
print "MyClass::iterateVisible:n";
foreach ($this as $key => $value) {
print "$key => $valuen"; } }}
$class = new MyClass();
foreach($class as $key => $value) {
print "$key => $valuen";}
print "n";
$class->iterateVisible();
9. OUTPUT OF ITERATIVE STATEMENTS: EXAMPLE
var1 => value 1
var2 => value 2
var3 => value 3
MyClass::iterateVisible:
var1 => value 1
var2 => value 2
var3 => value 3
protected => protected var
private => private var
10. OBJECT ITERATION OF C
For statement:
For (expression1;expression2;expressions)
Statement;(1=initialises; 2 =terminate test; 3= modifier )
For example:
Int x;
Main() {
For (x=3;x>0;x--){
Printf(“x=%d n”,x);
}}
Out put 321
11. COUNTER CONTROLLED LOOPS: C++
C++ differs from c in two ways:
1.the control expression can be Boolean.
2.the initial expression can include variable definitions.
Example:
for (int count = 1;count <=10;count++)
{…}
Java and c#
Differs from c++ in that the control expression must be Boolean.
12. LOGICALLY CONTROLLED LOOPS: PASCAL
Pascal has separate pre-test and post-test logical loop statements(while do
and repeat until)
Example of remove adjacent duplicates:1,1,2,2,2,3,1,4,4 ->1,2 3,1,4
Program uniq(input outpot);
Var x,next : integer;
Begin Read(x);
While x<>0 do begin
WriteIn(x);
Repeat read (next);until next<>x;
X:= next;
End;
13. UNCONDITIONAL BRANCHING
Well known mechanism: go to statement.
Major concern: readability
Transfers execution control to a specified place in the program
Some languages do not support go to statement(java )
C# offers go to statement(can be used in switch statements)
14. SYNTAX OF STATEMENTS‘:C
S :: = ;
E;
{ Slist }
if ( E ) S
if ( E ) S else S
while ( E ) S
do S while ( E ) ;
for ( Eopt ; Eopt; Eopt ) S
switch ( E ) S
Case Constant : S
15. SYNTAX OF STATEMENTS:C
default : S
break ;
continue ;
return ;
return E ;
goto L ;
L : S
Slist :: = ( empty )
Slist S
Eopt ::= ( empty )
E
16. PRECONDITION AND POST
CONDITION
A Post conditions are sometimes tested using assertions within the code itself. however post conditions
are simply included in the documentation of the affected section of code.
17. PROGRAM PROOF PROCESS
A weakest precondition is the least restrictive precondition that will
guarantee the postcondition.
For example: -a = b+1{a>1}
-one possible precondition: {b>10}
-weakest precondition :{b >0}
The post condition for the entire program is the desired result
work back through the program to the first statement. If the
precondition on the first statement is the same as the program
specification, the program is correct