Tutorial by Examples: d

This program will output "Hello World!" #import <Foundation/Foundation.h> int main(int argc, char * argv[]) { NSLog(@"Hello World!"); } #import is a pre-processor directive, which indicates we want to import or include the information from that file into the pr...
The map function is the simplest one among Python built-ins used for functional programming. map() applies a specified function to each element in an iterable: names = ['Fred', 'Wilma', 'Barney'] Python 3.x3.0 map(len, names) # map in Python 3.x is a class; its instances are iterable # Out: &...
Variables can be accessed via dynamic variable names. The name of a variable can be stored in another variable, allowing it to be accessed dynamically. Such variables are known as variable variables. To turn a variable into a variable variable, you put an extra $ put in front of your variable. $va...
The addition operator (+) adds numbers. var a = 9, b = 3, c = a + b; c will now be 12 This operand can also be used multiple times in a single assignment: var a = 9, b = 3, c = 8, d = a + b + c; d will now be 20. Both operands are converted to primitive types. ...
The division operator (/) perform arithmetic division on numbers (literals or variables). console.log(15 / 3); // 5 console.log(15 / 4); // 3.75
The remainder / modulus operator (%) returns the remainder after (integer) division. console.log( 42 % 10); // 2 console.log( 42 % -10); // 2 console.log(-42 % 10); // -2 console.log(-42 % -10); // -2 console.log(-40 % 10); // -0 console.log( 40 % 10); // 0 This operator returns th...
The decrement operator (--) decrements numbers by one. If used as a postfix to n, the operator returns the current n and then assigns the decremented the value. If used as a prefix to n, the operator assigns the decremented n and then returns the changed value. var a = 5, // 5 b = a...
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 use construct is used to import variables into the anonymous function's scope: $divisor = 2332; $myfunction = function($number) use ($divisor) { return $number / $divisor; }; echo $myfunction(81620); //Outputs 35 Variables can also be imported by reference: $collection = []; $a...
print_r() - Outputting Arrays and Objects for debugging print_r will output a human readable format of an array or object. You may have a variable that is an array or object. Trying to output it with an echo will throw the error: Notice: Array to string conversion. You can instead use the print_r...
The most widely used language construct to print output in PHP is echo: echo "Hello, World!\n"; Alternatively, you can also use print: print "Hello, World!\n"; Both statements perform the same function, with minor differences: echo has a void return, whereas print retu...
This program prints Hello World! to the standard output stream: #include <iostream> int main() { std::cout << "Hello World!" << std::endl; } See it live on Coliru. Analysis Let's examine each part of this code in detail: #include <iostream> is a...
Variables can be incremented or decremented by 1 using the ++ and -- operators, respectively. When the ++ and -- operators follow variables, they are called post-increment and post-decrement respectively. int a = 10; a++; // a now equals 11 a--; // a now equals 10 again When the ++ and -- ope...
dictionary = {"Hello": 1234, "World": 5678} print(dictionary["Hello"]) The above code will print 1234. The string "Hello" in this example is called a key. It is used to lookup a value in the dict by placing the key in square brackets. The number 1234 is ...
Arguments are passed to the program in a manner similar to most C-style languages. $argc is an integer containing the number of arguments including the program name, and $argv is an array containing arguments to the program. The first element of $argv is the name of the program. #!/usr/bin/php p...
When run from the CLI, the constants STDIN, STDOUT, and STDERR are predefined. These constants are file handles, and can be considered equivalent to the results of running the following commands: STDIN = fopen("php://stdin", "r"); STDOUT = fopen("php://stdout", "...
The exit construct can be used to pass a return code to the executing environment. #!/usr/bin/php if ($argv[1] === "bad") { exit(1); } else { exit(0); } By default an exit code of 0 will be returned if none is provided, i.e. exit is the same as exit(0). As exit is not a ...
The dict() constructor can be used to create dictionaries from keyword arguments, or from a single iterable of key-value pairs, or from a single dictionary and keyword arguments. dict(a=1, b=2, c=3) # {'a': 1, 'b': 2, 'c': 3} dict([('d', 4), ('e', 5), ('f', 6)]) # {'d': 4, 'e': ...
To create a simple C program which prints "Hello, World" on the screen, use a text editor to create a new file (e.g. hello.c — the file extension must be .c) containing the following source code: hello.c #include <stdio.h> int main(void) { puts("Hello, World")...
Alternatively, you can use the Interactive Ruby Shell (IRB) to immediately execute the Ruby statements you previously wrote in the Ruby file. Start an IRB session by typing: $ irb Then enter the following command: puts "Hello World" This results in the following console output (in...

Page 14 of 691