Personal comments: I'm bored enough to actually do this write up... so yeah lol.
C++ is not a particularly favourable language for me, but it does have it's benefits. In short the language is used for Object Oriented Programming. So it would make alot of sense to use it to design games, though other languages are equally capable of doing so. However this little write up is basically not about the arguments on different languages, instead it is an arguement on what suits where in C++ programming.
Since in class we recently covered if else and switch, my first argument is when each is suitable and why certain if else codes can simply be replaced with a switch. Most people seem to find ifelse to be highly difficult to utilise, which i see as more of an utter lack of understanding for fundalmentals.
if(...)
{
....
}
else
{
...
}
that is a simple if else statement it's like saying..
Ask user if he is boy or girl
if(user is boy)
{
tell him he is a boy
}
else
{
tell her she is a girl
}
simple enough? Yup, now let's look at switch, usually it's more efficient to write in that situation,
like above, but what if you want to write another add on, like if the person asked is a man or woman. You would see this:
Ask user if he is boy, girl, woman or man
if(user is boy)
{
tell him he is a boy
}
else
{
if(user is girl)
{
tell her she is a girl
}
else
{
if(user is woman)
}
tell her she is a woman
{
else
}
if(user is a man)
{
tell him he is a man
}
}
that was quite a code... imagine if you had to code it like that... that would be madness...well.. perhaps if you wanted to add another one which would be child aged 1 to age 9..... We can use something called a switch, which is,
switch(variable)
{
case 1: ...
case 2: ...
case 3: ...
case 4: ...
default: ...
}
now let's try the first one again with the switch way.
user inputs what he/she is
switch(userinput)
{
Case 'Man': Tell he is a man
Case 'Woman': Tell she is a woman
case 'Boy': Tell he is a boy
Case 'Girl': Tell she is a girl.
default: user did not input what he is
}
See? see how simple the code is now and how short it is? if you want to go one step furthur, you could just combine it with the ifelse, which i won't show it here. So you see certain situations the if else would do well, but in other situations, where possible, use the switch.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment