Showing posts with label boolean. Show all posts
Showing posts with label boolean. Show all posts

Monday, November 7, 2011

Operators

An operator is a symbol, which can be used to produce a new value from one or more input values. In programming there are seven main types:

Assignment
  • The assignment operator (=) is used to assign a value to a variable. The value is on the right and the variable is on the left side of the assignment operator.
    • int letters = 26;

Arithmetic
Examples in Python
  • Arithmetic operators are used to perform calculations. In general, these are used the same as in mathematics.
  • The addition operator (+) is used to add two or more inputs.
    • sum = 3 + 5
    • print sum            # sum should equal 8
  • The subtraction operator (-) is used to subtract one or more inputs from another.
    • difference = 5 - 3
    • print difference   # difference should equal 2
  • The multiplication operator (*) is used to multiply one or more inputs.
    • product = 5 * 3
    • print product       # product should equal 15
  • The division operator (/) is used to divide one of more inputs from another.
    • quotient = 5 / 3
    • print quotient      # quotient should equal 1 because both numbers are integers. The result would be 1.6667 if floats were used (declared in Python by writing either of the numbers with a decimal: 5.0 / 3)
  • The remainder operator (%) or modulo is used to find the remainder of the division of inputs.
    • remainder = 5 % 3
    • print remainder    # remainder should equal 2

Compound Assignment
Examples in Python.
  • Compound assignment operators are shortcuts that allow you to perform a calculation and immediately assign the resulting value to a variable.
  • So instead of:
    • sum = 3 + 5
    • sum = sum + 2
  • You can do:
    • sum = 3 + 5
    • sum += 2
  • There is a compound operator for all of the normal arithmetic operators, including:
    • addition: +=
    • subtraction: -=
    • multiplication: *=
    • division: /=
    • modulo: %=

Unary
Examples in C++.
  • In C++ and Java there are even easier compound assignment operators for addition (++) and subtraction (--). These perform calculations and assign the result to variables or vice versa, depending on where they are placed:
          int x, y;
          x = 0;
          y = x++;                            // the unary addition operator is placed after the variable name
          cout << x << ", ";               // assignment to y occurs first, addition to x occurs second
          cout << y << endl;             // 0, 1 should print to the screen
          
         int x, y;
         x = 0;
         y = ++ x;                           // the unary addition operator is placed before the variable name
         cout << x << ", ";               // addition to x occurs first, assignment to y occurs second
         cout << y << endl;              // 1, 1 should print to the screen


Equality and Relational
Examples in Python.
  • These operators are used to determine the relationship between two operands.
  • Equals (==) determines whether the operands are equal
    • print 3 == 5               # should print 0 (for false)
    • print 3 == 3               # should print 1 (for true)
  • Does not equal (!=) determines whether the operands are not equal
    • print 3 != 5                # should print 1 (for true)
    • print 3 != 3                # should print 0 (for false)
  • Greater than (>) determines whether the first operand is greater than the second
    • print 3 > 5                 # should print 0 (for false)
    • print 5 > 3                 # should print 1 (for true)
  • Less than (<) determines whether the first operand is less than the second
    • print 3 < 5                # should print 1 (for true)
    • print 5 < 3                # should print 0 (for false)
  • Greater than or equal (>=) determines whether the first operand is greater than or equal to the second
    • print 3 >= 5              # should print 0 (for false)
    • print 3 >= 3              # should print 1 (for true)
  • Less than of equal (<=) determines whether the first operand is less than or equal to the second
    • print 3 <= 3              # should print 1 (for true)
    • print 5 <= 3              # should print 0 (for false)

Logical
Examples in C++.
  • Logical operators (related to Boolean logic) are used to evaluate the inverse of an expression or two expressions to obtain one result.
  • The NOT operator (!) is used to determine the inverse of an expression.
           bool a = true;
           result = !a;
           cout << result << endl;      // should print 0 (for false)

           bool a = false;
           result = !a;
           cout << result << endl;       // should print 1 (for true)
  • The AND operator (&&) is used to determine whether both expressions are true. 
           bool, a, b;
           a = true;
           b = true;
           result = a && b
           cout << result << endl;      // should print 1 (for true)

           a = true;
           b = false;
           result = a && b;
           cout << result << endl;       // should print 0 (for false)
  • The OR operator (||) is used to determine whether either expression is true.
           bool a, b;
           a = true;
           b = false;
           result = a || b;
           cout << result << endl;        // should print 1 (for true)

           a = false;
           b = false;
           result = a || b;
           cout << result << endl;         // should print 0 (for false)
  • note that these symbols are appropriate for C++ and Java. Python also supports logical operations, but !, &&, and || are replaced by the words "not," "and," and "or," respectively.

