SlideShare a Scribd company logo
1 of 11
Download to read offline
PROGRAMMING SESSIONS
03
Computation: Control flow and Functions
Rafael Camacho
PROGRAM
Basic concepts
• Computation
• Control flow
• Managing
complexity -
abstraction
• Functions
Flow control in
MATLAB
• Selection
• If
• Switch
• Iteration
• While
• For
Functions
• Why are
functions
important
• How to
create a
MATLAB
function
2
BASIC CONCEPTS
• Computation: refers to the act of
producing some outputs based on some
inputs.
• Control flow: refers to the order in which
instructions are executed in a program.
Among the different control flow
statements we have: if, switch, while, for.
11/17/17 3
BASIC
CONCEPTS
• The most important task you will have to tackle
during programming is managing complexity. If
you want to solve a very complex problem your
main tool is to break such a problem into many
little ones that are easier to handle (divide and
conquer).
• Abstraction: hide details that we do not need in a
convenient container, e.g. a function.
• A function is a named sequence of statement.
4
FLOW CONTROL IN
MATLAB
One of the most basic decisions a program has
to do is to select among alternatives.
An if-statement will execute a block of code if a
condition is true.
MATLAB includes the elseif syntax. Beware!
Does my code on the left behave as you would
expect.
5
FLOW CONTROL
IN MATLAB
• One of the most basic
decisions a program has to do
is to select among alternatives.
• A switch-statement is designed
for a very specific and common
type of comparison, checking if
a value is equal to a constant.
6
FLOW CONTROL IN
MATLAB
Another very common thing that can
happen when doing a computation is to
repeat something several times.This
repetition of the same block of
statements is called iteration.
• A while-loop will repeat the
evaluation of some statement(s) as
long as a condition is true.
• Beware! Ensure that you do not
generate an infinite loop by mistake.
7
…
FLOW CONTROL IN
MATLAB
Another very common thing that can happen
when doing a computation is to repeat
something several times.This repetition of the
same block of statements is called iteration.
• Iterating over a list of elements is so common
that most programing languages have a special
syntax for it – the for-loop.
• A for-loop is very similar to a while-loop
except that this time we define how many
times we will iterate before hand.
8
FUNCTIONS
WHY?
Functions are very useful when we want to:
• Divide the logic of a program into clear separate sub-
calculations.
• Make the program easier to read.
• Make it possible to re-use a functionality.
• Make testing the program easier.
9
HOW TO CREATE A FUNCTION
IN MATLAB
10
1
2
3
ASSIGNMENT
The task for this session is to go back to project 01 step 01 and:
• Come up with a while-loop and for-loop implementation that
calculates the Mandelbrot set. Basically we will solve together
the last assignment.
• Save both implementations into a function file. Use a switch
statement to let the user choose which implementation he
wants to use, the for- or while-loop.
• As an extra bonus look into the ’tic’ and ’toc’ functions of
MATLAB.What do they do? Use them to check which
implementation is the fastest.
11

More Related Content

What's hot

Stacks and Mathematical Expressions
Stacks and Mathematical ExpressionsStacks and Mathematical Expressions
Stacks and Mathematical ExpressionsVictoria V
 
Stacks and Mathematical Expressions
Stacks and Mathematical ExpressionsStacks and Mathematical Expressions
Stacks and Mathematical ExpressionsVictoria V
 
Salesforce Apex Ten Commandments
Salesforce Apex Ten CommandmentsSalesforce Apex Ten Commandments
Salesforce Apex Ten CommandmentsNetStronghold
 
React suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred HitchcockReact suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred HitchcockMaurice De Beijer [MVP]
 
Microsoft SQL Server Log Management
Microsoft SQL Server Log ManagementMicrosoft SQL Server Log Management
Microsoft SQL Server Log ManagementStephan Lawson
 
Communicating with Scientific (Current)
Communicating with Scientific (Current)Communicating with Scientific (Current)
Communicating with Scientific (Current)Matthew Herndon
 
