PORTB = 0b11001011;
PORTC = 0b00111011;
if(PORTB & PORTC)
{
//do something
}
//will this "if" comes out to be true?how compiler will check it?
PORTB = 0b11001011;
PORTC = 0b00111011;
if(PORTB && PORTC)
{
//do something
}
//how this "&&" be compared? if i write "if(PORTB)" than how compiler wil evaluate it?
& does bit and, so
PORTB & PORTC == 0b00001011
converting this to a boolean yields true (non-zero)
&& does boolean and, so PORTB is true (non-zero), and PORTC is true, so
PORTB && PORTC
yields true
So is there a difference? Yes. && uses short-circuiting, while & doesn't. Consider the following:
0 & some_complicated_expression
0 && some_complicated_expression
In the first case, the complicated expression will be evaluated. However, since && uses short-circuiting, the complicated expression is not evaluated in the second case (the expression yields false, irrespective of the result of the complicated expression).
Related
I see in scala it is a bad practice to use both while and return. I have to click on the next page link if it exists. How can I do it without while and return?
def check = {
do {
if (findElements(locator).exists(_.getText.contains(input)))
return true
} while (nextPageLocatorInPageExists(nextPageLocator))
false
}
Here's a straightforward recursive solution:
def check = {
if (findElements(locator).exists(_.getText.contains(input)))
true
else if (! nextPageLocatorInPageExists(nextPageLocator))
false
else
check
}
It can be written in a terser way by using || and &&, but then it wouldn't be tail recursive, and that's pretty much mandatory in a case like this.
From your code, I'm guessing nextPageLocatorInPageExists(nextPageLocator) mutatates locator, and that's what drives your while loop.
If that's the case, here are two options:
a do-while loop without return: the closest to what you wrote:
def check = {
var found = false
do {
found = findElements(locator).exists(_.getText.contains(input))
} while (!found && nextPageLocatorInPageExists(nextPageLocator))
found
}
a recursive solution
def check: Boolean = findElements(locator).exists(_.getText.contains(input)) ||
(nextPageLocatorInPageExists(nextPageLocator) && check)
I have problem with the (&&) logic in swift, I don't understand why the compiler deal with 'and' logic as 'or' logic, I enter one field and he ignore the others
if (self.txtAddressNickname.text!.isEmpty == true) && (self.txtArea.text!.isEmpty ==
true) && (self.txtHouseNo.text.isEmpty == true) && (self.txtAddressType.text!.isEmpty
== true) && (self.txtStreet.text!.isEmpty == true) && (self.txtHouseNo.text!.isEmpty
== true) && (self.txtMobileNo.text!.isEmpty == true){
// alert
}
You want to show an alert if any of the fields is empty, right?
You need 'or', not 'and'.
if txtAddressNickname.text!.isEmpty ||
txtArea.text!.isEmpty ||
txtHouseNo.text.isEmpty ||
txtAddressType.text!.isEmpty ||
txtStreet.text!.isEmpty ||
txtHouseNo.text!.isEmpty ||
txtMobileNo.text!.isEmpty {
// alert
}
I also tidy your code up little bit to read more easly.
&& works like:
If 1 condition on the way give false, compiler won't check the further conditions in the expression.
Similarly, || works like,
If 1 condition on the way give true, compiler won't check the further conditions in the expression.
So, in your expression any of the initial textField might be empty, so it is not checking further.
AND and OR logic is the same in all programming languages.
A and B is true only if A is true and B is true.
A or B is true if A is true or B is true or both of them are true.
Check the images below, note that 0 is false and 1 is true :
In other languages, I've done logical expressions like this one without any issues, but I'm having a tough time in Swift.
I want this to evaluate as true if appPurchased = false AND enabled = true AND the button equals either photoLibraryBtn or takeVideoBtn:
for button in buttonList {
if appPurchased == false &&
enabled == true &&
button == photoLibraryBtn |
button == takeVideoBtn {
continue
}
button.isEnabled = enabled
button.isUserInteractionEnabled = enabled
button.alpha = alpha
}
I get the error "Adjacent operators are in non-associative precedence group 'ComparisonPrecedence'", which I can find no results on Google. I also don't see examples such as mine in Swift, so I'm thinking they did away with the single "|" pipe character, and you're only supposed to use the double pipe "||", but in a certain order. However, I don't want the if statement to pass as true if appPurchased = false, enabled = true, button = photoLibraryBtn OR button = takeVideoBtn.
You need ||, not |. || is "logical or". | is "bitwise or".
And when you mix || and &&, you need parentheses to avoid any ambiguity.
Based on your description, you want:
if appPurchased == false &&
enabled == true &&
(button == photoLibraryBtn ||
button == takeVideoBtn) {
continue
}
This can also be written as:
if !appPurchased &&
enabled &&
(button == photoLibraryBtn ||
button == takeVideoBtn) {
continue
}
This piece of code sorts an array of strings by placing strings with a specific prefix 'searchTerm' at the beginning of the array, and strings that don't have that prefix at the end. Can someone explain how this function works? I'm new at developing in Swift if you couldn't already tell, so please keep the explanation as SIMPLE as possible. Thank you!
stringArray = stringArray.sorted(by: {
switch ($0.hasPrefix(searchTerm), $1.hasPrefix(searchTerm) {
case (true, true): return $0 < $1 ;
case (true, false): return true ;
case (false, true): return false ;
case (false, false): return $0 < $1
}
})
The sorted(by:) function takes a closure as its input parameter, where you can define your own sorting function. The function iterates through the array in element pairs.
$0 and $1 are so called anonymous closure parameters, in this case, $0 is the current element and $1 is the next element.
The switch statement checks whether the current or the next element starts with the string that's stored in the searchTerm variable.
The different states of the switch statement define how the elements should be sorted. If both or neither of the elements have the prefix, they are alphabetically sorted. If only one of them has the prefix, the one that has it will come first in the sorted array.
Hope my explanation was sufficient, if not, just tell me what should I clarify in more detail.
The sorted method you are using runs through the array sorting the items according to the criteria you provide in the block. This block needs to return true if one item ($0) should be ordered before the other ($1)
Since you are sorting first on if there is a prefix before the value of the string the comparison is being made like this:
switch ($0.hasPrefix(searchTerm), $1.hasPrefix(searchTerm) {
case (true, true): return $0 < $1 ; // If both items have the prefix, just sort them the way strings are normally sorted
case (true, false): return true ; // Since the first item has a prefix and the second does not, it should appear before the second one, so return true.
case (false, true): return false ; // Since the second item has a prefix, and the first one does not, the first item should be sorted after the second, so return false
case (false, false): return $0 < $1 // Since neither items have a prefix, just sort them the way strings are normally sorted.
}
An alternative way to do this which might be clearer:
array.sorted { (first, second) -> Bool in
switch (first.hasPrefix("p"), second.hasPrefix("p")) {
case (true, true), (false, false): return first < second
case (true, false): return true
case (false, true): return false
}
}
stringArray = stringArray.sorted(by: {
Here, you are reassigning stringArray to be the sorted version of itself. The inclusion of the by parameter indicates to Swift that you would like it to use your own sorting order instead of the default one (in this case, things that begin with "searchTerm" come before anything else).
To describe your sorting order, you use a comparison function -- a piece of code that takes two elements from your array and compares them. Let's call these elementA and elementB. If elementA should come before elementB, your code should return true. If elementB should come before elementA, your code should return false. By using your code to determine which elements come before other elements, Swift can sort your array based on this specification.
switch ($0.hasPrefix(searchTerm), $1.hasPrefix(searchTerm) {
This line takes the two elements ($0 and $1) and checks if they begin with "searchTerm." For each case, the two booleans in the expression indicate if $0 and $1 start with "searchTerm" respectively.
case (true, true): return $0 < $1 ;
Both of these elements start with "searchTerm," so you want to return the natural ordering (i.e. alphabetical ordering). You let Swift use the standard comparison operator, because $0 < $1 will return true if $0 comes before $1 and false otherwise.
case (true, false): return true ;
$0 starts with "searchTerm," but $1 does not. You want $0 to come before $1, so you return true.
case (false, true): return false ;
$0 does not start with "searchTerm," but $1 does. You want $1 to come before $0, so you return false.
case (false, false): return $0 < $1
Neither $0 nor $1 start with "searchTerm", so you use natural ordering again.
sorted(by:) lets you specify your own sorting criteria. It performs the sorting by repeatedly asking you how certain elements compare to each other, by running the closure you gave it and passing in the elements as parameters.
This closure comes down to four possible cases:
case (true, true): return $0 < $1
If both $0 and $1 start with searchTerm, then the ordering between them is decided by regular string comparison ($0 < $1)
case (true, false): return true
If $0 starts with searchTerm, but $1 doesn't, then $0 should come before $1
case (false, true): return false
If $0 starts with searchTerm, but $1 doesn't, then $1 should come after $0
case (false, false): return $0 < $1
If neither $0 nor $1 start with searchTerm, then the ordering between them is decided by regular string comparison ($0 < $1)
As the title says, I just came across a case where if && (AND) and , give the same result in Swift. I tested the code below:
let a = 5
let b = 6
if a<6, b>0 {
print("should be true")
}
if a<6, b<0 {
print("should be false")
}
if a>6, b>0 {
print("should be false")
}
if a>6, b<0 {
print("should be false")
}
It only logs:
should be true
So, the behavior of , is just like &&, am I correct?
They can be used in similar situations but that does not mean they are exactly the same.
Consider:
if (a && b) || c
you cannot write
if (a, b) || c
even a && b || c is different from a, b || c.
if a, b is something like
if a {
if b {
...
}
}
Both expressions have to be evaluated to true but they are still two separate expressions. We shouldn't imagine && there.
Why do we need the , operator?
The operator is needed to combine optional binding with boolean conditions, e.g.
if let a = a, a.isValid() {
becuase && wouldn't help us in such situations.
They're different in that a comma will take the lowest precedence possible. i.e everything else will be executed before the commas are checked.
// (true || false) && false
if true || false, false {
print("I will never be executed")
}
// true || (false && false)
if true || false && false {
print("I am always executed")
}