Branching with the if Decision Statement and the Conditional Operator - 06
- Posted: Nov 21, 2011 at 9:12 AM
- 75,940 Views
- 62 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
Branching allows us to add logic to our applications. In this lesson introduces the if Decision statement (in its various forms) as well as the conditional operator. We also discuss how to refactor our code to make it more compact and less likely to produce errors by eliminating duplicate code.
Download the source code for Branching with the if Decision...
Already have a Channel 9 account? Please sign in
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
More C# videos most welcome, this is infectious.
I really love the careful and detailed explanations that I've watched so far.... I really cannot thank you enough!!! you saved my academics!!!!!!

Another video followed. Thanks to the team behind these. Really enjoying them and finding Bob's explanations clear and easy to follow. Also glad that he's not reading out every line of code as this gets me into the habit of reading the code before I start to type. I know what I'll be doing over the festive period.
i was struggling with learning C#, i have easily understood the stuff covered in these videos, thanks a million.
@santi_m @subaddiction @Scimac @nokks : Thank you all for the compliments. Glad these can be of help to you! Best wishes in 2012!
Thank you very much for your detailed and interesting videos, I appreciate them very much!
Thank you for sharing your knowledge... I learn a lot on this.
It's easy to understand and follow
I've been in MS software development over 10 years and I still find myself watching these. Maybe it's because the appealing way you present the concepts, or maybe because you look like Steve Martin :))
@Ricardo, @susan: Thank you for the feedback!
@piim: Thanks for the compliment ... I think?
You are the third person to tell me that within the past year, so I guess I need to grow my beard back.
Hats off to you, these have been so helpful. Thank you for being one of the best teachers i have encountered
@EmW:
Thanks for the nice words. Made my day!
Thanks to these C# beginners lectures I can now sleep at night!
@Aamir Syed: Is that a compliment? Or do I put you to sleep?
Sorry I did not express myself well.That was a good compliment!You see I work full time and have started learning C# at night on a limited time budget.Tried other books/sources but none drive home the basic concepts as quickly as your lectures. Cheers! Aamir
@Aamir Syed: Hehe ... thank you for the compliment then.
And I'm glad these videos helped you nail down the basic concepts. Keep pushing forward ... this is fun stuff!
Thank you for your lectures, I really enjoy the simplicity and easy learning experience.
Bob you made some awsom clear and simple videos , but where could we practice the skills we learning .
could you refer us to some practice examples?
@MorS: I have a series on my own website that has homework questions and solutions explained via video. Check it out!
This series is really god.The best I've seen so far.Will you maybe make next series?Maybe intermediate?
Thank you Bob for creating these videos. Was in school for programming for 3 years and events in life drove me off the academic path for now, but my passion for programming is strong as ever. These videos (along with the enormous compendium of knowledge on the MSDN) truly helped me understand the core concepts of OOP more than any professor has been able to/willing to explain.
Thank you again, truly appreciate the help!
@Davor: Thanks for the nice words! None in the works just yet. In your mind, what topics would you want covered in an "intermediate" series?
@Cale Elliott: Awesome, Cale. Thanks for passing that story along ... encouraging to me.
Thanks for the great tutorials so far. This is very easy to follow and I couldn't learn this anywhere else.
Thanks a lot for these videos, they are amazingly well done. I'm finally starting to see I can make this while other tutorials I tried were so confusing, unstructered or too fast - was very frustrating.
Bob, i guess everyone said pretty much everything i could think of to display my appreciation for your videos. Makes everything so easy to understand! Not everyone can do that ~ Cheers!
@Matt: @Uli: @PEDR0MEJI4: Thank you all for the nice words. I'm really glad that they are helping you learn C# and .NET.
Bob you look like Hollywood :)
@yan: I've been doing this for 10+ years, however, that's the first time ever said that "I look Hollywood". I'm assuming / hoping that's a good thing?
Great videos Bob! Quick question I try to run the application after the string message code, but the "You won a new car" doesn't pop up after the number is entered. There's a green squiggly line under message that says the variable message is assigned but the variable is never used. Any solutions to this?
@Zach Handzlik -- Trying to help you out in case Bob is busy. Perhaps you didn't write: Console.WriteLine(message) at the bottom of your code? Perhaps you just have: Console.WriteLine()
@Zach Handzlik: Just following up ... Did Paige's suggestion fix the issue?
@Paige: Nice catch, and thank you!!!
Great series. Thanks, Bob!
Bob These series you've uploaded are very helpful as i am a beginner in C# and .NET.
Frankly speaking i was unable to understand these concepts in my class but after watching ur videos you made it real easy. and INTERESTING........
There's a problem in new C# express 2012. Compiler do not accept second "else if" after i remove {} from code.
else if (userValue == "3") is also underlined with red wave line - "error CS1519: Invalid token 'else' in class, struct, or interface member declaration".
13 minute, 29 line of code on video.
Thank you Bob : You are good! :)
@Bri: I would make sure that the lines above the else statement are syntactically correct. I assure you, nothing changed in the latest version of C#. Hope that helps!
thank you so much Bob! I love yr videos. you are a great teacher.
Thank you very much Bob for these videos. I work in a Support Team and have keen interest in software development and just out of interest I developed two small applications for my team to automate some of their tasks.
Your detailed explanation is helping me "Refactor" my poorly written code. I will complete the whole series and learn more
This is an excellent series. I know a bit of vb.net but I'm already enjoying this far more. You don't find tutorials of this quality often. Keep it up!
Commendable Videos ! Great job mate.
I've been scripting for 10+ years and finally took the dive in to learning actual programming. I've tried learning C++ on my own with a c++ 'bible' and I became blocked and frustrated a few chapters in. Now, a few years later my passion for learning and creating is greater than ever. I've spent a while trying to decide on a language to learn and ultimately picked C# because it can be used in the Unity game engine and I've been wanting to create a 2d sprite game since junior high school.
I wanted to let you know that your tutorials are very well edited, you have this very welcoming nature and presentation, and they are super approachable. You're a great teacher and the world could use more people with your passion to continue learning and sharing.
(Keep the beard off, Steve Martin rules and you may get special treatment when going out!) :p
Hi Bob,
I have one doubt - why do we need to use "Console.ReadLine()" even when we actually dont want to read anything ? for example - the HelloWorld program, the if statements - all end with the ReadLine command. I tried personally and observed that the output window shows up and vanishes immediately if I do not use the ReadLine command? Is that the reason we use the ReadLine() command?
Thank you! I highly appreciate these videos! Just saying this doesn't do enough justice to the feeling of gratitude I have for your work
!
@Mehul Shah - I believe it tells the console to wait for the user to give input for its use. Since the user has to enter information the console window never closes until enter is pressed.
I've been struggling with trying to make it continuous questioning after entering first number. OK I entered 1 and did not win the boat how can you make the program not to end and ask me the question again?
Thanks.
Sorry bothering. This worked:
while(true)
{
}
Dear Bob,
Your teaching series is awesome and have had no problems till now. The code you were writing in lesson 6 gives me a problem. The word "else" gives me the error "Invalid expression term" and the word "if" gives me the error "; expected". I am running the 2012 Visual Express edition and the program and course is awesome.
Thanks
Hi Bob,
I have the exact same problem as @Kevin and @Bri.
Running 2012 VS. "else" = invalid expression term, and the curly bracket just above "else" gives the following error: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement. I've checked over and over to make sure my code is exactly the same as yours, but nothing!
Just also want to say thank you so much for these video lessons, you're a fantastic teacher!
Found my mistake! I had a Semi-colon after the: if (userValue == "1").
Facepalm!
Like the others have stated you are a great teacher.
The series needs to be updated though as I too cannot get the term "else" to function.
Good luck!
Hi
I'm learning C# and currently watching some excellent tutorial videos :-
Channel 9 c# tutorial 6
At 14:28 Bob talks about a condensed, easier way to write an if statement :-
string message = (uservalue ="1") ? "boat" : "strand of lint";
Just wondering if this is known as Pattern Matching which just checks for two conditions. Does anyone know if there is a way of writing a condensed if statement that checks for three conditions or more. Bob explains that this can be done by using a Switch statement. I know that c# is evolving all the time if anyone know a way please. Then it will help me keep my c# code more condensed.
Cheers
Matt
These videos are so well done I wish more beginners to programming could find it. It took me awhile to find this location. Very impressive Bob, your diction is clear and your intonation brings home the concepts. Thanks a billion.
Excellent presentation of these concepts. Over 30 as a Mainframe programmer and now needing to learn something new at this late stage of my career. I get it and eagerly awaiting my next Project to apply what I've learned.
you are simply amazing ! thanks for help us so much!
Coming from programming in LabVIEW for several years. Finest teaching of the subject I've found, after browsing several books and online tutorials. Good Job!
Thanks you so much for your contribution to the free learning world. It is great to see some very high quality professional videos that are easy to follow. Also very pleasing that such a high profile company like Microsoft provide this for the community. Thanks Team
This was a great help I am sure I will contue to use them to clarify and understand my errors as well Thank you
Console.WriteLine("Would you prefer what is behind door number 1, 2 or 3?");
string userValue = Console.ReadLine();
if (userValue == '1')
{
Console.WriteLine("You won a new car!");
Console.ReadLine();
}
Error 1:
Operator '==' cannot be applied to operands of type 'string' and 'char'
I've checked it ten times, and i can't find any problems with it.
I've tried with "int" (insted of string) and "ReadKey", and i still get the error?
Any help please?
Hi Bob,
Thanks for your videos, they are so easy to follow along and work with. I have one question about this tutorial, you wrote the statement
message = "you won a new car";
in-between the if and else-if statements without writing console.WriteLine, how did the computer know to write the message string after each input?
I would appreciate a response from anyone regarding this, really enjoying these videos, see you in the next one!
HA! just looked back up the video and answered my own question. Thanks for the fantastic work :)
My first foray into the world of C# and what a truly EXCELLENT series to follow. Will be moving on to Lesson 7 tomorrow. The first 6 have me eager for more and it's only been my first evening. Great stuff Bob - so fluently and carefully explained too. :)
Thanks for the videos... did my C# course last week and i couldn't remember everything! this will help so much!
Bob, thank you for the nice video lessons. My question is if there would be any difference to write
if
....
if...
if...
insead of
if
...
else if
...
else if...
Remove this comment
Remove this thread
close