Reactive programming with rx java
Reactive programming with rx javaReactive programming with rx java
Reactive programming with rx javaCongTrung Vnit
 
Baking an FME Dashboard: Two Cups of Python & a Dash of Frustration
Baking an FME Dashboard: Two Cups of Python & a Dash of FrustrationBaking an FME Dashboard: Two Cups of Python & a Dash of Frustration
Baking an FME Dashboard: Two Cups of Python & a Dash of FrustrationSafe Software
 
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...European SharePoint Conference
 
Fault tolerance and computing
Fault tolerance  and computingFault tolerance  and computing
Fault tolerance and computingPalani murugan
 
Async discussion 9_29_15
Async discussion 9_29_15Async discussion 9_29_15
Async discussion 9_29_15Cheryl Yaeger
 

What's hot (13)

Stacks and Mathematical Expressions
Stacks and Mathematical ExpressionsStacks and Mathematical Expressions
Stacks and Mathematical Expressions
 
Stacks and Mathematical Expressions
Stacks and Mathematical ExpressionsStacks and Mathematical Expressions
Stacks and Mathematical Expressions
 
Salesforce Apex Ten Commandments
Salesforce Apex Ten CommandmentsSalesforce Apex Ten Commandments
Salesforce Apex Ten Commandments
 
React suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred HitchcockReact suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred Hitchcock
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
Microsoft SQL Server Log Management
Microsoft SQL Server Log ManagementMicrosoft SQL Server Log Management
Microsoft SQL Server Log Management
 
Communicating with Scientific (Current)
Communicating with Scientific (Current)Communicating with Scientific (Current)
Communicating with Scientific (Current)
 
Reactive programming with rx java
Reactive programming with rx javaReactive programming with rx java
Reactive programming with rx java
 
Baking an FME Dashboard: Two Cups of Python & a Dash of Frustration
Baking an FME Dashboard: Two Cups of Python & a Dash of FrustrationBaking an FME Dashboard: Two Cups of Python & a Dash of Frustration
Baking an FME Dashboard: Two Cups of Python & a Dash of Frustration
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
 
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...
State Machine Workflow: Esoteric Techniques & Patterns Everyone Should Buy pr...
 
Fault tolerance and computing
Fault tolerance  and computingFault tolerance  and computing
Fault tolerance and computing
 
Async discussion 9_29_15
Async discussion 9_29_15Async discussion 9_29_15
Async discussion 9_29_15
 

Similar to Programming Sessions KU Leuven - Session 03

Matlab for diploma students(1)
Matlab for diploma students(1)Matlab for diploma students(1)
Matlab for diploma students(1)Retheesh Raj
 
CPP11 - Function Design
CPP11 - Function DesignCPP11 - Function Design
CPP11 - Function DesignMichael Heron
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxssusere336f4
 
Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02Rafael Camacho Dejay
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structureSelf-Employed
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tipsBill Buchan
 
Functions and procedures
Functions and proceduresFunctions and procedures
Functions and proceduresunderwan
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptxmiki304759
 
Learn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in DelhiLearn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in Delhiibinstitute0
 
Technical trainning.pptx
Technical trainning.pptxTechnical trainning.pptx
Technical trainning.pptxSanuSan3
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in Indiaibinstitute0
 
C++ question and answers
C++ question and answersC++ question and answers
C++ question and answersAdenKheire
 
Introduction to OpenMP (Performance)
Introduction to OpenMP (Performance)Introduction to OpenMP (Performance)
Introduction to OpenMP (Performance)Akhila Prabhakaran
 

Similar to Programming Sessions KU Leuven - Session 03 (20)

Matlab for diploma students(1)
Matlab for diploma students(1)Matlab for diploma students(1)
Matlab for diploma students(1)
 
CPP11 - Function Design
CPP11 - Function DesignCPP11 - Function Design
CPP11 - Function Design
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
 
Matopt
MatoptMatopt
Matopt
 
Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02Programming Sessions KU Leuven - Session 02
Programming Sessions KU Leuven - Session 02
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Algorithm and C code related to data structure
Algorithm and C code related to data structureAlgorithm and C code related to data structure
Algorithm and C code related to data structure
 
