module 201 object oriented programming lecture 4 branching...
TRANSCRIPT
![Page 1: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/1.jpg)
Module 201 Object Oriented Programming Lecture 4 – Branching in Code
![Page 2: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/2.jpg)
Working with strings Wide variety of tasks you might want to accomplish when
working with strings Separate out first and last name from a string representing
someone’s full name Convert a string to all uppercase Concatenate two strings representing first and last name and create
a string containing last name followed by a comma followed by a first name
Display a number in currency format, for example £9,999.99 Replace some string with another string
![Page 3: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/3.jpg)
Conditional branching
![Page 4: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/4.jpg)
Any programming language must provide a means of branching in code Procedure may need to execute one statement if a condition is true
Optionally, execute a second statement if the condition is false
![Page 5: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/5.jpg)
Code can “make decisions” as it’s executing If statements Single-line If statements If/Else statements Nested If statements Testing for multiple conditions Comparing a condition to a single value
![Page 6: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/6.jpg)
Allows you to execute a block of code if a condition is true
Skips the code if the condition is false Use the If statement in different ways Depending on whether you want to execute a single statement or block
of statements Can also execute one block if a statement is true
And another if it’s false
Files on the wiki: (simpleIf.pdf) (testIfElse.pdf)
![Page 7: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/7.jpg)
Comparing one expression to multiple values? Could use if/else Better to use switch
switch statement is easiest when comparing one expression to multiple values
Less code – easier to understand/comprehend
Files on the wiki: (singleCondition.pdf) (testSwitch.pdf)
![Page 8: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/8.jpg)
Switch statement is simple to use, but note these issues:
The break statement causes C# to jump out of the switch line to the line immediately following the switch
If you do not include the break, execution falls through to the next case
You cannot fall through unless the case includes no code – otherwise code will NOT compile
You can use a string literal as the comparison value Don’t have to compare on numbers
If no other cases match, C# executes the code in the default case This is the end of the block
Files on the wiki: (moreSwitch.docx)
![Page 9: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/9.jpg)
Several different code constructs that allow you to run code repeatedly You can execute code while or until a condition is/becomes true Can execute code a fixed number of times Can execute a block of code once for each element of a collection
![Page 10: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/10.jpg)
There are several different options available for repeating a block of code while a condition remains true While loop Do loop
![Page 11: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/11.jpg)
Simplest loop Executes a block indefinitely, while a condition is TRUE
What if a condition never changes? Code must exit the loop on demand
More likely to exit when the condition changes state The examples on the wiki show both those techniques
![Page 12: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/12.jpg)
While loop checks condition at the top of the loop It’s possible you’ll never enter the loop
Do…While loop checks the condition at the end of the loop
Guarantees that you will run the code within the loop at least once
Let’s look at examples that look at both….. Remember that the Do…While loop ALWAYS executes at least
once
Files on the wiki: testWhile.docx DoWhile.docx
![Page 13: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/13.jpg)
Try all the examples
![Page 14: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/14.jpg)
Conditional branching If….Else Switch While Do…While
![Page 15: Module 201 Object Oriented Programming Lecture 4 Branching ...wiki.computing.hct.ac.uk/_media/computing/fdsc/201oop-lec04.pdf · Any programming language must provide a means of branching](https://reader030.vdocument.in/reader030/viewer/2022041117/5f2cf2c8ad0d9a6bd01be09c/html5/thumbnails/15.jpg)
Looping in code