21天学C++(四)表达式和语句

完整目录、平台简介、安装环境及版本:参考《21天学C++ 概览》

四、表达式和语句

4.1 语句

4.1.1 空白

x = a + b;
x		=     a   +b;
x      =  a
      +            b;
  • 语句会忽略制表符、空格、空行;
  • 空白会使程序更易读且易维护,但使用不当也会变得很糟糕。

4.1.2 块,也称符合语句{}

{
    temp = a;
    a = b;
    b = temp;
}
  • 建议1:大括号必须成对使用;
  • 建议2:语句必须以分号结束;
  • 建好:空白是程序易懂,但是使用时需注意

4.2 表达式

任何一个计算值的操作都可称为表达式

x = a + b;
y = x = a + b;

#include <iostream>
int main(){
   using std::cout;
   using std::endl;
  
   int a=0, b=0, x=0, y=35;
   cout << "a: " << a << " b: " << b;
   cout << " x: " << x << " y: " << y << endl;
   a = 9;
   b = 7;
   y = x = a+b;
   cout << "a: " << a << " b: " << b;
   cout << " x: " << x << " y: " << y << endl;
   return 0;
} 

执行结果如下:

a: 0 b: 0 x: 0 y: 35
a: 9 b: 7 x: 16 y: 16
请按任意键继续. . .

4.3 运算符

赋值运算符=
数学运算符+-*/%

#include <iostream>
 using namespace std;
int main(){
   unsigned int difference;
   unsigned int bigNumber = 100;
   unsigned int smallNumber = 50;

   difference = bigNumber - smallNumber;
   cout << "Difference is: " << difference;

   difference = smallNumber - bigNumber;
   cout << "\nNow difference is: " << difference <<endl;

   return 0;
} 

执行结果如下:

Difference is: 50
Now difference is: 4294967246
请按任意键继续. . .

4.4 自加与自减

前置与后置

	C++;//先取值,然后再加1
	++C;//值加1然后再取
	C += 1;
	C = C + 1;

	C--; //先取值,然后再减1
	--C; //值减1然后再取
	C -= 1;
	C = C – 1;
#include <iostream>
int main(){
   using std::cout;
   int myAge = 39;      // initialize two integers
   int yourAge = 39;
   cout << "I am: " << myAge << " years old.\n";
   cout << "You are: " << yourAge << " years old\n";
   myAge++;            // postfix increment
   ++yourAge;          // prefix increment
   cout << "One year passes...\n";
   cout << "I am: " << myAge << " years old.\n";
   cout << "You are: " << yourAge << " years old\n";
   cout << "Another year passes\n";
   cout << "I am: " << myAge++ << " years old.\n";
   cout << "You are: " << ++yourAge << " years old\n";
   cout << "Let's print it again.\n";
   cout << "I am: " << myAge << " years old.\n";
   cout << "You are: " << yourAge << " years old\n";
   return 0;
} 

执行结果如下:

I am: 39 years old.
You are: 39 years old
One year passes...
I am: 40 years old.
You are: 40 years old
Another year passes
I am: 40 years old.
You are: 41 years old
Let's print it again.
I am: 41 years old.
You are: 41 years old
请按任意键继续. . .

4.5 优先级

4.6 括号的嵌套

如下语句

ToalPersonSeconds = (
    ((NumMinutesToThink + NumMinutesToType) * 60) * 
    (PeopleInTheOffice + PeopleOnVaction));

优化

TotalMinutes = NumMinutesToThink + NumMinutesToType;
TotalSeconds = TotalMinutes * 60;
TotalPeople = PeopleInTheOffice + PeopleOnVaction;
ToalPersonSeconds = TotalMinutes * TotalPeople;

建议:使用括号可以改变优先级,但是不要使用太深的括号嵌套,否则难理解和维护。

4.7 真值bool

bool类型只有两个可能的值-真与假,占一个字节;

4.7.1 关系运算符

关系运算符共有6个,分别是:等于(==)、小于(<)、大于(>)、小于等于(<=)、大于等于(>=)、不等于(!);

  • 注1:关系运算符返回值是真(true)或假(false);
  • 注2:不要赋值运算符(=)和关系运算符(==)混淆。

4.8 if…else

4.8.1 if语句

if (bigNumber > smallNumber)
{
    bigNumber = smallNumber;
}//可以
if (bigNumber > smallNumber){
    bigNumber = smallNumber;
}//可以

if (bigNumber > smallNumber)
    bigNumber = smallNumber;//不推荐
if (bigNumber > smallNumber)
    {
    bigNumber = smallNumber;
    }//不推荐

4.8.2 if…else 语句

int val = 0;
      if (bigNumber > smallNumber)
{
    //当条件为真时
    val = bigNumber;
}
else
{
    //当条件为假时
    val = smallNumber;
}
std::cout<<val;

4.8.3 if 语句嵌套

 if (x >= 10)
      if (x > 100)
         std::cout << "More than 100, Thanks!\n";
 else
      std::cout << "Less than 10, Thanks!\n";
 if (x >= 10)
 {
      if (x > 100)
         std::cout << "More than 100, Thanks!\n";
 }
 else
      std::cout << "Less than 10, Thanks!\n";
 if (x >= 10)
 {
      if (x > 100)
         std::cout << "More than 100, Thanks!\n";
      else
         std::cout<<“between 10 and 100\n”;
 }
 else
      std::cout << "Less than 10, Thanks!\n";
 if (x > 100)
    std::cout << "More than 100, Thanks!\n";
    std::cout<<“less than 100\n”;

4.9 逻辑运算符

4.9.1 逻辑运算符

4.9.2 逻辑“与”,符号为&&

  • 只有两个表达式均为真时,整个表达式才为真,否则为假;
  • 计算过程中,如果表达式1为假,则不再计算表达式2,只有表达式1为真时,才会计算表达式2;

4.9.3 逻辑“或”,符号为||

  • 只有两个表达式均为假时,整个表达式才为假,否则为真;
  • 计算过程中,如果表达式1为真,则不再计算表达式2,只有表达式1为假时,才会计算表达式2;

4.9.4 逻辑“非”,符号为!

  • 如果表达式为假,则整个表达式为真;
  • 如果表达式为真,则整个表达式为假;

4.10 条件运算符(三目运算符)

(表达式1)?(表达式2):(表达式3)

#include <iostream>
int main()
{
	int valA = 7, valB = 5;
	int minVal = (valA > valB) ? valB : valA;
	std::cout<<minVal<<std::endl;
	return 0;
} 

执行结果如下:

5
请按任意键继续. . .

发表回复