Statement Level Control Structures Control Statements Control the

  • Slides: 38
Download presentation
Statement Level Control Structures

Statement Level Control Structures

Control Statements • Control the sequence of execution of statements in a program •

Control Statements • Control the sequence of execution of statements in a program • Influence the readability and writeability of a programming language.

Control Structures A control structure is a control statement along with the collection of

Control Structures A control structure is a control statement along with the collection of statements it controls Control structures should have a single entry point and a single exit.

Control Structures • Compound statements • Selection statements • Iterative statements • Unconditional branching

Control Structures • Compound statements • Selection statements • Iterative statements • Unconditional branching • Guarded commands

Control Structures • Compound statements • Selection statements • Iterative statements • Unconditional branching

Control Structures • Compound statements • Selection statements • Iterative statements • Unconditional branching

Control Structures: compound statements Compound statements allow a collection of statements to be abstracted

Control Structures: compound statements Compound statements allow a collection of statements to be abstracted into a single statement Pascal: begin statement_1; statement_2; … statement_n end

Control Structures: compound statements Compound statements allow a collection of statements to be abstracted

Control Structures: compound statements Compound statements allow a collection of statements to be abstracted into a single statement C, C++, Java: { statement_1; statement_2; … statement_n; }

Control Structures: compound statements Some languages allow data declarations to be made at the

Control Structures: compound statements Some languages allow data declarations to be made at the beginning of a compound statement, forming a block C++, Java: { int num; statement_1; … statement_n; }

Control Structures • Compound statements • Selection statements • Iterative statements • Unconditional branching

Control Structures • Compound statements • Selection statements • Iterative statements • Unconditional branching

Control Structures: selection statements Selection statements provide a means of choosing between two or

Control Structures: selection statements Selection statements provide a means of choosing between two or more execution paths in a program. • single-way selection • two-way selection • multiple selection

