天天看點

BP-2-2 Forms of Data

Chapter 02 Description of Simple Data

3. Forms of Data

In a program, data usually appears as constant or variable.
BP-2-2 Forms of Data

3.1 Constant

Constant refers to the data which remains the same or can’t be modified during the execution of the program.
1. literal
literal refers to the constant represented just by its value and doesn’t have a unique name.

Scientific Notation:

We use E(or e) as 10 when using scientific notation in C++, for example

4.5678E2 = 456.78

.

2. symbolic constant

Symbolic constant refers to the constants which has a unique name.

Using symbolic constant in tour code will make it more readable, more consistent and easier to maintain.

BP-2-2 Forms of Data

Definition of Symbolic Constant:

const <data-type> <constant-name> = <value>;
//or
#define <constant-name> <value>
//Example:
const double PI = 3.1415926;
#define PI 3.1415926
           

3.2 Variable

Each variable has a name, which is an identifier in C++, belongs to a specific and unique data type and has a value that corresponds to its data type. When running the program, each variable will get a space with a concrete address in the memory.

Definition of Variable:

<data-type> <name>; //or
<data-type> <name> = <initial-value>; //or
<data-type> <name> (<initial-value>);
//Example:
int a = 0; //a is an integer type variable, which can also be written like: int a(0);
int b = a + 1; //initial value can also be an expression
double x; //x is a double type variable with out initial value
           
  • Definition of multiple variables with the same data value can be defined in one line:
  • Remember that we must give the variable a value before we using the it. A variable without value makes no sense.

3.3 How to Input a Value?

#include <iostream> //include some declaration of basic operations
using namespace std; //C++ built-in functions are defined in the namespace std

int main(){
    int i;
    double d;
    cin >> i; //input an integer from keyboard and assign the number to the variable i
    cin >> d; //input a real number from keyboard and assign the number to the variable d
    ......
    return 0;
}
           
cin >> i;
cin >> j;
           
can also be written in one line like:

cin

use white-space character as a division for the input.