if (Condition1)
{
dothis;
}
else if (Condition1)
{
dothat;
}
Out of curiosity, when I invoke the same condition for the else if, will the dothat part of the code still execute when Condition1 is satisfied? Does this vary from programming language to programming language?
What you wrote is basically equivalent to
if(Condition1)
{
dothis;
}
else
{
if(Condition1)
{
dothat;
}
}
So no, it will never be called.
No, the second condition will never be executed in any language. Here is a basic flowchart of your logic.
However, if Condition1 somehow evaluates to false in the first condition and true in the second, then the second will be called. Example:
<?php
$var = true;
function condition() {
global $var;
$var = !$var;
return $var;
}
if(condition()) {
echo "Conditional 1";
} elseif(condition()) {
echo "Conditional 2";
}
In this case, "Conditional 2" will be printed because condition() first evaluates to false, and then to true.
While at most one branch is executed1 per if-else-statement, in cases where the "condition" contains a a side-effect, it might not always be the if branch that is executed.
For instance, consider this JavaScript code with a side-effect introduced by the "condition" expression.
y = -1;
if (++y) { // the "condition" is ++y
alert("1")
} else if (++y) {
alert("2")
}
Only one branch ("2") was executed, but the same "condition" was used in both cases. For such reasons it can be problematic to have side-effects in conditionals.
1 In all modern mainstream languages the conditions for an if-else are evaluated "on demand" and "in order". Thus the first branch for which the condition evaluates to true is executed, regardless of other side-effects, and it is the only branch executed.
The second if follows an else – consequently, it will only be executed if the first if does not succeed. So – no; the dothat will not be executed. All (sane) programming languages do this.
However, some programming languages have special constructs for an else if, and you have to write it as one statement, variously called elif, elsif or ElseIf, depending on the language.
Since Condition1 evaluated to True the if's "dothis" will be invoked and skip over the else if. If you wanted to invoke the dothis and dothat then you would have two if statements with the same Condition1 or just put both dothis and dothat under one if statement. This is what I have seen in computer programming languages I have used.
Python example.
b = 2
if b == 2:
print "Hello"
elif b == 2:
print "World"
...
Hello
Related
Why is the else statement is not allowed to have a then or other conditions?
Is it because it is the final condition within the else-if conditions it represents?
I am struggling to understand this concept since I'm a beginner who just learned about variables.
I'm asking this because I received an error with my else statement in the code:
message = 0
condition = 30
if condition <=10
message = “try harder”
elseif
condition <=20 then
message = "Almost learning"
else
condition = <=30 **—This is the line where I get the error message**
message = "Now you’re getting it"
end
print(message)
Would appreciate someone breaking down in laymen terms, why else is not allowed to have < or > or then or other conditions.
else condition = <= 30
(which is the way your code was originally formatted) would be a very unusual feature in a language.
The whole point of else on its own is to execute if none of the other conditions were true. So a condition on the else is absolutely useless.
The Programming in Lua book if statement shows the normal usage:
if op == "+" then
r = a + b
elseif op == "-" then
r = a - b
elseif op == "*" then
r = a*b
elseif op == "/" then
r = a/b
else
error("invalid operation")
end
However, your actual code (when formatted correctly) ends up looking like:
else
condition = <=30
which is correct in terms of the else but unfortunately makes the next line a statement. And this statement is very much incorrect syntax.
Now it may be that you meant to assign 30 to condition but, based on your other lines (that sort of match this as a condition), I suspect not. So it's probably best just to remove that line totally.
Excuse me for the use of the term "component", there probably is a better term to use in such context.
But moving on to my question, I want to use the else statement to execute a statement block based on the truth of the test statement. Here is the test statement I tried using:
else:
if ((reply != a) && (reply != c) && (reply =! e)):
I'm getting a syntax error, and the carrot is pointing at the first set of ampersands. I'm assuming now that I might be improperly using '&&'.
With this statement, my goal is to execute the statement block only if the test statement is true, meaning further, that 'reply' must not be equal to a, c, or e.
I know that I can use nested if's under the else statement, but I'm hoping StackExchange knows a better way. Thank you.
parenthesis check :
if (((reply != a) && (reply != c)) && (reply =! e))
I am strugging a bit to implement a nested if else expressions in SML. Can anyone highlight its syntax. Suppose there are three conditions C1, C2, C3 I need equivalent of following in C code.
if (C1) {
return true;
}
else {
if(C2) {
return true;
}
else {
if (C3) {
return true;
}
else {
return false;
}
}
}
I tried the following, but its treated as "if, else if, and else" cases
if C1 then true
else if C2 then true
else if C3 then true
else false
You're correct. Two code fragments are equivalent.
With a bit of indentation, your SML example looks more like using nested if/else:
if C1 then true
else
if C2 then true
else
if C3 then true
else false
You could also use parentheses so that SML example looks almost the same as C example but it isn't necessary.
Of course, the most idiomatic way in SML is to write
C1 orelse C2 orelse C3
You could use the same trick for your C code. Remember that returning true/false in if/else blocks is code smell.
I agree that using orelse is the right way to go here, but just as an alternative to situations where you want to act on more complex combinations, pattern matching would be able to help you.
fun funtrue (false,false,false) = false
| funtrue _ = true
or as a case statement
case (C1,C2,C3) of
(false,false,false) => false
| _ => true
I'm just starting out using Lua, and I was wondering (because I can't find it on the website) if Lua has a OR operator, like how in other languages there is ||:
if (condition == true || othercondition == false) {
somecode.somefunction();
}
whereas in Lua, there is
if condition then
x = 0
end
how would i write an IF block in Lua to use OR?
With "or".
if condition or not othercondition then
x = 0
end
As the Lua manual clearly states.
So I've been actively programming bot in school and work the past 5 years, but I never tried to find out the difference between == and ===.
I can see the difference of a comparator using a single =, it'll look at the value of the left handed variable through the loop, ex:
while($line = getrow(something))
So what's the difference between == and === in statements such as:
if ($var1 === $var2)
//versus
if ($var1 == $var2)
Likewise:
if ($var1 !== $var2)
//versus
if ($var1 != $var2)
I have always used double equals, I have never used tripple.
The languages I use are :php, vb.net, java, javascript, c/c++.
I'm interested in learning systematically what is going on in a tripple quote that is different than that of a double quote.
When should one be used over another? Thanks for appeasing to my curiosity :)
Typically, == looks at equality of value only. So, for instance...
5 == 5.0 //true
However, === also considers value and type (in the languages I am familiar with).
var five = 5;
var five_float = (float)5.0;
five === 5; //true - both int, both equal to 5
five_float === 5; //false - both equal 5 but one is an int and one is a float
FYI, the = operator (usually called the assignment operator) is used to set the value of the left side parameter to the right side. This is pretty obvious. However, in most languages, this will also return true if the assignment is successful. You want to avoid using = where you mean to use == (or ===) because it will look like a comparison, but it's not - and it will return true unexpectedly.
For instance, lets say you want to check if a number is equal to 10...
myNumber = 7;
if(myNumber = 10)
{
//will always be true and execute this code because myNumber will successfully
//be assigned the value of 10 instead of checking to see if the number is 10.
//oops!
}
A final note - this is true in PHP and JavaScript. I don't think there is a === operator in C++ or Java and == has a slightly different meaning as well.
$a === $b TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)
$a !== $b TRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4)
Reference
== will check the value only (equality operator), where === checks the data type as well (strict equality operator).
1 == '1' is true.
1 === '1' is false - the first is an Integer, the second is a String.
1 == true is true.
1 === true is false - the first is an Integer, the second is a Boolean.
Generally you want to use == (equality operator) but sometimes you want to make sure things are of certain types. I'm sure someone can provide an example, I can't think of one off the top of my head, but I've definitely used it.
In PHP and JavaScript (I'm not sure of other languages where the triple === syntax is valid) the difference is that === is a strict comparison. While == is loose. That means that === compares value and type, but == just compares value. A perfect example of this is the buggy PHP code below:
$str = 'Zebraman stole my child\'s pet lime!';
// Search for zebra man
if(strpos($str, 'Zebraman')){
echo 'The string contains "Zebraman"';
}else{
echo 'The string doesn\'t contain "Zebraman"';
}
Example Here
Since strpos($str, 'Zebraman') returns 0 (The index of the string Zebraman), and since 0 is falsy. That code will output The string doesn't contain "Zebraman". The correct code uses a strict comparison with false:
$str = 'Zebraman stole my child\'s pet lime!';
// Search for zebra man
if(strpos($str, 'Zebraman') !== false){
echo 'The string contains "Zebraman"';
}else{
echo 'The string doesn\'t contain "Zebraman"';
}
Example Here
See the PHP man page on strpos
I don't know if this holds true for all languages but in javascript the === stands for type comparison.
0 == false (true) 0 === false (false)
It is a common js error to not use the === when comparing a falsy value.
var a;
if(a) do something
(if a is zero the if will not get entered)