Upload
ra-na
View
23
Download
1
Embed Size (px)
Citation preview
STATEMENT LEVEL CONTROL STRUCTURES
1.Introduction2. Compound Statements3.Selection Statements4.Iterative Statements5.Unconditional Branching
INTRODUCTIONSelection, 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 stored program of the single address type.
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
SECTION STATEMENTS
A Section statements provides that means of choosing between two or more paths of execution.
Two way selectorsGeneral form:If control expression(condition)Then clauseElse clause
ALGOL 60:If (Boolean expr)Then statement(then clause)Else statement(then clause)NOTE: the statement could be single or compound
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 likeIf (sum == 0) {If (count == 0)Result = 0; }Else result = 1; NOTE: every requires that all then and else clauses to be compound.
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; }
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
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.
<?phpclass 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 => $value\n"; } }}
$class = new MyClass();foreach($class as $key => $value) { print "$key => $value\n";}print "\n";$class->iterateVisible();?>
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
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
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.
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; End.
UNCONDITIONAL BRANCHINGWell known mechanism: go to statement.Major concern: readabilityTransfers execution control to a specified place in the programSome languages do not support go to statement(java )C# offers go to statement(can be used in switch statements)
SYNTAX OF STATEMENTS‘:C S :: = ;
E;{ Slist }if ( E ) Sif ( E ) S else Swhile ( E ) Sdo S while ( E ) ;for ( Eopt ; Eopt; Eopt ) Sswitch ( E ) SCase Constant : S
SYNTAX OF STATEMENTS:Cdefault : S
break ;continue ;return ;return E ;goto L ;L : S
Slist :: = ( empty )Slist S
Eopt ::= ( empty ) E
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.
PROGRAM PROOF PROCESSA 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 resultwork 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
END OF THE PRESENTATION