Logical operations: Difference between revisions
Content deleted Content added
→{{header|UNIX Shell}}: Add implementation. |
|||
Line 3,893: | Line 3,893: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
The shell has at least two levels of logical operators. Conditional logic (<tt>if</tt>, <tt>while</tt>, <tt>&&</tt> and <tt>||</tt> at the statement level) operates on commands; the commands are executed, and their exit status determines their value in a Boolean context. If they return an exit code of 0, signaling successful execution, that is considered a "true" result; if they return a nonzero exit code, signaling a failure condition, that is considered a "false" result. However, these results are not returned as a Boolean value. <tt>if command; then do something; fi</tt> will do something if the |
The shell has at least two levels of logical operators. Conditional logic (<tt>if</tt>, <tt>while</tt>, <tt>&&</tt> and <tt>||</tt> at the statement level) operates on commands; the commands are executed, and their exit status determines their value in a Boolean context. If they return an exit code of 0, signaling successful execution, that is considered a "true" result; if they return a nonzero exit code, signaling a failure condition, that is considered a "false" result. However, these results are not returned as a Boolean value. <tt>if command; then do something; fi</tt> will do something if the command succeeds, but there's no "true" value, only the zero exit status. So this demo uses a function that examines the exit status of the last command and prints "true" if it is 0 and "false" otherwise. The two values for the task are the <em>commands</em> <tt>true</tt> and <tt>false</tt>, which do nothing but exit with status 0 and 1, respectively. |
||
{{works with|Bourne Again SHell}} |
{{works with|Bourne Again SHell}} |
||
Line 3,928: | Line 3,928: | ||
1 or 0 = 1 |
1 or 0 = 1 |
||
not 1 = 0</pre> |
not 1 = 0</pre> |
||
=={{header|V}}== |
=={{header|V}}== |