the IF block

HegemonKhan
HK Note:

still a work in progress, need to finish it up first, and then go back and format~organize~section it nicely, for it being easier to read by people

(I'm trying to: ~ "if you're going to do~teach this, then do~teach it right", as Pixie made me aware of, so that, is what I'm now trying to do, to the best of my ability and current, hopefully at least somewhat correct, knowledge~understanding of things)

------------------------------------

first, some background Programming~Computer~Physics~Electronics information:

(as best as I currently understand it, lol)

Computers use electricty, and they have circuitry 'switches' which are lit 'on' or 'off' (via high/low voltages and~or +/- charged currents ~ I'm still a bit confused in regards to the electrical-electronic physics of it). Somehow (this is still a total mystery to me~magical), this 'physical layer' is converted (and also vice-versa) into the binary (zeros and ones) 'logical layer', and thus is called 'machine language' (this is the lowest of the low level, aka computer-friendly, of programming languages). All code, all programming, must be converted into machine language, into binary (zeroes and ones) for the computer ~ CPU ~ processor. Luckily, this is done for us, and we only have to learn the high level (human-friendly) programming languages like C++, Java, Python, HTML, JS, Ruby, Basic, MS-DoS and various modern 'Shell' languages, AppleScript, C#, etc etc etc, and~or similar but unique game engine languages like Quest's code language.

just for (pretend) example only (I hope to eventually learn into the low level languages, learning assembly language and maybe even a bit of machine language):

quest's high level language <-----> machine language (lowest low level language)
msg ("hi, my name is HK") <------> 010111000110000011000010001101110001001

machine language wiki link: https://en.wikipedia.org/wiki/Machine_c ... 0Q9QEIGTAA

So, why the information above, for the topic of the 'if' block?

because, this is actually how conditionals and booleans work

a boolean's 'true' and 'false' values are human-friendly, but underneath the user level, 'true' is converted into '1' (technically it's any number except 0, but 1 is the convention used and to keep this simple too for people) and 'false' is converted into '0', this is binary.

so, for example, in concept (not sure if this actually works in quest):

'if (true)' is the same as 'if (1)'
'if (false)' is the same as 'if (0)'

and let's expand the concept a bit further:

(I'm still a bit confused on 'if (FALSE)' concept, as 'unary' or non-comparison conditionals are confusing for me, so if any of this confuses you, ignore it)

(comparison conditionals make much more sense to me: if (true = true), if (false = false), if (true = false), if (false = true), lol)

if (player.strength = 100)
// let's say: player.strength = 100
// if (100 = 100) -> TRUE: the scripts will be run
// if (TRUE == 100 = 100) -> TRUE: the scripts will be run
// if (TRUE)
// if (1)

if (player.strength = 100)
// let's say: player.strength = 0
// if (0 = 100) -> FALSE: the scripts will NOT be run
// if (TRUE =/= 0 = 100) -> FALSE: the scripts will NOT be run
// if (FALSE)
// if (0)

if (not player.strength = 100)
// let's say: player.strength = 0
// if (0 =/= 100) -> TRUE: the scripts will be run
// if (TRUE == 0 =/= 100) -> TRUE: the scripts will be run
// if (TRUE)
// if (1)

if (not player.strength = 100)
// let's say: player.strength = 100
// if (100 =/= 100) -> FALSE: the scripts will NOT be run
// if (TRUE =/= 100 == 100) -> FALSE: the scripts will NOT be run
// if (FALSE)
// if (0)

and now, we can finally address the 'if' block:

(I'll get to this, but not now, I'm tired)

The Pixie
Rather than:
msg ("HK")
if (flag = true) {
msg ("hi")
}
else if (flag = false) {
msg ("bye")
}
msg ("HegemonKhan")

Do this:
msg ("HK")
if (flag) {
msg ("hi")
}
else {
msg ("bye")
}
msg ("HegemonKhan")

OurJud
HegemonKhan wrote:
now, let's add a bit of complexity to it, multiple 'if' blocks:

msg ("HK")
if (flag = true) {
msg ("hi")
}
msg ("bye")

if (flag = false) {
msg ("bye")
}
msg ("hi")


let's say I set: flag = false, what happens?

Answer, outputs:

HK
bye
bye
hi


I don't get this. If flag is false, then why isn't the output just:

HK
bye
hi

This script, to me, is saying, "print 'HK' " (regardless of flags because it comes before any 'ifs') and then if flag is false say the following: 'bye' and 'hi'.

So why is the output:

HK
bye
bye
hi


The Pixie
The if and else only apply to the block inside the curly braces. If flag is false, only one line is skipped.
msg ("HK")
if (flag = true) {
msg ("hi") // only this line skipped
}
msg ("bye") // first "bye"

if (flag = false) {
msg ("bye") // second "bye"
}
msg ("hi")

HegemonKhan
The Pixie wrote:Rather than:
msg ("HK")
if (flag = true) {
msg ("hi")
}
else if (flag = false) {
msg ("bye")
}
msg ("HegemonKhan")

Do this:
msg ("HK")
if (flag) {
msg ("hi")
}
else {
msg ("bye")
}
msg ("HegemonKhan")


I'm explaining the 'if~else if' here, NOT the 'if~else' situation. Yes, my example is bad design (I know: extra un-needed operations by using 'else if' instead of 'else' as it checks the condition: flag=false, when it shouldn't~doesn't have to), but it's very simple, and thus makes it easy for people to understand how the 'if~if else' works, which is the point here. This thread is NOT about explaining (deeper programming understanding of) code efficiency, though I will add these comments about efficiency into this thread, as well. I've just been working on the combinations of the IF block, I'll get to commenting about the efficiency and etc stuff after I got all of the combinations done for people to look at and understand. I will also use this thread to dive into trying to explain how the conditionals are actually working (the user level of booleans:true~false, and the underneath of booleans, of ints: zero vs non-zero), but this is a bit more deep~advanced, so I'm not doing it until the end. I want them to understand the concept of the STRING COMPARISON, using the extremely simple 'flag(true/false) = true/false' expression, even if it's poor design.

if (flag:true=true)
if (flag:false=false)

the above makes more sense (at least it did to me when I began learning to program using quest 2-3 years ago) then, the more efficient-proper-true:

if (flag:true)
if (flag:false)

which is what is taking place underneath (and further underneath is the zero vs non-zero of ints, of course)

------------

I want to keep my explanations uniform to help people, and the simpliest and easiest to understand uniform expression for people I could think of is 'flag=true/false'

HegemonKhan
@ OurJud:

Pixie already explained, but let me jsut do so as well:

notice that there's a 'msg("bye")' between the two 'if' blocks, just as there's a 'msg ("HK")' at the top of the code and a 'msg ("hi")' at the bottom of the code

these three messages are always run, the 'ifs' may or may not be, hence why the 'if' expressions (and some other expressions) true name is: CONDITIONAL expressions.

--- I should go back and re-label all of the msg scripts to be unique, so you know which msgs are displaying and which aren't... I didn't realize this until after I posted... I'll go back and change them sometime soon...

The Pixie
HegemonKhan wrote:I'm explaining the 'if~else if' here, NOT the 'if~else' situation. Yes, my example is bad design (I know: extra un-needed operations by using 'else if' instead of 'else' as it checks the condition: flag=false, when it shouldn't~doesn't have to), but it's very simple, and thus makes it easy for people to understand how the 'if~if else' works, which is the point here. ...

If you are teaching people, you should be teaching them to do it right. Find an example that is good design.

If you are doing "if/else if" then you need two different conditions, not one condition and its negative. If you have one condition and its negative, you do "if/else", and not "if/else if".

HegemonKhan
sighs, after my finals, when I have the free time needed, I'll edit up my post, making it better, thought out on its design, what examples to use, etc, and organized hopefully well, as a teaching guide on the IF block, so I'm "teaching them to do it right".

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums