
C++
以下是bool类型的使用示例:
1. 声明和初始化一个bool变量
C++
bool isTrue = true; //声明并初始化为true
bool isFalse(false); //声明并初始化为false
2. 使用bool类型进行条件判断
C++
bool isPositive = true;
int num = 10;
if(num > 0 && isPositive){
cout<<"num is positive"< }else{ cout<<"num is negative"< } 3. 使用bool类型进行循环语句控制 bool keepGoing = true; while(keepGoing){ cout<<"Do you want to continue? (Y/N)" char answer; cin>>answer; if(answer == 'N' || answer == 'n'){ keepGoing = false; }else{ cout<<"Keep going"< } } 以上是bool类型的使用示例,可以看到bool类型非常有用,可以帮助程序员方便地进行逻辑判断和控制流程。C++
Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号