Tutorialshare

Full Version: Operators and Operands
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I will now cover the most used operators. I will tell about operands or the variable, constant, string, etc that is taking the operation.

First I'm sure everyone is familiar with math operators on numbers.
The math operators are + for addition, - for subtraction, * for multiply, / for divide, and %, modulus, for the remainder after division. Parentheses are an operator also and are explained later. I will show an example of each one being used so I will pull out the magic template for console programs, for use with dev-cpp of course but MS Visual C++ can use it with some modifications. Two additional operators are also available to to increment, ++, or decrement, --, a variable by one value for example a variable has a value of 1 is incremented to 2 or decremented to 0. Note: using ++ or -- before a variable increments or decrements before using it and after increments or decrements after using it. A sizeof operator is also available on all types of variables to tell how much memory a variable of that type takes up.

Code:
#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  //let's give n a value of 0.3
  int n = 0.3;

  //let's give a a value of 100
  int a = 100;

  //add a and 2 and store the value to b
  int b = a + 2;

  //add n and 1000 and store to o
  float o = n + 1000.2;

  //let's output the value of b
  //"\n" or endl can be used to output to a newline
  cout<<b<<"\n";

  //let's output the value of o
  cout<<o<<"\n";

  //let's give c a value of .1
  float c = .1;

  //subtract c from o
  float p = o - c;

  //let's output the value of p
  cout<<p<<"\n";

  //multiply p times .4
  p = p * .4;

  //let's output the value of p
  cout<<p<<"\n";

  //divide p by 3
  p = p / 3;

  //let's output the value of p
  cout<<p<<"\n";

  system("PAUSE");
  return 0;
}
Parentheses are used just like in math to indicate an operation you want done first. Like if you want to add two values before multiplying them by another.

Alright that takes care of the math operators. Now let's try some operators on strings.
Operators on strings are same as some math operators but be careful. Strings and numbers can not have an operator in between them unless the number is converted to a string. Other common operators are little more advanced such as braces, {}, and brackets, [].

That concludes the basic operators and operands of C++.
Reference URL's