C++ Arithmetic Expressions

C++ Arithmetic Expressions

This topic can be explained generally and by specific datatypes( ex: doing things with integers is different from doing things with floating point numbers).

Generally there are three terms that need to be defined before you can do anything.

  • Expression: Any term or combo of terms, with terms being in this case variables or literals, with them being able to be combined with operators.
  • Literal: A specific value
    • ex: 2 is a int literal and “This” is a string literal
  • Operator: a symbol that does a built in operation
    • list of operators
      • + addition
      • – subtraction (and negative)
      • * multiplication
      • / division
      • % (only works with integers)
    • There are also compound operators, bitwise operators, conditional operators logical operators, most of which will be explained elsewhere
      • compound operators
        • ex: +=, -=, %=, . . .
          • basically its one of the arithmetic operates and the ‘=’ sign
          • This does combines an operation and an assignment, so it basically means that if you were changing the value of a variable you can shorten the equation
            • ex: Instead of ‘b=b+5;’ you can do ‘b+=5;’

The expressions in order to be read correctly by the computer must follow the precedence rules Which are:

  1. ( ) parenthesis
  2. * / %
  3. unary ( operators that only need one term to be used )
    • ex: – when used to denote negative
  4. + –
  5. right to left

It is good practice to always use parenthesis so its easier for you and other programmers to read. In the same vein you should put spaces around operators unless unary so its easier to read.

Integers

For doing Arithmetic expressions with integers, the first thing you need to know is what are the integer types. They are as their name applies integers so when represented numerically they do not have a decimal point.

A list of these are:

  • int
    • (this is for reference, either 32 or 64 bits unless your using something really old, in which case it might be 16, but that’s unlikely)
  • long int
    • ( it doubles the number of bits in an int )
  • short int
    • (its half the number of bites in an int )
  • bool
    • ( this is true, false basically but false = 0, and true =1 (or well anything but 0 is typically going to convert to true))
  • char
    • ( all characters have an integer representation, which can be found on an ASCII table)
  • hex
    • ( this is where you use a base 16 number system to represent a number)
  • octal
    • (this is where you use a base 8 number system to represent a number)
  • bit
    • ( this is binary)
  • etc
    • Generally you can modify a datatype to only have positive integers in it using the unsigned keyword in front of the datatype, (most integer datatypes are signed unless otherwise specified)
      • ex: unsigned int
      • what happens if you set it to a negative number?
        • Well it overflows the datatype so it prints out the maximum number that can be held in it, since it basically flips all the zeros into ones in the computers binary representation(In case you don’t know computers actually work in binary, binary is just annoying to read so we made it translate it into something comprehensible for us)

When using integer literals ( as in actual numbers not characters) the default datatype is int. To change this you add a suffix to the end if the integer or you type cast it. For example the suffix L on an integer denotes a long int, which would look like, 135L.

Now in C++ you can divide by zero if and only if its floating point division not integer division. In integer division the quotient will always be a integer, so if the number you are dividing isn’t divisible it will round down, since it basically ignores the remainder. You get the remainder by using modulo (which is why modulo can only be used with integers).

Floating Point

These datatypes are basically a real number containing a decimal point. Their are three datatypes which in order of storage space are float (4 bytes), double (8 bytes) and long double ( 16 bytes).

Now you can divide by zero with floating point numbers. When you do so, a positive number / 0 = inf, a negative number /0 = -inf and 0/0 = NaN (or not a number).

The default for a floating point literal is a double, if you want it to be a float you use the suffix f.

Some other things to note are that very large and very small floating point numbers might be printed using scientific notation and it is a good practice to always have a number before the decimal even if its a ‘0’ so its easier to read.

I will probably go more in depth on integer and/or floating point types noting how to use them or how you should use them in a different post that I will link here.

Naming Conventions

Naming Conventions

The basic naming conventions apply to most if not all languages( I am mainly basing this off C++ as its what I have been working with recently, but from what I remember these also generally apply to Java, C, and C#), although the standard(if there is one) may vary depending on the language. The reason to follow naming conventions is to make your code more readable.

The basic rules for this are:

  • Use meaningful names
    • Try to be short, but still easily readable
      • a good rule of thumb is for it to be at least two words, but no more than 20-30 characters long.
        • also if you use abreviations make sure its a common abreviation
      • ex: call something studentNumber or studentNum not num
  • Use different styles for different kinds of identifiers
    • ex: use camal case for variables, but use pascals case for function names, and snakecase for class names
  • For global constant varibles use all caps with underscors between words
    • global as in declared outside of any function, which means it applies to the whole module
  • BE CONSISTANT
    • choose a convention and stick with it throughout the code, don’t randomly switch what you use for function names and variable names or something

There are 4 or so naming conventions:

  • camelCase:
    • the first letter of everyword but the first word is capitalized
  • PascalCase:
    • the first letter of every word is capitalized including the first word
  • snake_case:
    • no capital letters
    • put a underscore between differwnt words
  • sHungarianNotation:
    • in this notation you start the identifier with an abbreviation that tells you what the datatype or the purpose of the variable is
      • in this case sHungarianNotation would be a string as ‘s’ is a common abreviation of string
        • another example would be arrStudentNames, which would be an array
    • after the abreviation should be a descriptor of the variable
    • this also follows CamalCase, in how its capitalization

One other thing to note is that it is generally better for code to be over commented than under commented, so feel free to comment the purpose of the variable, especially if it’s something very specific.

Some typical cases where people don’t follow naming conventions is when it’s a temporary variable like the index for a loop, in which case people generally use something along the lines of i, or if i is already being used some other random letter like j or k. People may also commonly just name temporary variables temp.

Keep in mind that naming conventions are not enforced by the compiler, they simply are good practice in terms of making sure other programmers can actually read your code when necessary.

C++ Variables

C++ Variables

A variable is a named item used to hold a value. This can be an actual value or an address that points to where a value is stored. If the variable holds an address that points to a place a value is stored it is a pointer.

All variables need to be assigned a value to work, on occasion a value will automatically be assigned to the variable, but that will depend on the variable. You assign a value with the ‘=’ sign. You should be reading this symbol in code as ‘assigned’, not ‘equal to’. (When it is by itself, ‘==’ is checking for equality).

A variable declaration is just where you are declaring that a variable of datatype exists. For example:

    datatype varName;

In the above example, while a variable of datatype has been made it has not yet been assigned a value, you will also notice that it is using the camel case naming convention, which will be explained in a different post.

A expression is a number, a variable, or a calculation. Simply put it’s something that will simplify to a value. An assignment statement is when a variable is assigned a value, whether it already had a value stored in it or not. As such an assignment statement has a left value and a right value. The left value is the variable, it’s the location at which you will be storing the right value. The right value is an expression it is the value that will be stored in the location on the left side. When the right value datatype doesn’t match the left value datatype there can be errors, either in terms of something like rounding or an actual compiler error. An example of an assignment statement is:

    varName = expression;

You can also initialize a variable. This is when you declare a variable and assign it a value in the same line. For example:

    datatype varName = expression;

The rules for a variable name (also known as an identifier) are:

  • Must be a sequance of letters, underscores and numbers
  • Must start with a letter or a underscore
    • This means that it cant start with a number
  • They can’t be a reserved word
    • a identifier can only be declared once in the same scope
  • Identifiers are case sensative

A scope is where a variable is defined. For example, if you declare a variable at the beginning of a function the scope of that variable is that entire function, while if you were to declare it in an if statement or a while loop it would only be defined in the while loop or if statement.

Related posts:(coming soon)