oxygen adsorbates on Pt(111) surface


C/C++ Tutorials - Lesson 2
Variables

Programs that only output pre-programmed messages are not very useful. Ideally a program should be able to take input from a user, compute something useful from the input, and output the results for the user. In order to accomplish this we need a method to store and process information. This information is stored in variables.

#include <iostream>

using namespace std;

int main()
{
// declaring integer variables
           int x, y, z;
// setting x = 5, y = 6, and then multiplying x and y and storing the value in z
           x = 5;
           y = 6;
           z = x*y;
// outputing z to the command prompt
           cout << z << endl;
           return 0;
}

Types of Variables

In C and C++ each variable must be explicitly declared. In this example you are multiplying two integers, stored in x and y, and storing the result in z. Take a look at this cplusplus page about types of variables as well as asciitable.com about the ASCII table, a table of integer values that represent characters. cplusplus has good information, though their introductory information may be a bit daunting for someone new to programming.

In addition to multiplication C++ supports the following basic arthmetic operators:
Symbol Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
The order of operations is obeyed:
Symbol(s) Operation(s) Order
( ) Parentheses 1st
*, /, or % Multiplication, division, or Modulus 2nd
+ or - Addition or subtraction 3rd

For a deeper understanding, write a program to add 1 to 3.5 and then multiply by 2. Hint: you will need to use floating point numbers and parentheses.


© 2013 Dave Schmidt

Web design: Dave Schmidt