Tutorial by Examples: condition

Java provides a conditional-and and a conditional-or operator, that both take one or two operands of type boolean and produce a boolean result. These are: && - the conditional-AND operator, || - the conditional-OR operators. The evaluation of <left-expr> && <righ...
Conditions are a fundamental part of almost any part of code. They are used to execute some parts of the code only in some situations, but not other. Let's look at the basic syntax: a = 5; if a > 10 % this condition is not fulfilled, so nothing will happen disp('OK') end if a < 1...
In some cases we want to run an alternative code if the condition is false, for this we use the optional else part: a = 20; if a < 10 disp('a smaller than 10') else disp('a bigger than 10') end Here we see that because a is not smaller than 10 the second part of the code, after t...
A condition variable is a primitive used in conjunction with a mutex to orchestrate communication between threads. While it is neither the exclusive or most efficient way to accomplish this, it can be among the simplest to those familiar with the pattern. One waits on a std::condition_variable with...
The AND keyword is used to add more conditions to the query. NameAgeGenderSam18MJohn21MBob22MMary23F SELECT name FROM persons WHERE gender = 'M' AND age > 20; This will return: NameJohnBob using OR keyword SELECT name FROM persons WHERE gender = 'M' OR age < 20; This will return: n...
# example data DT = data.table(Titanic) Suppose we want to see each class only if a majority survived: DT[, if (sum(N[Survived=="Yes"]) > sum(N[Survived=="No"]) ) .SD, by=Class] # Class Sex Age Survived N # 1: 1st Male Child No 0 # 2: 1st Fema...
def results = [] (1..4).each{ def what = (it%2) ? 'odd' : 'even' results << what } assert results == ['odd', 'even', 'odd', 'even'] Here, the if-condition (in (parentheses)) is slightly more complex than just testing for existence/Groovy-Truth.
Using else we can perform some task when the condition is not satisfied. But what if we want to check a second condition in case that the first one was false. We can do it this way: a = 9; if mod(a,2)==0 % MOD - modulo operation, return the remainder after division of 'a' by 2 disp('a is ev...
When we use a condition within another condition we say the conditions are "nested". One special case of nested conditions is given by the elseif option, but there are numerous other ways to use nested conditons. Let's examine the following code: a = 2; if mod(a,2)==0 % MOD - modulo o...
You use the condition attribute to specify the condition to use. <cfset myVar=false> <cfloop condition="myVar eq false"> <cfoutput> myVar = <b>#myVar#</b> (still in loop)<br /> </cfoutput> <cfif RandRange(1,10) eq 10> ...
Multiple conditions can be written using where() method as given below. // Creates a new \yii\db\Query() object $query = new \yii\db\Query(); $rows = $query->select(['emp_name','emp_salary']) ->from('employee') ->where(['emp_name' => 'Kiran', 'emp_salary' => 2500...
Extension Method can work on null references, but you can use ?. to null-check anyway. public class Person { public string Name {get; set;} } public static class PersonExtensions { public static int GetNameLength(this Person person) { return person == null ? -1 : pers...
namespace CodeContractsDemo { using System; using System.Collections.Generic; using System.Diagnostics.Contracts; public class PaymentProcessor { private List<Payment> _payments = new List<Payment>(); public void Add(Payment payment) ...
public double GetPaymentsTotal(string name) { Contract.Ensures(Contract.Result<double>() >= 0); double total = 0.0; foreach (var payment in this._payments) { if (string.Equals(payment.Name, name)) { total += payment.Amount; } } ...
eof returns true only after reading the end of file. It does NOT indicate that the next read will be the end of stream. while (!f.eof()) { // Everything is OK f >> buffer; // What if *only* now the eof / fail bit is set? /* Use `buffer` */ } You could correctly write: ...
Sometimes you may need to validate record only under certain conditions. class User < ApplicationRecord validates :name, presence: true, if: :admin? def admin? conditional here that returns boolean value end end If you conditional is really small, you can use a Proc: class ...
pushunique!(A, x) = x in A ? A : push!(A, x) The ternary conditional operator is a less wordy if...else expression. The syntax specifically is: [condition] ? [execute if true] : [execute if false] In this example, we add x to the collection A only if x is not already in A. Otherwise, we jus...
In explicit wait, you expect for a condition to happen. For example you want to wait until an element is clickable. Here is a demonstration of a few common problems. Please note: In all of these examples you can use any By as a locator, such as classname, xpath, link text, tag name or cssSelector ...
The markdown help states Available conditionals are gt, gte, lt, lte, eq, and neq. <!-- if version [eq C++03] --> eq equal to <!-- end version if --> <!-- if version [neq C++03] --> neq not equal to <!-- end version if --> <!-- if version [gt C++03] -...
Multiple conditions can be used. <!-- if version [lte 1.2.0] [gt 1.3.12] [gt 1.4.9] [neq 1.2.0] [eq 1.5.7] --> content <!-- end version if -->

Page 4 of 7