Bitwise
Examples are just binary numbers. They are NOT written in a language's syntax.
  • Underneath the surface, all computations in programming are done in binary. Occasionally it may be more useful to work directly in binary, so many languages support bitwise operations. There is some overlap between logical operations and bitwise operations, so these operators should look familiar.
  • The bitwise complement operator (~) is equivalent to the logical NOT. It is used to invert all of the bits of a binary number. (returns 1 if 0, returns 0 if 1)

    ~01001001
    ---------------
      10110110
  • The bitwise AND operator (&) is equivalent to the logical AND. It is used to determine whether both bits are true. (returns 1 only if both bits are 1, 0 otherwise)

        01110011
    & 10010110
    -----------------
        00010010
  • The bitwise OR operator (|) is equivalent to the logical OR. It is used to determine whether either bit is true. (returns 0 only if both bits are 0, 1 otherwise)

      11010110
    | 00100011
    ---------------
      11110111
  • The bitwise XOR (exclusive or) operator (^) is used to determine whether both bits are equal. (returns 1 if both 1 or both 0, returns 0 otherwise)

       11101100
    ^ 01010110
    ----------------
       01000101
  • The bitwise left shift operator (<<) is used to shift the bits a specified number left. Zeros are placed in the empty spaces created on the right side of the binary number. (numbers shifted out of the number in green, numbers that remain in blue, placeholder zeros in red)

    11010100 << 2
    --------------------
    01010000
  • The bitwise right shift operator (>>) is used to shift the bits a specified number right. Zeros are placed in the empty spaces created on the left side of the binary number. (numbers shifted out of the number in green, numbers that remain in blue, placeholder zeros in red)

    11010100 >> 2
    --------------------
    00110101

Saturday, November 5, 2011

Variables and Data Types

Variables in programming have five attributes:

  1. Name: A variable's name is linked to information. A descriptive name makes code easier to understand by other programmer's or even by you in the future.
  2. Type: The kind of information that is linked to the variable name. (See discussion of data types below)
  3. Value: The actual information stored.
  4. Scope: Where in a program a variable can be used. (to be discussed with functions)
  5. Lifetime: When the variable exists during each run of the program. (to be discussed with functions)

Python

factor1 = 3
factor2 = 5
quotient = factor1 * factor2

OR

quotient = 3 *5


C++

int factor1, factor2, quotient;
factor1 = 3;
factor2 = 5;
quotient = factor1 * factor2;

OR

int factor1 = 3;
int factor2 = 5;
int quotient = factor1 * factor2;

OR

int quotient = 3 * 5;


Java

int factor1, factor2, quotient;
factor1 = 3;
factor2 = 5;
quotient = factor1 * factor2;

OR

int factor1 = 3;
int factor2 = 5;
int quotient = factor1 * factor2;

OR

int quotient = 3 * 5;

  • When doing math with any of the languages you can either define variables for each number and then define another variable, which uses those variables to actually do the calculations, or you can define a variable by doing the calculations immediately.
  • In Python, type declarations are unnecessary. Unfortunately, this makes it impossible to declare a variable before you define it.
  • In both C++ and Java there are a few ways declare and define variables:
    • you can declare all of your variables first and then define them individually before doing the calculations.
    • you can define each variable as you declare it before doing the calculations.
    • or you can define a variable at declaration with the calculations.

Data Types:
since variables don't need to be declared in Python, all example declarations are applicable to only C++ and Java.

Every programming language has built-in data types that allow you to perform various tasks. Primitive data types are found in most languages. These include:

Numbers
  • Integer: Integers are whole numbers without decimal places. They are usually a sufficient size for whatever data manipulation needs to be accomplished, but they can lead to integer overflow in some situations.
    • int twenty = 20;
  • Short (integer): Shorts are the smallest integer variation in most languages. They are used when it is necessary to save memory
    • short two = 2;
  • Long (integer): The long is a longer variation of the integer. They are used when an integer would lead to overflow.
    • long two_hundred = 200L;
  • Float: Floats are decimal numbers that can contain approximately 7 digits. 
    • float decimal_two = 2.0341f;
  • Double: Doubles are a double precision float. They can contain approximately 15 digits (rather than only 7).
    • double pi = 3.14159265;
Other
  • Boolean: Boolean values are either true or false. They can be used to track conditions (whether the condition is true or false).
    • bool contains_letters = 1;
    • 1 = true, 0 = false
  • Character: Characters are just a single character.
    • char a_letter = 'A';
    • characters must be declared in single quotes
There are other data types as well, but they vary by language. For example, in Python strings (a sequence of characters) are an included data type. In Java, the byte (a variation of the integer, which is even smaller than a short) is a primitive data type.