Tutorial by Examples: conditions

A bit counter-intuitive to the way most other languages' standard I/O libraries do it, Haskell's isEOF does not require you to perform a read operation before checking for an EOF condition; the runtime will do it for you. import System.IO( isEOF ) eofTest :: Int -> IO () eofTest line = do ...
prop_evenNumberPlusOneIsOdd :: Integer -> Property prop_evenNumberPlusOneIsOdd x = even x ==> odd (x + 1) If you want to check that a property holds given that a precondition holds, you can use the ==> operator. Note that if it's very unlikely for arbitrary inputs to match the precondit...
Due to Node's asynchronous nature, creating or using a directory by first: checking for its existence with fs.stat(), then creating or using it depending of the results of the existence check, can lead to a race condition if the folder is created between the time of the check and the time of ...
Preconditions allows methods to provide minimum required values for input parameters Example... void DoWork(string input) { Contract.Requires(!string.IsNullOrEmpty(input)); //do work } Static Analysis Result...
Postconditions ensure that the returned results from a method will match the provided definition. This provides the caller with a definition of the expected result. Postconditions may allowed for simplied implmentations as some possible outcomes can be provided by the static analyizer. Example......
In the condition of the for and while loops, it's also permitted to declare an object. This object will be considered to be in scope until the end of the loop, and will persist through each iteration of the loop: for (int i = 0; i < 5; ++i) { do_something(i); } // i is no longer in scope...
Most browsers, when configured to block cookies, will also block localStorage. Attempts to use it will result in an exception. Do not forget to manage these cases. var video = document.querySelector('video') try { video.volume = localStorage.getItem('volume') } catch (error) { alert('If...
See this Q&A question if you don't know what race conditions are. The following code may be subject to race conditions : article = Article.objects.get(pk=69) article.views_count += 1 article.save() If views_count is equal to 1337, this will result in such query: UPDATE app_article SET vi...
Orders Table CustomerIdProductIdQuantityPrice12510013220014150021450356700 To check for customers who have ordered both - ProductID 2 and 3, HAVING can be used select customerId from orders where productID in (2,3) group by customerId having count(distinct productID) = 2 Return value:...
A data race or race condition is a problem that can occur when a multithreaded program is not properly synchronized. If two or more threads access the same memory without synchronization, and at least one of the accesses is a 'write' operation, a data race occurs. This leads to platform dependent, p...
(defun fn (x) (cond (test-condition the-value) (t (fn reduced-argument-x))))
When a runtime error occurs, good code should handle it. The best error handling strategy is to write code that checks for error conditions and simply avoids executing code that results in a runtime error. One key element in reducing runtime errors, is writing small procedures that do one thing. Th...
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...
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; } } ...
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 ...
In order to use a conditional jump a condition must be tested. Testing a condition here refers only to the act of checking the flags, the actual jumping is described under Conditional jumps. x86 tests conditions by relying on the EFLAGS register, which holds a set of flags that each instruction ca...
using System.Diagnostics.Contracts; public int IncrementByRandomAmount(int input) { Contract.Requires<ArgumentNullException>(input != null); // Don't allow null parameter. Contract.Requires<ArgumentOutOfRangeException>(input < int.MaxValue); // We can't do anything if...
The following statement if (conditionA && conditionB && conditionC) //... is exactly equivalent to bool conditions = conditionA && conditionB && conditionC; if (conditions) // ... in other words, the conditions inside the "if" statement just form an...
All documents from students collection. > db.students.find().pretty(); { "_id" : ObjectId("58f29a694117d1b7af126dca"), "studentNo" : 1, "firstName" : "Prosen", "lastName" : "Ghosh", "age" ...

Page 1 of 2