5ProgramStructureIV - Program Structure IV Transfer of...

Info iconThis preview shows pages 1–5. 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 5. Program Structure IV - 1 http://www.cs.cityu.edu.hk/~helena Program Structure IV Program Structure IV 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 5. Program Structure IV - 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. va r m a rk; m a rk=pro m pt('Wha t is  yo ur m a rk? ',''); if (m a rk>40) do c um e nt.write ('Pa s s e d.'); e ls e { do c um e nt.write ('Fa ile d.'); do c um e nt.write ('<b r/>'); do c um e nt.write ('Yo u m us t ta ke   this  c o urs e  a g a in!'); } do c um e nt.write ('<b r/>End o f re po rt.'); • 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 5. Program Structure IV - 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 we apply other control structures (2) and/or (3) below . 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 (learn later) 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
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 5. Program Structure IV - 4 http://www.cs.cityu.edu.hk/~helena Transfer of control – The if statement The if statement if ( condition ) statement-to-be-executed ; 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 (. .) this is wrong!!   Outer brackets MUST be there.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/10/2012 for the course CS 1301 taught by Professor Dr.wong during the Winter '08 term at City University of Hong Kong.

Page1 / 24

5ProgramStructureIV - Program Structure IV Transfer of...

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

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