When an if case is true, it should run another if case - xojo

I'm programming in Xojo and I want my computer to first run an if case and if that case is true, it should run another if case. How can I do that with Xojo ?

Below is an example of an If statement that includes the use of ElseIf and Else clauses:
Var theNumber As Double
If theNumber > 0 And theNumber < 100 Then
MessageBox("The entered number is between 0 and 100")
ElseIf theNumber == 0 Or the number == 100 Then
MessageBox("The entered number is either 0 or 100")
ElseIf theNumber < 0 Or theNumber > 100 Then
MessageBox("The entered number is either below 0 or above 100")
Else
MessageBox("Invalid input. Please try again...")
End If

Related

Check for equality between more than 2 values in Lua

In Lua 5.3.3, equalities between two values work normally using the == operator.
However, equalities between more than two values don't work.
> print(1 == 1 == 1)
false
>
How can I check if more than 2 values are equal to one another in Lua?
You should compare 2 values at a time:
print((1 == 1) and (1 == 1))
If you have specific needs and if this approach is not acceptable, then you could also write a dedicated function:
function EqualsAll (Values, Number)
local Equals = true
local Index = 1
while (Equals and (Index <= #Values)) do
if Values[Index] == Number then
Index = Index + 1
else
Equals = false
end
end
return Equals
end
You could use it as following:
> EqualsAll({1, 2, 3}, 1)
false
> EqualsAll({1, 1, 1}, 1)
true

Recursive algorithm to evaluate postfix language in dafny

I have a sequence of this datatype
datatype Op = Operand(int) | Addition | Multiplication
I wrote the following evaluation function but it does not work. (I think the recursion is not correct but I do not know how to fix it.)
function method ValueOfPostFix(postfix: seq<Op>, index : nat): int
requires 0 <= index < |postfix|
decreases index
{
match postfix[index]
{
case Operand(x) => x
case Multiplication => if (index > 0) then ValueOfPostFix(postfix, index-1) *
ValueOfPostFix(postfix, index-1)
else 0
case Addition => if (index > 0) then ValueOfPostFix(postfix, index-1) +
ValueOfPostFix(postfix, index-1)
else 0
}
}

While loop with double conditional and || logical operator in Swift

I have a While Loop with a condition that limits it to repeat only 10 times, every time a cycle is repeated a constant D generates a random number in a range from 0 to 24, if D is 0, I change a variable dIsZero to true and prints the cycle where D is 0 for the first time.
var S = 0
var dIsZero = false
while S < 10 || dIsZero == false {
S += 1
let D = Int.random(in: 0...24)
if dIsZero == false && D == 0 {
dIsZero = true
print("D = 0 in a cycle \(S)/10")
}
}
My problem is that I want the While Loop can also end when D is 0 before the 10 cycles are completed. I already tried to put the logical operator || but it doesn't work and I get the following results:
10 cycles are exceeded until D is 0. For example: 84 cycles.
If D is 0 before 10 cycles, the loop does not stop until that 10 cycles
are reached.
I read about the logical operators and found the following:
The Swift logical operators && and || are left-associative, meaning
that compound expressions with multiple logical operators evaluate the
leftmost subexpression first.
What solution do you recommend?
You just need to break loop
while S < 10 {
S += 1
let D = Int.random(in: 0...24)
if D == 0 {
print("D = 0 in a cycle \(S)/10")
break
}
}

Ruby on Rails function defined failed tests

I have defined a function that takes in a number and returns true if it is a
power of 2. Otherwise, return false:
def is_power_of_two?(num)
n = 0
res = false
if num % 2 == 0
while 2^n <= num
if 2^n == num
res = true
end
n += 1
end
end
puts(n.to_s)
return res
end
# These are tests to check that your code is working. After writing
# your solution, they should all print true.
puts('is_power_of_two?(1) == true: ' + (is_power_of_two?(1) == true).to_s)
puts('is_power_of_two?(16) == true: ' + (is_power_of_two?(16) == true).to_s)
puts('is_power_of_two?(64) == true: ' + (is_power_of_two?(64) == true).to_s)
puts('is_power_of_two?(78) == false: ' + (is_power_of_two?(78) == false).to_s)
puts('is_power_of_two?(0) == false: ' + (is_power_of_two?(0) == false).to_s)
However, my test results turn out to fail four out of five:
0
is_power_of_two?(1) == true: false
16
is_power_of_two?(16) == true: false
64
is_power_of_two?(64) == true: false
77
is_power_of_two?(78) == false: false
0
is_power_of_two?(0) == false: true
The result printed out seems to match what's expected, however, the tests still failed. Does anyone know why this happened?
if you expecting ^ to calculate the power then that is wrong ^ is XOR to calculate power use **
2^2 # 0
2**2 # 4
You always want to check if it is true that it is a power of two, this way it returns false when it is not a power of two.
This feels like a homework question so I'm not going to give you the exact answer, but this should nudge you in the correct direction.
As mohamed-ibrahm says, you're using the wrong operator.
The caret is a bitwise XOR operation. So 2^3 == 1 (because decimal 2 is 010 in binary and decimal 3 is 011 in binary and as all bits are the same except the last, the result is 001 or decimal 1).
Exponentiation is done by double asterisks, so 2**3 == 8
Here's a description of the various operators.
http://www.tutorialspoint.com/ruby/ruby_operators.htm

all values same sign validation

User should insert all the values either positive or negative.
How may i set same sign validation ?
Right i have written this on before_save ..
unless (self.alt_1 >= 0 && self.alt_2 >=0 && self.alt_3 >= 0 &&
self.alt_4 >= 0 && self.alt_5 >= 0 && self.alt_6 >= 0) ||
(self.alt_1 <= 0 && self.alt_2 <=0 && self.alt_3 <= 0 &&
self.alt_4 <= 0 && self.alt_5 <= 0 && self.alt_6 <= 0)
self.errors.add_to_base(_("All values sign should be same."))
end
first_sign = self.alt_1 <=> 0
(2..6).each do |n|
unless (self.send("alt_#{n}") <=> 0) == first_sign
errors.add_to_base(_("All values' signs should be same."))
break
end
end
With this method we first get the sign of alt_1, and then see if the signs of the rest of the elements (alt_2 through alt_6) match. As soon as we find one that doesn't match we add the validation error and stop. It will run a maximum of 6 iterations and a minimum of 2.
Another more clever, but less efficient method, is to use the handy method Enumerable#all?, which returns true if the block passed to it returns true for all elements:
range = 1..6
errors.add_to_base(_("All values' signs should be same.")) unless
range.all? {|n| self.send("alt_#{n}") >= 0 } ||
range.all? {|n| self.send("alt_#{n}") <= 0 }
Here we first check if all of the elements are greater than 0 and then if all of the elements are less than 0. This method iterates a maximum of 12 times and a minimum of 6.
Here's a slightly different approach for you:
irb(main):020:0> def all_same_sign?(ary)
irb(main):021:1> ary.map { |x| x <=> 0 }.each_cons(2).all? { |x| x[0] == x[1] }
irb(main):022:1> end
=> nil
irb(main):023:0> all_same_sign? [1,2,3]
=> true
irb(main):024:0> all_same_sign? [1,2,0]
=> false
irb(main):025:0> all_same_sign? [-1, -5]
=> true
We use the spaceship operator to obtain the sign of each number, and we make sure that each element has the same sign as the element following it. You could also rewrite it to be more lazy by doing
ary.each_cons(2).all? { |x| (x[0] <=> 0) == (x[1] <=> 0) }
but that's less readable in my opinion.
unless
[:<=, :>=].any? do |check|
# Check either <= or >= for all values
[self.alt1, self.alt2, self.alt3, self.alt4, self.alt5, self.alt6].all? do |v|
v.send(check, 0)
end
end
self.errors.add_to_base(_("All values sign should be same."))
end

Resources