Upload
others
View
8
Download
0
Embed Size (px)
Citation preview
Module 201 Object Oriented Programming Lecture 4 – Branching in Code
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
Conditional branching
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
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
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)
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)
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)
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
There are several different options available for repeating a block of code while a condition remains true While loop Do loop
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
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
Try all the examples
Conditional branching If….Else Switch While Do…While
Looping in code