our earlier post, we talked about using boolean operators and boolean
expressions. These expressions will return a yes or a no. However, you
will have to do something based on the yes and no. This will your program to do some or all of the following.
Choose between two options.
Choose between more than just two options.
Keep circling between a statement depending on some terminating condition.
...and some combination of the above.
like making decisions in programming is best learnt with code. So, we
would ask you to check our code repository for this right away.
However, for those who want a little bit of theory, I have included short snippets of the different decision based things available in c sharp.
This is used when you have to check multiple conditions.
Let's say, you want to decide how you want to travel to a movie, and this depends on the number of people who are coming.
If you are going alone, you will take a bus.
If you are going with a friend, you will take a bike.
If you are going with two friends, you will book a cab.
This is used when you want to keep doing the same thing until some expression becomes true or false.
For example, you will keep staring at that girl in the class until she scolds you.
For example, you will keep working until your boss says you can go home.
You will keep watching TV until you fall asleep.
is used when you want to do something exactly one time, and then keep
doing it until some expression becomes true or false.
For example, you will stare at that girl, then keep staring at her until she slaps you.
For example, you will start working, then continue to work until your boss says you can go home.
Understanding the difference between While and Do While
There is a very small difference between while and do while. Let's take the girl staring/slapping example.
you will keep staring at that girl in the class until she scolds you. (while)
you will stare at that girl, then keep staring at her until she slaps you. (do while)
while, you will check the condition and then start doing something. In
this example, if you are using while, there is a good chance that the
girl may slap you, even before you have a chance to look at here. In
while, if the expression is already active, you won't even get to do the work, even once.
do while, no matter what happens, you will at least do something once.
That means, you will look at the girl exactly once. This is for sure.
Then, after you have looked at her once, she may or may not slap you. If
she does not slap you, will keep looking. Then, when she does slap you,
you will stop looking.
example would be the television example. If you are using while, you
will first check if you are sleepy. If you are sleepy from the beginning
itself, then you won't watch TV at all.
do while, it does not matter whether you are sleepy or not. You will
start watching right away, and then, you will check if you are sleepy.
If you are sleepy, then you will stop watching TV. If not, you will
continue to watch until you are sleepy.
The for loop. As a trainer, no other concept in programming has
bothered me (with respect to students) as much as the for loop. In fact,
students go bonkers just trying to understand how for loop works.
think the problem is, many students tend to learn for loop first, then
learn while and do while loop. The interesting thing is, a for loop is
nothing but a while loop with a counter. When the counter becomes zero
(like those bombs in movies), the loop ends.
That is why, that is all I will talk about for loops. Learn while loop properly, and you learn for loop as well.
A switch is like a ready made version of if else. Yes, that is all that I am going about that as well.
null-coalescing operator (try saying this five times, really fast!)
This is a special type of operator. To explain, this I really have to give a code line sample. Here we go.
string something_something = something_empty ?? "wow value";
This operator is useful when you wish to assign a default value to an object that might
be null. This null coalescing operator is kind of a tricky beast.
That's because, there are some specific scenarios where you would use
let me give you a scenario. Suppose, you are planning to watch this new
batman movie. However, you are not sure if you will be able to get
tickets for it, when you reach the multiplex. Then, there is also that
new captain america
movie. That would be your second choice. What if, both the movies are
sold out? Then, you will tell yourself that you will watch some hindi
movie that you know for sure is bad, and hence it is a certainty that
you will get tickets.
The null coalescing
operator is like that. It will check if your object has a value
assigned to it. If it already has a value, then this operator will not
do anything. However, if the object is null, it will assign some default
value. The idea is to make sure that you don’t do stuff with a null
this is important because, you cannot do anything with a null object.
Imagine drinking coffee from an empty cup. That's what a null object
In the above code, string something_something = something_empty ?? "wow value";, something_something
will be assigned whatever is stored in something_empty. However, in
case, something_empty is actually null, then something_something gets
assigned "wow value".
the null coalescing operator from earlier, the conditional operator
serves a special purpose of assigning a value based on the true/false of
a boolean expression.
string you_entered = some_expression ? "you entered true" : "you entered false";
this code, if some_expression is true, you_entered gets assigned "you
entered true". If some_expression is false, you_entered gets assigned
"you entered false".
note about conditional operator and null coalescing operator
When you look below the surface of these two operators, you will notice that they are not really doing anything special. The same thing that a conditional operator does can be done with a simple if/else. The task that null coalescing operator does can also be done with a if/else.
Then, why bother using them, would be the question?
I, personally, prefer simpler
solutions. Given a choice, I would just if/else over these two
operators. However, there are at least two reasons to use them.
reason is compiler efficiency. While you are practicing programming,
you don’t normally worry about things like optimization. For instance,
you should not worry about deciding between using a string object or a string builder object. At least, not while you are learning to code just now. However, when you graduate to building real world applications, every tiny code that can be saved, should be saved.
Even a simple if/else uses 4 lines of code. These operators do the same thing in just one line of code. That's a 75 % efficiency,
in terms of lines of code written alone. This helps the compiler to
build things faster, make the final executable smaller and also, help
make debug easier.
second reason, is that, it just looks cool. If you are using just
if/else everywhere, then you are but a simpleton. You use all these cool
and complicated operators, suddenly people you think you are a Jedi or a Sith, depending on your force orientation.
Of course, check out our code repository to understand these better