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
‘#Masteries16’ Articles at TC101 Fall 2015 https://kenscourses.com/tc101fall2015 Introduction to Programming Python and C++ Fri, 18 Sep 2015 19:19:05 +0000 en hourly 1 https://creativecommons.org/licenses/by/4.0/ How to use ELSE https://kenscourses.com/tc101fall2015/2015/how-to-use-else/ Fri, 18 Sep 2015 19:19:05 +0000 http://saldabal.wordpress.com/?p=45 Continue Reading →]]> I recently posted on how to use IF and ELIF loops. Well, this time im going to show you how to use “ELSE” which is kind of the family of IF and ELIF.

When we use IF we evaluate a condition, if there the chance that it might be FALSE we can add an ELIF to tell the program what to do in case there are two possible options and the first is false. When we have more than two options we can use IF and ELIF, but when we have three options we have to look at our program and see it the third option is the last and unique option left. Let’s see an example.

x = 15

if ( x > 10 ):

print(“greater than 10”)

elif (x>0):

print(“greater than 0 and smaller than 10”)

else:

print(“the number is negative”)

This program will display the message: greater than 10. In case we change the value of x to 2 it will show: greater than 0 and smaller than 10 the number is negative. And the last choice is that if none of that happened it will show: the number is negative. We could have use elif, but instead of that with ELSE we save us some time, some effort make everything have concordance, we save some letters that means the program will be faster (perhaps we don’t see the difference in this case, but we could notice in a larger, complex code).

This is why we should use ELSE, is not better who writes a larger complex program, but who writes a smaller, simple, powerful one.

]]>
https://creativecommons.org/licenses/by/4.0/
Use of the conditionals “If” and “Elif” https://kenscourses.com/tc101fall2015/2015/use-of-the-conditionals-if-and-elif/ Fri, 18 Sep 2015 18:22:55 +0000 http://saldabal.wordpress.com/?p=37 Continue Reading →]]> There is something in math called Conditional expressions. They are expressión that can only be answered with YES or NOT. So the statements needed to declare one expression of this kind must be also special, they are called conditionals.

So we must use this statement to value if our expression is True or False. For that purpose we’ll use the special word “If”

“If” in python means the same as it does in the english language. So after typing “If” you must state the expression you want to evaluate, either to be TRUE, or FALSE.

An example of this will be.

x = 5

if ( x == 5):

print(‘True’)

It is very important that at the end of our statement we put a colon (:) because this means that the block of code we write next to it (actually under it and with a space tab) will be part of what will be executed in case that our conditional turns out to be TRUE.

In the previous example, the code i wrote translated to english will literally mean:

set the value of x to 5

if x is equal to 5, print ‘True’

But what if our expression is valuated at False. That would mean that our block of code we wrote under the expression wont be executed. In this case we have two options, we can just let it like that or we can arrange a second conditional to say what we want the computer to execute in case that it (the second conditional) turns out TRUE. For this purpose we can use the special word: ELIF, this is kind of a combination of Else If.

So let’s see an example.

Correct use of "if" and "elif"
Correct use of “if” and “elif”

In this piece of code we can see how at first instance, the value of x is equal to 5. Then we tried to evaluate x, so we´re saying that if x is equal to 6, print in the screen ‘If’ but because we set the value at 5 this turns out to be false, so it wont print ‘If’

Then we set an Elif, which is kind of “if the last ‘If’ statement doesn´t work, evaluate this to see if it’s true so you can run it.”

The Elif says that if the value of x is equal to 5, print ‘Elif’.

At the moment you run the program, it prints ‘Elif’ because the conditions of Elif is True.

Thats how If and Elif works, you just have make sure you know where and when you want to evaluate

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