Samenvatting Hoofdstuk 5c: Control Flow Constructs
12 views 0 purchase
Course
Structuur van Computerprogramma\'s II
Institution
Vrije Universiteit Brussel (VUB)
Book
The C Programming Language
Dit is de samenvatting van het negende hoofdstuk van het vak Structuur van Computerprogramma's II. In deze samenvatting werd zowel alle relevante informatie uit de slides als informatie uit eigen notities opgenomen.
Hoofdstuk 5c: Structured Control Flow
Constructs
1 C supports Unstructured Control Flow
• Typically, assembly languages support three kind of instructions:
o Pass control to next instruction ( eg. add, sub, mov, cmp)
o Pass control to another instruction (at memory location indicated by operand) (jmp
<location>)
o Conditional jump depending on status of set of condition codes
1.1 goto Statement
• Allows to jump unconditionally to an arbitrary part of your
code within the same function identified using a label
2 Spaghetti Code
• Excess use of goto creates spaghetti code.
• Using goto makes code harder to read
and debug.
2.1 Structured Control Flow
• Any code that uses goto can be written
without using one.
• Replace evil goto by structured constructs
such as:
o for, while, break, return,
continue, do.. while, if.. then..else
2.2 goto Use Case
• Error handling and resource cleanup
See code →
1
, 3 Error handling in C
• Programmers use “certain values” that differ from the return values of normal executions to
check and propagate errors.”
3.1 What if we use exceptions to handle errors?
• In languages like Java or Scala exceptions are actually designed to be used as control flow
structures.
3.1.1 Non-local control flow
4 How to transpose
high-level control flow
constructs like
exceptions to C?
4.1 Continuation
• First, it requires to control “the next thing to do after the expression or the statement has
been executed”
o Continuation
o “A continuation represents the computational process at a given point in the process
execution”
4.1.1 Continuations by Example
• The continuation of an expression or a statement is the things that remain to be done after
“doing” that expression or statement
4.1.2 How can we represent continuations?
• Conclusion: the continuation of an
expression is a function that consumes
the value of that expression and
proceeds the computation with this
value
2
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller lennyS. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $3.76. You're not tied to anything after your purchase.