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.
在这里插入图片描述

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.
在这里插入图片描述

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:

    int a = 0, b = a + 1;
    
  • 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 >> i >> j;

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值