Warning: The magic method Slickr_Flickr_Plugin::__wakeup() must have public visibility in /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php on line 152

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/feed-rss2.php on line 8
‘MASTERY #17’ Articles at TC101 Fall 2015 https://kenscourses.com/tc101fall2015 Introduction to Programming Python and C++ Thu, 26 Nov 2015 01:45:58 +0000 en hourly 1 https://creativecommons.org/licenses/by/4.0/ “Triple kill”: If, Elif and Else https://kenscourses.com/tc101fall2015/2015/triple-kill-if-elif-and-else/ Thu, 26 Nov 2015 01:45:58 +0000 http://juansalvadorfernandez.wordpress.com/?p=163 Continue reading “Triple kill”: If, Elif and Else]]> #this is the only mastery with three topics since all of them relate to each other.#

 

This three pals work together to make our life easier and just like the while they work with conditions.

We use if, elif and else when we desire to perform different actions depending on the outcome of a certain conditions, or multiple of them for that instance.

First let’s start with If and else. If will perform an action once IF a condition is met, and we use the else as a complement for this if, this is because if the condition of the “if” is not met, then the program will have nowhere else to go and it will die. Using “else” we suggest a second option, an escape route, for the program to use when the condition for “if” is not met.

Now, the thing with while and if is that they give you only two options to choose from, vanilla or chocolate, that’s why we have the “elif” condition to help us out, with else we can add as many extra conditions as we desire, sparing us a lot of trouble.

As an example:

X=input (“write 1 or 2”)

If (x=1):

Print (“yes”)

Elif (x=2):

Print (“no”)

Else:

Print (“wrong answer”)

 

In here we are validating the answer given to us by the user, we give the program two specific conditions for pairing the value of x given to us by the user and an “escape route” in case that the value doesn’t fit in any of the two previous conditions.

First the program will get the value of x from the user, and it will pair it with each of the conditions, if the value provided by the user is equal to one then the program will print “yes”, and if I is not it will validate the second condition, in the case that the value is equal to two then the program will print “no”, last but not least, if the value given by the user is neither one or two, then the program will take the “escape route” and print “wrong answer”.

This conditionals can be paired with “While” to make a program that constantly evaluates the values given to it, allowing us to make programs that adapt to a changing value.

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery #8 #17 https://kenscourses.com/tc101fall2015/2015/mastery-8-17/ Wed, 25 Nov 2015 21:03:19 +0000 http://fernyalanis.wordpress.com/?p=116

]]>
https://creativecommons.org/licenses/by/4.0/
Use of “Switch” as a Conditional. https://kenscourses.com/tc101fall2015/2015/use-of-switch-as-a-conditional-3/ Wed, 25 Nov 2015 04:53:44 +0000 http://myfreakingcrazythoughts.wordpress.com/?p=268 Continue Reading →]]> The syntax of the switch statement is a bit peculiar. Its purpose is to check for a value among a number of possible constant expressions. It is something similar to concatenating if-else statements, but limited to constant expressions. Its most typical syntax is:

switch (expression)
{
  case constant1:
     group-of-statements-1;
     break;
  case constant2:
     group-of-statements-2;
     break;
  .
  .
  .
  default:
     default-group-of-statements
}

 

Basically, what “Switch” actually does is to evaluate expression and checks if it is equivalent to constant1; if it is, it executes group-of-statements-1 until it finds the break statement. When it finds this break statement, the program jumps to the end of the entire switch statement (the closing brace).

If expression was not equal to constant1, it is then checked against constant2. If it is equal to this, it executes group-of-statements-2 until a break is found, when it jumps to the end of the switch. Finally, if the value of expression did not match any of the previously specified constants (there may be any number of these), the program executes the statements included after the default: label, if it exists (since it is optional). Both of the following code fragments have the same behavior, demonstrating the if-else equivalent of a switch statement:

switch example if-else equivalent

switch (x) {

  case 1:

    cout << "x is 1";

    break;

  case 2:

    cout << "x is 2";

    break;

  default:

    cout << "value of x unknown";

  }

if (x == 1) {

cout &lt;&lt; "x is 1";

}

else if (x == 2) {

cout &lt;&lt; "x is 2";

}

else {

cout &lt;&lt; "value of x unknown";

}

 

Here is a simple example of a code with a “Switch” Statement in it.

switch (x) {
  case 1:
  case 2:
  case 3:
    cout << "x is 1, 2 or 3";
    break;
  default:
    cout << "x is not 1, 2 nor 3";
  }

 

Sources:

http://en.cppreference.com/w/cpp/language/switch

http://www.cplusplus.com/doc/tutorial/control/#switch

-The Admin.

]]>
https://creativecommons.org/licenses/by/4.0/