Control Structures: selection statements Single-way selection statements : FORTRAN IV: (Logical IF) IF (boolean

Control Structures: selection statements Single-way selection statements : FORTRAN IV: (Logical IF) IF (boolean expression) statement Example: IF (X. GE. 10) X = X-10 To execute a group of statements: 22 IF (X. LT. 10) GO TO 22 X = X-10 WRITE *, X CONTINUE

Control Structures: selection statements Two-way selection statements : design issues - What is the

Control Structures: selection statements Two-way selection statements : design issues - What is the form and type of the expression which controls the selection? - Can the selected statements be a single statement, a sequence of statements or a compound statement? - How to handle the dangling else problem?

Control Structures: selection statements Two-way selection statements : Pascal: if boolean_expression then statement_1 else

Control Structures: selection statements Two-way selection statements : Pascal: if boolean_expression then statement_1 else statement_2; Single-way selector: if boolean_expression then statement_1;

Control Structures: selection statements Two-way selection statements : C: if (expression) statement_1 else statement_2;

Control Structures: selection statements Two-way selection statements : C: if (expression) statement_1 else statement_2; Single-way selector: if (expression) statement_1;

Control Structures: selection statements Two-way selection statements : FORTRAN 77: IF (BOOLEAN_EXP) THEN STATEMENT_1

Control Structures: selection statements Two-way selection statements : FORTRAN 77: IF (BOOLEAN_EXP) THEN STATEMENT_1 STATEMENT_2 … STATEMENT_N ELSE STATEMENT_X STATEMENT Y ENDIF

Control Structures: selection statements The dangling else problem occurs when an else clause follows

Control Structures: selection statements The dangling else problem occurs when an else clause follows two else-less if clauses Pascal: if sum = 0 then if count = 0 then result : = 0 else result : = 1; 1; Pascal resolves this conflict semantically, it pairs the else with the nearest if.

Control Structures: selection statements The dangling else problem occurs when an else clause follows

Control Structures: selection statements The dangling else problem occurs when an else clause follows two else-less if clauses ALGOL: if sum = 0 then begin if count = 0 then result : = 0 else end else result : = 1; result end : = 1; ALGOL resolves this conflict syntactically; it requires a begin-end pair

Multiple Selection Constructs Multiple selection statements : design issues - What is the form

Multiple Selection Constructs Multiple selection statements : design issues - What is the form and type of the expression which controls the selection? - Can the selected statements be a single statement, a sequence of statements or a compound statement? - Does the execution flow select just a single statement? - How should unrepresented expression values be handled?

Multiple Selection Constructs • multi-way if statement • case statement in Pascal, Algol •

Multiple Selection Constructs • multi-way if statement • case statement in Pascal, Algol • switch statement in C

Multiple Selection Constructs • multi-way if statement (C) if (value == 1) answer =

Multiple Selection Constructs • multi-way if statement (C) if (value == 1) answer = sum + x; else if (value == 2) answer =sum * x; else if (value== 3||value==4||value==5) answer = sum - x; else answer = sum / x;

Multiple Selection Constructs • case statement (Pascal) case value of 1: answer 2: answer

Multiple Selection Constructs • case statement (Pascal) case value of 1: answer 2: answer 3, 4, 5: answer else answer end {case} : = : = sum sum + * / x; x;

Multiple Selection Constructs • case statement (Ada) case value is when 1 when 2

Multiple Selection Constructs • case statement (Ada) case value is when 1 when 2 when 3. . 5 when others end case; => => answer : = : = sum sum + * / x; x;

Multiple Selection Constructs • switch statement (C) switch (value) { case 1: answer break;

Multiple Selection Constructs • switch statement (C) switch (value) { case 1: answer break; case 2: answer break; case 3: case 4: case 5: answer break; default: answer } /* switch */ = sum + x; = sum * x; = sum - x; = sum / x;

Multiple Selection Constructs • switch statement (C) switch (value) { case 1: answer case

Multiple Selection Constructs • switch statement (C) switch (value) { case 1: answer case 2: answer case 3: case 4: case 5: answer default: answer } /* switch */ = sum + x; = sum * x; = sum - x; = sum / x; here the flow falls through from previously occurring cases. answer is always sum / x

Control Structures • Compound statements • Selection statements • Iterative statements • Unconditional branching

Control Structures • Compound statements • Selection statements • Iterative statements • Unconditional branching

Control Structures: iterative statements Iterative statements allow a statement or group of statements to

Control Structures: iterative statements Iterative statements allow a statement or group of statements to be executed zero, one or more times. • counter controlled loops • logically controlled loops

Control Structures: iteration statements Iteration statements : design issues - What is the form

Control Structures: iteration statements Iteration statements : design issues - What is the form and type of the expression which controls the iteration? - Where should the control mechanism appear in the loop?

Control Structures: iteration statements Logically controlled statements : Pascal: while expression statement repeat statement_1;

Control Structures: iteration statements Logically controlled statements : Pascal: while expression statement repeat statement_1; statement_2; statement_n; until expression;

Control Structures: iteration statements Logically controlled statements : C: while (expression) statement do statement;

Control Structures: iteration statements Logically controlled statements : C: while (expression) statement do statement; while (expression);

Control Structures: iteration statements Counter controlled loops - loop variable - initial & terminal

Control Structures: iteration statements Counter controlled loops - loop variable - initial & terminal values - stepsize

Control Structures: iteration statements Counter controlled loops: Design issues - type and scope of

Control Structures: iteration statements Counter controlled loops: Design issues - type and scope of loop variable - value of loop variable upon loop termination - can loop variable be changed inside the loop, if so does it affect the loop control? - is test for termination at top or bottom of loop? - are the loop parameters evaluated once, or once for every iteration?

Control Structures: iteration statements Counter controlled loops: Pascal: for j : = 1 to

Control Structures: iteration statements Counter controlled loops: Pascal: for j : = 1 to 10 do writeln (j); for j : = 10 downto 1 do writeln (j);

Control Structures: iteration statements Counter controlled loops: C: for (j = 1; j <=

Control Structures: iteration statements Counter controlled loops: C: for (j = 1; j <= 10; j++) printf (j); for (j = 10; j >= 1; j--) printf (j); for (j = 10; j >= 1; j-=2) printf (j);

Control Structures: iteration statements Counter controlled loops: FORTRAN: 20 do 20 j=1, 10 print(j)

Control Structures: iteration statements Counter controlled loops: FORTRAN: 20 do 20 j=1, 10 print(j) continue 30 do 30 j=10, 1, 2 print(j) continue

Control Structures • Compound statements • Selection statements • Iterative statements • Unconditional branching

Control Structures • Compound statements • Selection statements • Iterative statements • Unconditional branching

Control Structures: Uncontrolled Branch An uncontrolled branch transfers control to a specified place in

Control Structures: Uncontrolled Branch An uncontrolled branch transfers control to a specified place in the program Design issues: • label forms • restrictions on branching

Control Structures: Uncontrolled Branch An uncontrolled branch transfers control to a specified place in

Control Structures: Uncontrolled Branch An uncontrolled branch transfers control to a specified place in the program ALGOL and C: (identifiers for labels) goto Error; … Error: { printf(“An error has occurredn”); exit(1); }

Control Structures: Uncontrolled Branch An uncontrolled branch transfers control to a specified place in

Control Structures: Uncontrolled Branch An uncontrolled branch transfers control to a specified place in the program FORTRAN and Pascal: goto 444; (integer constants for labels)