THEORY


java

Break statement

Another point of interest is the break statement. Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block.
The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
Technically, the final break is not required because flow falls out of the switch statement. Using a break is recommended so that modifying the code is easier and less error prone. The default section handles all values that are not explicitly handled by one of the case sections.

switch statement break statement

java

Garbage collection

The Java interpreter knows exactly what objects and arrays it has allocated. It can also figure out which local variables refer to which objects and arrays, and which objects and arrays refer to which other objects and arrays. Thus, the interpreter is able to determine when an allocated object is no longer referred to by any other object or variable. When the interpreter finds such an object, it knows it can destroy the object safely and does so. The garbage collector can also detect and destroy cycles of objects that refer to each other, but are not referenced by any other active objects. Any such cycles are also reclaimed.

garbage collector gc

cplusplus

Implicit conversions

Implicit conversions are automatically performed when a value is copied to a compatible type. For example:
short a=2000;
int b;
b=a;
Here, the value of a is promoted from short to int without the need of any explicit operator. This is known as a standard conversion. Standard conversions affect fundamental data types, and allow the conversions between numerical types (short to int, int to float, double to int...), to or from bool, and some pointer conversions.
Converting to int from some smaller integer type, or to double from float is known as promotion, and is guaranteed to produce the exact same value in the destination type. Read more

cplusplus

No-promotion arithmetic types conversions

No-promotion conversions between arithmetic types may not always be able to represent the same value exactly:
  • negative integer to an unsigned type, the resulting value corresponds to its 2's complement bitwise representation (i.e., -1 becomes the largest value representable by the type, -2 the second largest, ...).
  • conv. from/to bool consider false equivalent to zero (for numeric types) and to null pointer (for pointer types); true is equivalent to all other values and is converted to the equivalent of 1.
  • floating-point type to an integer type, the value is truncated (the decimal part is removed). If the result lies outside the range of representable values by the type, the conversion causes undefined behavior.
  • if the conv. is between numeric types of the same kind, the conversion is valid, but the value is implementation-specific (may not be portable).
Read more

data-type-conversion types promotion

cplusplus

Types conversions

No-promotion conversions may imply a loss of precision, which the compiler can signal with a warning. This warning can be avoided with an explicit conversion.
For non-fundamental types, arrays and functions implicitly convert to pointers, and pointers in general allow the following conversions:
  • Null pointers can be converted to pointers of any type
  • Pointers to any type can be converted to void pointers.
  • Pointer upcast: pointers to a derived class can be converted to a pointer of an accessible and unambiguous base class, without modifying its const or volatile qualification.
Read more

implicit-conversion

htmlcss

Omitting the html, head, and body tags

Omitting the html, head, and body tags is certainly allowed by the HTML specs. The underlying reason is that browsers have always sought to be consistent with existing web pages, and the very early versions of HTML didn't define those elements. When HTML 2.0 first did, it was done in a way that the tags would be inferred when missing.
In the HTML5 standard, the <html> tag, the <body> tag, and the <head> tag can be omitted. The following code will validate as HTML5:
<!DOCTYPE html>
<title>Page Title</title>
<h1>This is a heading</h1>

<p>
This is a paragraph.
</p>

More details: Stackoverflow: Is it necessary to write HEAD, BODY and HTML tags?

body-tag html-tag head-tag omitting-tags

javascript

Switch statement syntax

The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.

Syntax

switch (expression) {
  case value1:
    //Statements executed when the result of expression matches value1
    [break;]
  case value2:
    //Statements executed when the result of expression matches value2
    [break;]
  ...
  case valueN:
    //Statements executed when the result of expression matches valueN
    [break;]
  default:
    //Statements executed when none of the values match the value of the expression
    [break;]
}
expression

An expression whose result is matched against each case clause.

case valueN

A case clause used to match against expression.


Read more: MDN: Switch

javascript

Switch statement

A switch statement first evaluates its expression. It then looks for the first case clause whose expression evaluates to the same value as the result of the input expression (using strict comparison, ===) and transfers control to that clause, executing the associated statements. (If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.) If no matching case clause is found, the program looks for the optional default clause, and if found, transfers control to that clause, executing the associated statements. If no default clause is found, the program continues execution at the statement following the end of switch. By convention, the default clause is the last clause, but it does not need to be so.
Read more: MDN: Switch

switch-statement strict-comparison

javascript

Break in switch statement

The Switch Statement

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

What happens if I forgot a break?

If you forget a break then script will run from the case where criteria is met, and will run the case after that regardless if criteria was met. See example here:
var foo = 0;
switch (foo) {
  case -1:
    console.log('negative 1');
    break;
  case 0: 
    console.log(0);
    // NOTE: the forgotten break 
  case 1:     
    console.log(1);
    break; //  break 
  case 2:
    console.log(2);
    break;
}
Console output:
01

Read more: MDN: Switch

break-keyword

javascript

Multi-case - single operation

Multi-case - single operation

This method takes advantage of the fact that if there is no break below a case statement it will continue to execute the next case statement regardless if the case meets the criteria.
This is an example of a single operation sequential switch statement, where four different values perform exactly the same.
var Animal = 'Giraffe';
switch (Animal) {
  case 'Cow':
  case 'Giraffe':
  case 'Dog':
  case 'Pig':
    console.log('This animal will go on Noah\'s Ark.');
    break;
  case 'Dinosaur':
  default:
    console.log('This animal will not.');
}
Read more: Stackoverflow: Switch statement multiple cases in JavaScript

multi-case-single-operation multi-case

Loading rules...