DOC PREVIEW
TAMU CSCE 110 - 13-otherControlStructures
Type Miscellaneous
Pages 26

This preview shows page 1-2-3-24-25-26 out of 26 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 26 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

J. Michael MooreOther Control StructuresCSCE 110Influenced by material developed by James Tam & Jennifer WelchJ. Michael MooreRevisiting Selection (Decision-Making) In PascalSelection / branching constructs (mechanisms) in Pascal •If-then•If-then-else•If, else-if•______________J. Michael Moore___________ StatementsThe selector expression can be a• __________• __________• __________That evaluates to a:• __________• __________• __________The body is a _______ statement (can be a __________statement)An alternative to the if, else-if :at most only one of many conditions can be true, i.e. mutual exclusionFormat:case (selector_expression) ofi1:body;i2:body;:in:body;elsebdJ. Michael MooreCase Statement: Integer ExampleExamplecase (gpa) of4:writeln(‘You got an A’);3:writeln(‘You got a ‘B’);2:writeln(‘You got a C’);1:writeln(‘You got a D’);0:writeln(‘You got an F’);elsewriteln('GPA must be one of 4, 3, 2, 1 or 0');end; (* case *)J. Michael MooreCase Statement: Character ExampleExamplecase (letter) of'A':writeln('GPA = 4');'B':writeln('GPA = 3');'C':writeln('GPA = 2');'D':writeln('GPA = 1');'F':writeln('GPA = 0');elsewriteln('Letter grade must be one of an ''A'', ', '''B'', ''C'', ''D'' or ''F''');end; (* case *)J. Michael MooreCase Statement: Menu Examplewhile (menuChoice<>'q') and (menuChoice<>'Q') dobegindisplayMenu;getMenuChoice(menuChoice);case (menuChoice) of'f', 'F':begingetNumber(num);answer := factorial(num);outputAnswer(num, answer);end;'s', 'S':begingetNumber(num);answer := sqrt(num);outputAnswer(num, answer);end;end; (* case *) end What should menuChoicebe initialized to?______________ ______________J. Michael MooreRecap: What Decision Making Constructs Are Available In Pascal/When To Use ThemSimilar to the ‘if, else-if’ but results in smaller (cleaner) programs but only works for specific situations (Boolean expressions that involve characters or integer values only).Case-ofMultiple Boolean expressions need to be evaluated but zero or at most only one of them can be true (mutually exclusive). Zero bodies or exactly one body will execute.If, else-ifMultiple Boolean expressions need to be evaluated with the answer for each expression being independent of the answers for the others (non-exclusive). Separate code (bodies) can be executed for each expression.Multiple if’sEvaluate a Boolean expression and execute some code (first body) if it’s true, execute alternate code (second body) if it’s falseIf-then-elseEvaluate a Boolean expression and execute some code (body) if it’s trueIf-thenWhen To UseConstructJ. Michael MooreRecap: When To Use Compound And Nested Decision Making ConstructsThe outer Boolean expression must be true before the inner expression will even be evaluated.Nested decision makingMore than one Boolean expression must be evaluated before some code (body) can execute.Compound decision makingWhen To UseConstructJ. Michael MooreRevisiting Loops - Basic Structure Of Loops1) ___________ the controla) Control – typically a variable that determines whether or not the loop executes or not. 2) _________ the control against a ___________3) __________ the ___________ of the loop4) ___________________of the controlJ. Michael MooreTypes Of Loops1.Pre-test loops• Check the stopping condition ________executing the body of the loop.• The loop executes _____or _____ times.• Pascal implementation: _________, _____2.Post-test loops• Check the stopping condition ________executing the body of the loop.• The loop executes _____or _____ times.• Pascal implementation: _________J. Michael MoorePre-Test Loops1. Initialize loop control2. Check if the stopping condition has been meta. If it’s been met then the loop endsb. If it hasn’t been met then proceed to the next step3. Execute the body of the loop (the part to be repeated)4. Update the loop control5. Go to step 2Initialize loop controlExecute bodyCondition met?Update controlAfter the loop (done looping)YesNoJ. Michael MoorePre-Test Loop: ______Typically used when it is ______________ how many times that the loop will execute (__________ loops). Loop executes until the loop control would go _____ the ___________ condition.Format (counting ______):for initialize control to final value dobody Format (counting ______):for initialize control downto final value dobody Note: For loops are only supposed to count ___(‘___’) or _____ (‘______’) by ____. If the program must go up or down by other ______then use a _______ ______ instead. NEVER _______the _____ ______ of a Pascal ____loop in the _____ of the loop!J. Michael MooreFirst For Loop ExampleExample one:beginvar i : integer;var total : integer;total := 0;for i := 1 to 5 dobegintotal := total + i;writeln('i=', i, ‘ total=', total);end; (* for *)end.J. Michael MooreExample one:beginvar i : integer;var total : integer;total := 0;for i := 1 to 5 dobegintotal := total + i;writeln('i=', i, ‘ total=', total);end; (* for *)end.First For Loop Example1) ________________i := 13) ________________to2) ________________4) ________________to 5J. Michael MooreSecond For Loop ExampleExample twobeginvar i : integer;var total : integer;total := 0;for i := 5 downto 1 dobegintotal := total + i;writeln('i=', i, ' total=',total);end; (* for *)end.J. Michael MoorePost-Test Loops1. Initialize loop control (sometimes not needed because initialization occurs when the control is updated)2. Execute the body of the loop (the part to be repeated)3. Update the loop control4. Check if the stopping condition has been meta. If it’s been met then the loop endsb. If it hasn’t been met then return to step 2.Initialize loop controlExecute bodyUpdate controlNoCondition met?After the loop (done looping)YesJ. Michael MoorePost Test Loops: ________________Can be used instead of a __________ loop if you need the loop to execute the loop ____________. (Note: A _________ can also be modified so that it is ___________ to execute at least _____by __________ the loop control to a value that will result in a ______ evaluation of the Boolean expression). Loop executes while some Boolean expression is _______, it stops when it’s ________.Format:repeatbodyuntil (Boolean expression);Unlike the __________, the repeat-until loop can have ___________________ in its body without using a _________________.J. Michael MooreRepeat-Until: An Example (2)program repeatUntil (output);beginvar i : integer;i:= 1;repeatbeginwriteln('i = ',


View Full Document

TAMU CSCE 110 - 13-otherControlStructures

Type: Miscellaneous
Pages: 26
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download 13-otherControlStructures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view 13-otherControlStructures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view 13-otherControlStructures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?