第二节 流控制

1. 关系运算符

符号 意义 运算结果
> 大于 5 > 5 = false
>= 大于等于 5 >=5 = true
< 小于 5 < 5 = false
<= 小于等于 5 <= 5 = true
== 等于 5 == 5 = true
!= 不等于 5 != 5 = false

注意:== 才是进行等于判断, = 是进行赋值操作,赋值操作的结果永远都是 true。

2. 逻辑运算符

符号 意义 运算结果
&& T && T = T; T && F = F; F && F =F
|| T || T = T; T || F = F; F || F = F
!T = F; !F = T

注意: 在C++中,true和1(或非0)相同,false和0相同

3. 条件判断

条件判断,主要用来验证表达式的结果,条件判断的结构如下

复制代码
if(conditon)
{
    // todo
}
else if(condition)
{
    // todo
}
else
{
   // todo
}
复制代码

对于条件判断而言,只要列表中的条件有一个得到满足,其他的判断都不再执行

如果给出的所有条件都不满足,则执行else中的代码

复制代码
#include <iostream>

using namespace std;

int main() {

  int x = 6;
  int y = 2;

  if(x > y)

    cout << "x is greater than y\n";

  else if(y > x)

    cout << "y is greater than x\n";

  else

    cout << "x and y are equal\n";

  return 0;

 }
复制代码

注意:if...else结构后面是没有分号的,如果if...else中的代码只有一行可以省略{ }

复制代码
// 输入两个数字,输出较大的数字
#include <iostream>
using namespace std;

int main()
{
    int a, b;
    cin >> a;
    cin >> b;
    if(a>b)
        cout << a;
    else
        cout << b;
    returen 0;
}
复制代码
复制代码
// 输入一个ASCII字符,判断是字母、数字还是其他符号
#include <iostream>
using namespace std;

int main()
{
    int character;
    cin >> character;
    
    if(character >= 48 && character <= 57)
        cout << "输入的是数字";
    else if((character >= 65 && character <= 90) || (character >= 97 && character <= 122))
        cout << "输入的是字母";
    else
        cout << "输入的是其他符号";
    returen 0;

if(5 > 0)
     cout << "真" << endl;
   else
     cout << "假" << endl;

   cout << (5 > 0) ? "真" : "假" << endl; }
复制代码

4. 循环控制

当在满足一定条件下,需要重复执行操作时,就可以使用循环控制语句。

while(condition){ // todo }

do { // todo } while(contidion)
复制代码
#include <iostream>

using namespace std;

int main() {
    int x = 0;

    while(x < 10){
      x = x + 1;
      cout << "x is " << x << "\n";
    }
    return 0;
}
复制代码
for(initialization; condition; incrementation){ // todo }
复制代码
#include <iostream>

using namespace std;

int main() {

  for(int x = 0; x < 10; x = x + 1)
    cout << x << "\n";

  return 0;

}
复制代码
复制代码
 #include <iostream>

using namespace std;

int main() {

    int x = 0;

    // 注意此时for循环的初始化操作,被放到for循环的外面
    for(; x < 10; x = x + 1)

        cout << x << "\n";
    return 0;
 }
复制代码
复制代码
//for循环与while循环可以相互转化
#include <iostream>

using namespace std;

int main() {

  for(int x = 0; x < 10; x = x + 1)

     cout << x << "\n";

  return 0;
}


#include <iostream>

using namespace std;

int main() {

  int x = 0;

  while(x < 10){
    cout << x << "\n";
    x = x + 1;
  }
  return 0;
}
复制代码
复制代码
for(int i = 0; i < 10; i++)
{
   if(i % 2 == 0)
   {
       continue;
   }
   cout << i << endl;  
}

for(int i = 0; i < 10; i++)
{
   if(i % 2 = 0)
   {
       break;
   }
   cout << i << endl;  
}
复制代码

 

5. GOTO 跳转

不推荐使用

复制代码
#include <iostream>
using namespace std ;

// 声明整形变量a,此时变量a还未被分配内存空间
//extern int a;

int main () {
    // 两者区别在于,goto语句不会自动释放局部变量的内存空间
    int a = 0;
    reset:
    a = 0;
    a = 10;
    cout << a << endl;
    goto reset;

    while(1)
    {
        int a = 0;
        a = 10;

    }
    return 0;
}
复制代码

 

6. continue, break, return, exit()

如何中断一个循环?

复制代码
#include <iostream>
using namespace std ;

int main () {
    // continue 结束本次逻辑循环,继续下次逻辑循环
    for(int a = 0; a < 10; a++)
    {
        if(a % 2 == 0)
            continue;
        cout << a << " " << endl;
    }
    cout << "END" << endl;
    
    // break 结束全部逻辑循环判断
    for(int a = 0; a < 10; a++)
    {
        if(a % 2 == 0)
            break;
        cout << a << " " << endl;
    }
    cout << "END" << endl;
    
    // return 结束当前函数
    for(int a = 0; a < 10; a++)
    {
        if(a % 2 == 0)
            return 0;
        cout << a << " " << endl;
    }
    cout << "END" << endl;
    
    // exit() 结束当前程序
    for(int a = 0; a < 10; a++)
    {
        if(a % 2 == 0)
            exit(0);
        cout << a << " " << endl;
    }
    cout << "END" << endl;
}
复制代码

 

除了if...else if...else多条件判断,还可以使用switch....case结构进行多条件判断,区别是switch...case中的全部条件判断都会被执行

复制代码
#include <iostream>

using namespace std;

int main() {

    int x = 6;

    switch(x){
        case 1:
            cout << "x is 1\n";
            break;
        case 2:
        case 3:
            cout << "x is 2 or 3";
            break;
        default:
            cout << "x is not 1, 2, or 3";
    }

    return 0;
}
复制代码

7. 练习

复制代码
// 在屏幕中打印出从1-99中所有的偶数
#include <iostream>

using namespace std;

int main() {

  for(int x = 0; x < 99; x = x + 1)
    if(x%2 == 0)
        cout << x << endl;

  return 0;

}   
复制代码
复制代码
//利用do…while结构创建屏幕菜单,1. 打印姓名,2. 打印学号,3. 退出程序
#include <iostream>

using namespace std;

int main() {

  int userInput = 0;

  do {
    cout << "请输入选择菜单编号:" << endl;
    cout << "1. 打印姓名" << endl;
    cout << "2. 打印学号" << endl;
    cout << "3. 退出程序" << endl;
    cin >> userInput;
    switch(x){
        case 1:
            cout << "庞兴庆";
            break;
        case 2:
            cout << "学号:009126";
            break;
        default:
            cout << "输入不是 1, 2, or 3";
    }while(userInput != 3)
  }
  return 0;
}
复制代码
复制代码
//使用双重循环在屏幕打印九九乘法表
#include <iostream>

using namespace std;

int main() {

    for(int i = 1; i < 10; i++)
    {
        for(int j = 1; j < 10; j++)
        {
            cout << i << " * " << j << " = " << i * j << " ";
        }
        cout << endl;
    } 
    return 0;
}
复制代码

6. 作业

作业1:根据用输入的三个角度,判断这三个角度能够构成三角形

作业2:在屏幕中输出小于100的奇数

作业3:根据用户输入的数字,判断数字的类型(整数或小数)

posted @   庞兴庆  阅读(244)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示