Join Gerry O'Brien for an in-depth discussion in this video Controlling program flow with switch, part of Up and Running with C#.
Finally, let's look at our switch statement as another way of determining a…code branch to execute based on a value being passed in or a value being compared.…So, a switch statement starts off with the keyword switch…and accepts a local variable to be used for the comparison.…Now in this case, we've simply declared another integer…variable called value and assigned it the value zero.…And in a switch statement, we enclose everything with curly braces.…We used something called a case statement.…Now it's important to note that you use the keyword case, followed by the value…you want to compare against this value, and you end it with a full colon.…
Place your line of code that you want to execute if this evaluates to true.…And then when it's complete, we evaluate a break.…If we remove this break statement, C# will complain because it won't know which…line of code it is it really wants to execute based on a certain case.…So this break statement must be in place to…provide the separation between each one of your case statements.…
- Installing C#
- Working with loops
- Controlling program flow
- Using variables
- Building functions
- Creating and instantiating classes
- Catching errors
- Managing resources with the garbage collector
- Building collections
Skill Level Beginner
1. Introducing C#
2. C# Language Elements
3. Object-Oriented Programming in C#
4. Structured Exception Handling
5. Resource Management in C#
6. Extensible Types in C#
7. Windows Apps in C#
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.