How to resolve the algorithm Short-circuit evaluation step by step in the Kotlin programming language
How to resolve the algorithm Short-circuit evaluation step by step in the Kotlin programming language
Table of Contents
Problem Statement
Assume functions a and b return boolean values, and further, the execution of function b takes considerable resources without side effects, and is to be minimized. If we needed to compute the conjunction (and): Then it would be best to not compute the value of b() if the value of a() is computed as false, as the value of x can then only ever be false. Similarly, if we needed to compute the disjunction (or): Then it would be best to not compute the value of b() if the value of a() is computed as true, as the value of y can then only ever be true. Some languages will stop further computation of boolean equations as soon as the result is known, so-called short-circuit evaluation of boolean expressions
Create two functions named a and b, that take and return the same boolean value. The functions should also print their name whenever they are called. Calculate and assign the values of the following equations to a variable in such a way that function b is only called when necessary: If the language does not have short-circuit evaluation, this might be achieved with nested if statements.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Short-circuit evaluation step by step in the Kotlin programming language
The code snippet you provided creates an array of pairs of booleans, and then it iterates over the array, calling two functions, a
and b
, on each pair. The functions a
and b
print a message and return the value of the boolean passed to them.
The &&
operator is used to perform a logical AND operation on the two booleans, and the ||
operator is used to perform a logical OR operation on the two booleans.
The output of the code is as follows:
'a' called
'b' called
true && true = true
'a' called
true || true = true
'a' called
'b' called
true && false = false
'a' called
true || false = true
'a' called
'b' called
false && true = false
'a' called
false || true = true
'a' called
'b' called
false && false = false
'a' called
false || false = false
Source code in the kotlin programming language
// version 1.1.2
fun a(v: Boolean): Boolean {
println("'a' called")
return v
}
fun b(v: Boolean): Boolean {
println("'b' called")
return v
}
fun main(args: Array<String>){
val pairs = arrayOf(Pair(true, true), Pair(true, false), Pair(false, true), Pair(false, false))
for (pair in pairs) {
val x = a(pair.first) && b(pair.second)
println("${pair.first} && ${pair.second} = $x")
val y = a(pair.first) || b(pair.second)
println("${pair.first} || ${pair.second} = $y")
println()
}
}
You may also check:How to resolve the algorithm Exceptions step by step in the Aikido programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the TXR programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Wren programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the Julia programming language
You may also check:How to resolve the algorithm Remove lines from a file step by step in the Tcl programming language