Tutorial by Examples: condition

The Default keyword is used to execute an action when no other conditions match the input value. Example: switch('Condition') { 'Skip Condition' { 'First Action' } 'Skip This Condition Too' { 'Second Action' } Default { 'Default Action' } } Output: D...
Patterns can be matched based on values independent to the value being matched using if guards: // Let's imagine a simplistic web app with the following pages: enum Page { Login, Logout, About, Admin } // We are authenticated let is_authenticated = true; // But we aren't admins...
The for keyword is also used for conditional loops, traditionally while loops in other programming languages. package main import ( "fmt" ) func main() { i := 0 for i < 3 { // Will repeat if condition is true i++ fmt.Println(i) } } play ...
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...
Syntax {condition-to-evaluate} ? {statement-executed-on-true} : {statement-executed-on-false} As shown in the syntax, the Conditional Operator (also known as the Ternary Operator1) uses the ? (question mark) and : (colon) characters to enable a conditional expression of two possible outcomes. It c...
To avoid verbose null checking, the ?. operator has been introduced in the language. The old verbose syntax: If myObject IsNot Nothing AndAlso myObject.Value >= 10 Then Can be now replaced by the concise: If myObject?.Value >= 10 Then The ? operator is particularly powerful when you...
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...
type Person = { Age : int PassedDriversTest : bool } let someone = { Age = 19; PassedDriversTest = true } match someone.PassedDriversTest with | true when someone.Age >= 16 -> printfn "congrats" | true -> printfn "wait until you are 16" | false -> p...
6.0 Introduced in C# 6.0, the Null Conditional Operator ?. will immediately return null if the expression on its left-hand side evaluates to null, instead of throwing a NullReferenceException. If its left-hand side evaluates to a non-null value, it is treated just like a normal . operator. Note tha...
Adding a Conditional attribute from System.Diagnostics namespace to a method is a clean way to control which methods are called in your builds and which are not. #define EXAMPLE_A using System.Diagnostics; class Program { static void Main() { ExampleA(); // This method will ...
The intrinsic pack function packs an array into a vector, selecting elements based on a given mask. The function has two forms PACK(array, mask) PACK(array, mask, vector) (that is, the vector argument is optional). In both cases array is an array, and mask of logical type and conformable with...
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:...
if v:version >= 704 " Do something if Vim is the right version. endif if has('patch666') " Do something if Vim has the right patch-level. endif if has('feature') " Do something if Vim is built with 'feature'. endif See :help has-patch and :help feature-li...
Tag syntax Parameters AttributeRequiredTypeDefaultDescriptionconditiontruestringCondition that manages the loop. Cannot contain math symbols like <, > or =. Must use ColdFusion text implementations like less than, lt, greater than, gt, equals or eq. Final value of x is 5. <cfset x = 0 /...
The @if control directive evaluates a given expression and if it returns anything other than false, it processes its block of styles. Sass Example $test-variable: true !default =test-mixin @if $test-variable display: block @else display: none .test-selector +test-mixin ...

Page 2 of 7