Dev buchan 30 proven tips
Dev buchan 30 proven tipsDev buchan 30 proven tips
Dev buchan 30 proven tips
 
Python recursion
Python recursionPython recursion
Python recursion
 
Functions and procedures
Functions and proceduresFunctions and procedures
Functions and procedures
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Learn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in DelhiLearn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in Delhi
 
Technical trainning.pptx
Technical trainning.pptxTechnical trainning.pptx
Technical trainning.pptx
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in India
 
C++ question and answers
C++ question and answersC++ question and answers
C++ question and answers
 
ES-CH5.ppt
ES-CH5.pptES-CH5.ppt
ES-CH5.ppt
 
Mit6 094 iap10_lec01
Mit6 094 iap10_lec01Mit6 094 iap10_lec01
Mit6 094 iap10_lec01
 
C- language Lecture 4
C- language Lecture 4C- language Lecture 4
C- language Lecture 4
 
Introduction to OpenMP (Performance)
Introduction to OpenMP (Performance)Introduction to OpenMP (Performance)
Introduction to OpenMP (Performance)
 

Recently uploaded

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 

Recently uploaded (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

Programming Sessions KU Leuven - Session 03

  • 1. PROGRAMMING SESSIONS 03 Computation: Control flow and Functions Rafael Camacho
  • 2. PROGRAM Basic concepts • Computation • Control flow • Managing complexity - abstraction • Functions Flow control in MATLAB • Selection • If • Switch • Iteration • While • For Functions • Why are functions important • How to create a MATLAB function 2
  • 3. BASIC CONCEPTS • Computation: refers to the act of producing some outputs based on some inputs. • Control flow: refers to the order in which instructions are executed in a program. Among the different control flow statements we have: if, switch, while, for. 11/17/17 3
  • 4. BASIC CONCEPTS • The most important task you will have to tackle during programming is managing complexity. If you want to solve a very complex problem your main tool is to break such a problem into many little ones that are easier to handle (divide and conquer). • Abstraction: hide details that we do not need in a convenient container, e.g. a function. • A function is a named sequence of statement. 4
  • 5. FLOW CONTROL IN MATLAB One of the most basic decisions a program has to do is to select among alternatives. An if-statement will execute a block of code if a condition is true. MATLAB includes the elseif syntax. Beware! Does my code on the left behave as you would expect. 5
  • 6. FLOW CONTROL IN MATLAB • One of the most basic decisions a program has to do is to select among alternatives. • A switch-statement is designed for a very specific and common type of comparison, checking if a value is equal to a constant. 6
  • 7. FLOW CONTROL IN MATLAB Another very common thing that can happen when doing a computation is to repeat something several times.This repetition of the same block of statements is called iteration. • A while-loop will repeat the evaluation of some statement(s) as long as a condition is true. • Beware! Ensure that you do not generate an infinite loop by mistake. 7 …
  • 8. FLOW CONTROL IN MATLAB Another very common thing that can happen when doing a computation is to repeat something several times.This repetition of the same block of statements is called iteration. • Iterating over a list of elements is so common that most programing languages have a special syntax for it – the for-loop. • A for-loop is very similar to a while-loop except that this time we define how many times we will iterate before hand. 8
  • 9. FUNCTIONS WHY? Functions are very useful when we want to: • Divide the logic of a program into clear separate sub- calculations. • Make the program easier to read. • Make it possible to re-use a functionality. • Make testing the program easier. 9
  • 10. HOW TO CREATE A FUNCTION IN MATLAB 10 1 2 3
  • 11. ASSIGNMENT The task for this session is to go back to project 01 step 01 and: • Come up with a while-loop and for-loop implementation that calculates the Mandelbrot set. Basically we will solve together the last assignment. • Save both implementations into a function file. Use a switch statement to let the user choose which implementation he wants to use, the for- or while-loop. • As an extra bonus look into the ’tic’ and ’toc’ functions of MATLAB.What do they do? Use them to check which implementation is the fastest. 11