introduction selection statements -control expression -single/compound clauses -dangling else...
TRANSCRIPT
INTRODUCTIONSELECTION STATEMENTS-Control Expression-Single/Compound Clauses-Dangling ElseMUTLIPLE SELECTION CONSTRUCTS-C/Java Switch-C# Switch-Ada CaseITERATIVE STATEMENTS-Counter controlled
-Fortran 95-ADA-C-Based
-Logically Controlled-Pretest-Postest
USER-LOCATED LOOP CONTROLDATA STRUCTURE ITERATIONGUARDED COMMANDS
LECTURE OUTLINE FOR:CHAPTER 8
CONTROL FLOW
GOTO 10 ! jump forward 23 CONTINUE i = i - 1 IF (i .eq. 0) GOTO 99 10 PRINT*, "Line 10" j = 2 69 j = j - 1 ! loop ... IF (j .ne. 0) GOTO 69 GOTO 23 ! jump back 099 CONTINUE
What is the OUTPUT of this FORTRAN program?
if (sum==0) if (count == 0)result = 0; else result = 1;
Fill in the chart below.
sum count result ------------------------------------------- 0 0 ? 0 1 ? 1 0 ? 1 1 ?
if (sum==0) if (count == 0)result = 0; else result = 1; cout << “Result is set to one!” << end;
Fix this code.
Convert this C++ Dangling Else to Ada.
if (sum==0) if (count == 0)result = 0; else result = 1;
switch (expression) { case constant_expression1: statement_1; ... case constant_expressionN: statement_n; [default: statement_n+1]
}
Multiple Selection Statements (C-Based Syntax)
Is this a valid C++ switch statement?
int i=0, j=1, value; switch (value=(1+2-2)) { case i: cout << “it was i” << endl; break; case j: cout << “it was j” << endl; break; }
C# Switch Statementswitch (index) { case 1:
goto case 3; case 3: odd += 1; sumodd += index; break; case 2:
goto case 4; case 4: even += 1; sumeven += index; break; default: Console.Writeline(“Error bad index!”); break;
}
Multiple Selection Statements (Ada Syntax)
case expression is when choice list => sequence-of-statements when choice list => sequence-of-statements when others => sequence-of-statements end case;
type Directions is (North, South, East, West); Heading : Directions; X : Integer; y : Integer; Heading := East; X := 0; Y := 0;
case Heading is when North => Y := Y + 1; when South => Y := Y - 1; when East => X := X + 1; when West => X := X - 1; end case;
What are values of X and Y?
for (row = 0; row < numRows; row++) { for (col = 0; col < numCols; col++) { sum += mat[row][col]; } }
Terminate loops when sum>1000 without altering loop parameters
for (row = 0; row < numRows; row++) { for (col = 0; col < numCols; col++) { sum += mat[row][col]; if (sum > 1000.00) break bothloops; } } bothloops:
C# Example Using Break Labels
ArrayList* list = new ArrayList(); list->Add("Hello"); list->Add("World"); list->Add("!"); for (IEnumerator* i= list->GetEnumerator(); i->MoveNext();;) { Console::Write(myEnumerator->Current); } Console::WriteLine();
C++.NET Iterator Example
// For a set or list ArrayList list = new ArrayList(); list.Add("Hello"); list.Add("World"); list.Add("!");
for (Iterator i=list.iterator(); i.hasNext(); ) { System.out.print(i.next()); } System.out.println();
Java Iterator Example