Tutorial by Examples: conditional

The ?. operator is syntactic sugar to avoid verbose null checks. It's also known as the Safe navigation operator. Class used in the following example: public class Person { public int Age { get; set; } public string Name { get; set; } public Person Spouse { get; set; } } If a...
Similarly to the ?. operator, the null-conditional index operator checks for null values when indexing into a collection that may be null. string item = collection?[index]; is syntactic sugar for string item = null; if(collection != null) { item = collection[index]; }
var zipcode = myEmployee?.Address?.ZipCode; //returns null if the left operand is null. //the above is the equivalent of: var zipcode = (string)null; if (myEmployee != null && myEmployee.Address != null) zipcode = myEmployee.Address.ZipCode;
var letters = null; char? letter = letters?[1]; Console.WriteLine("Second Letter is {0}",letter); //in the above example rather than throwing an error because letters is null //letter is assigned the value null
Given a list comprehension you can append one or more if conditions to filter values. [<expression> for <element> in <iterable> if <condition>] For each <element> in <iterable>; if <condition> evaluates to True, add <expression> (usually a function...
The system header TargetConditionals.h defines several macros which you can use from C and Objective-C to determine which platform you're using. #import <TargetConditionals.h> // imported automatically with Foundation - (void)doSomethingPlatformSpecific { #if TARGET_OS_IOS // code t...
Consider the below list comprehension: >>> def f(x): ... import time ... time.sleep(.1) # Simulate expensive function ... return x**2 >>> [f(x) for x in range(1000) if f(x) > 10] [16, 25, 36, ...] This results in two calls to f(x) for 1,000 values of...
To conditionally include a block of code, the preprocessor has several directives (e.g #if, #ifdef, #else, #endif, etc). /* Defines a conditional `printf` macro, which only prints if `DEBUG` * has been defined */ #ifdef DEBUG #define DLOG(x) (printf(x)) #else #define DLOG(x) #endif Norm...
Conditional comments can be used to customize code for different versions of Microsoft Internet Explorer. For example, different HTML classes, script tags, or stylesheets can be provided. Conditional comments are supported in Internet Explorer versions 5 through 9. Older and newer Internet Explorer ...
An if statement checks whether a Bool condition is true: let num = 10 if num == 10 { // Code inside this block only executes if the condition was true. print("num is 10") } let condition = num == 10 // condition's type is Bool if condition { print("num is 10&...
Evaluates its first operand, and, if the resulting value is not equal to zero, evaluates its second operand. Otherwise, it evaluates its third operand, as shown in the following example: a = b ? c : d; is equivalent to: if (b) a = c; else a = d; This pseudo-code represents it : c...
When the following is compiled, it will return a different value depending on which directives are defined. // Compile with /d:A or /d:B to see the difference string SomeFunction() { #if A return "A"; #elif B return "B"; #else return "C"; #endif ...
The ternary operator is used for inline conditional expressions. It is best used in simple, concise operations that are easily read. The order of the arguments is different from many other languages (such as C, Ruby, Java, etc.), which may lead to bugs when people unfamiliar with Python's "s...
Payments Table CustomerPayment_typeAmountPeterCredit100PeterCredit300JohnCredit1000JohnDebit500 select customer, sum(case when payment_type = 'credit' then amount else 0 end) as credit, sum(case when payment_type = 'debit' then amount else 0 end) as debit from payments group by ...
In a nutshell, conditional pre-processing logic is about making code-logic available or unavailable for compilation using macro definitions. Three prominent use-cases are: different app profiles (e.g. debug, release, testing, optimised) that can be candidates of the same app (e.g. with extra log...
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 ...
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...
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...

Page 1 of 4