HES1300_4_1 - Program Flow Control HES1300 C Programming...

Info icon This 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 square6 Many algorithms consist of a list of steps executed in a well defined sequence. Many, however do not. square6 The C programming language supports constructs that allow for implementation of algorithms that have parts that are either conditionally executed or repetitively executed. square6 These constructs are known as “flow control” constructs. square6 In the C programming language, the if , switch , while, do and for constructs are used for flow control. Swinburne University of Technology Control Structures square6 Transfer of control square6 In 1969, Bohm and Jacopini work demonstrated that all programs could be written in terms of only three control structures: box2 Sequence box2 Selection box2 Repetition square6 C statements are normally executed one after the other unless there is a control transfer command ( sequence ). square6 “go to” statement and “go to-less programming” Swinburne University of Technology if square6 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: box2 1 Selection A box2 2 Selection B square6 What is your choice (A or B)? box2 The user would enter 1 or 2, depending on what he/she wanted to do. Swinburne University of Technology if square6 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 square6 The ‘==‘ operator tests for equality. It is not to be confused with ‘=‘ which denotes assignment to a variable. square6 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. square6 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”.
Image of page 1

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

View Full Document Right Arrow Icon
Swinburne University of Technology 2 Swinburne University of Technology Compound Statements square6 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
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern