HES1300_4_1

HES1300_4_1 - Program Flow Control HES1300 C Programming...

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

View Full Document Right Arrow Icon
Swinburne University of Technology 1 HES1300 C Programming Lecture Notes – Part 4 Swinburne University of Technology Program Flow Control s Many algorithms consist of a list of steps executed in a well defined sequence. Many, however do not. s The C programming language supports constructs that allow for implementation of algorithms that have parts that are either conditionally executed or repetitively executed. s These constructs are known as “flow control” constructs. s In the C programming language, the if , switch , while, do and for constructs are used for flow control. Swinburne University of Technology Control Structures s Transfer of control s In 1969, Bohm and Jacopini work demonstrated that all programs could be written in terms of only three control structures: b Sequence b Selection b Repetition s C statements are normally executed one after the other unless there is a control transfer command ( sequence ). s “go to” statement and “go to-less programming” Swinburne University of Technology if s There will be times in a program when you need to test a value, in order to decide what to do. For example, you may provide the user with a simple menu, like the following: b 1 Selection A b 2 Selection B s What is your choice (A or B)? b The user would enter 1 or 2, depending on what he/she wanted to do. Swinburne University of Technology if s The section of the program would look like this: int Selection; printf(“Selection1\nSelection2\n\nWhat is your choice ?\n”); scanf(“%d”,&Selection); if(Selection == 1) { /* Do something */ } else /* This is like saying OTHERWISE */ { /* Do something else */ } Swinburne University of Technology if s The ‘==‘ operator tests for equality. It is not to be confused with ‘=‘ which denotes assignment to a variable. s The condition test of an if statement can be followed by a single statement eg. function call, assignment. It should NEVER be immediately followed by a semicolon. s Alternatively, it can be followed by a group of functions. A group of functions is partitioned with braces, and is referred to as a “compound statement”.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Swinburne University of Technology 2 Swinburne University of Technology Compound Statements s Compound statements are often used to define an isolated block of a program . A compound statement is defined by surrounding the section in a program in braces. The body of a function is
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/29/2010 for the course C++ c taught by Professor Lauren during the Three '10 term at Swinburne.

Page1 / 5

HES1300_4_1 - Program Flow Control HES1300 C Programming...

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

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