4ProgramStructureIII - Program Structure III Transfer of...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 4. Program Structure III - 1 http://www.cs.cityu.edu.hk/~helena Program Structure III Program Structure III Transfer of control Sequence, selection, repetition structures Selection structure if if-else if… else if … Code formatting Repetition structure while -loop for -loop Nested control structures [Please switch off your phone]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 4. Program Structure III - 2 http://www.cs.cityu.edu.hk/~helena Transfer of control Overview : Sometimes, next statement to execute may not be the next one in sequence. For example: line 1. line 2. line 3. line 4. line 5. line 6. line 7. line 8. line 9. line 10. var mark; mark=prompt('What is your mark?',''); if (mark>40) document.write('Passed.'); else { document.write('Failed.'); document.write('<br/>'); document.write('You must take this course again!'); } document.write('<br/>End of report.'); After the checking at line 3 has finished, either line 4 or line 5 will be executed. Suppose line 4 is executed after line 3, we say "the control is transferred to line 4". Suppose line 5 is executed after line 3, we say "the control is transferred to line 5". • In case line 4 is executed, then after line 4 is executed, line 10 will be executed. We say "the control is transferred to line 10".
Background image of page 2
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 4. Program Structure III - 3 http://www.cs.cityu.edu.hk/~helena Transfer of control Three different kinds of Control Structures (1) Sequence structure Statements execute in the order they are written. This is the default flow unless other control structures are applied. Firstly execute line 1 Then execute line 2 Finally execute line 3 (2) Selection structure The choice of next statement is based on a test: whether a condition is true or not. Example (3) Repetition structure A statement (or a set of statements) is to be repeated again and again until the repetition condition becomes false. a = 3; b = 4; alert(a+b); 1 2 3 var count=1; while (count<=10) { document.write('hello</br>'); count++; } 1 2 3 4 if (mark>40) document.write('pass'); else document.write('fail'); 1 2 3 4
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4. Program Structure III - 4 Transfer of control – The if statement One whole if statement If the condition is true, then statement-to-be-executed will be executed. If the condition is not true (false), then statement-to-be-executed will not be executed. Must have a pair of outer brackets: if (. .) c this is wrong!! Outer brackets should NOT be omitted. if (mark > 80) if mark > 80 if ( mark < 0 | | mark > 100 ) if (mark < 0) (mark > 100) if ( (mark > 100) ) alert('invalid mark'); . this is OK!! . this is OK too!! brackets around “mark<0” and “mark>100” are optional
Background image of page 4
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 4. Program Structure III - 5 http://www.cs.cityu.edu.hk/~helena Transfer of control – The if statement if ( condition ) { ..
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

4ProgramStructureIII - Program Structure III Transfer of...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online