c++while控制语句
2024-10-07 15:28:30
while语句结构:while(condition){
statement;
}
condition 表示返回值是true or false 如果返回的一直是true则statement语句则一直执行下去,直至condition为false;
#include<iostream>
int main(){
int sum=,val=;
//只要val小于10(condition)则一直执行执行while循环内的语句
while(val<=){
sum+=val;
++val;//val 加1 add 1 to val 等于val=val +1;
}
std::cout<<"sum of 1 to 10 is "<<sum<<std::endl;
return ;
}
练习:计算50到100的和;
//write a program that uses a while to sum the numbers from 50 to 100
#include<iostream>
using namespace std;
int main(){
int sum=,val=;
while(val<=){
sum+=val;
++val;
}
cout<<"sum of 50 to 100 is "<<sum<<endl;
return ;
}
练习:用户输入两个整数,输出者两个整数之间的所有整数
/*write a program that prompts the user for two intergers,print each number in the range specified by those two interger*/
#include<iostream>
using namespace std;
int main(){
int val1=,val2=,tmp=;
cout<<"Enter the first number: ";
cin>>val1;
cout<<"Enter the second number:";
cin>>val2;
if(val1>val2){
tmp=val1;
val1=val2;
val2=val1;
}
while(val1<=val2){
tmp=val1;
++val1;
cout<<tmp<<endl;
}
return ;
}
最新文章
- Mysql多表表关联查询 inner Join left join right join
- iOS----- Crash 分析(文二)-崩溃日志组成
- Java中图片压缩处理
- ARM处理器解析
- Spark 宏观架构&;执行步骤
- (转)教你实现Winform窗体的四边阴影效果
- DSP中CMD文件
- WPF学习之深入浅出话命令
- NoSql数据库使用半年后在设计上面的一些心得 (转)
- 分享一个通用的分页SQL
- aspose.words 处理word转PDF
- manecher
- 【读书笔记】iOS-正则表达式
- JAVA中使用MD5加密实现密码加密
- AlexNet
- Linux设备文件三大结构:inode,file,file_operations
- SNMP 安装及使用
- [UE4]使用UnrealVS扩展快速编译C++代码
- SQL学习之MYSQL的常用命令和增删改查语句和数据类型
- 反Nim博弈
热门文章
- Java调用MySql数据库函数
- ConcurrentLinkedQueue和LinkedBlockingQueue区别
- HTTP协议-Cookie和Session详解
- pytorch数据预处理错误
- Buuctf | BUU LFI COURSE 1
- [CSP-S模拟测试]:attack(支配树+LCA+bitset)
- [CSP-S模拟测试]:Tourist Attractions(简单图论+bitset)
- python global和nonlocal的使用
- 第六周学习总结&;实验报告四
- asp label居中