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 ;
}

最新文章

  1. Mysql多表表关联查询 inner Join left join right join
  2. iOS----- Crash 分析(文二)-崩溃日志组成
  3. Java中图片压缩处理
  4. ARM处理器解析
  5. Spark 宏观架构&amp;执行步骤
  6. (转)教你实现Winform窗体的四边阴影效果
  7. DSP中CMD文件
  8. WPF学习之深入浅出话命令
  9. NoSql数据库使用半年后在设计上面的一些心得 (转)
  10. 分享一个通用的分页SQL
  11. aspose.words 处理word转PDF
  12. manecher
  13. 【读书笔记】iOS-正则表达式
  14. JAVA中使用MD5加密实现密码加密
  15. AlexNet
  16. Linux设备文件三大结构:inode,file,file_operations
  17. SNMP 安装及使用
  18. [UE4]使用UnrealVS扩展快速编译C++代码
  19. SQL学习之MYSQL的常用命令和增删改查语句和数据类型
  20. 反Nim博弈

热门文章

  1. Java调用MySql数据库函数
  2. ConcurrentLinkedQueue和LinkedBlockingQueue区别
  3. HTTP协议-Cookie和Session详解
  4. pytorch数据预处理错误
  5. Buuctf | BUU LFI COURSE 1
  6. [CSP-S模拟测试]:attack(支配树+LCA+bitset)
  7. [CSP-S模拟测试]:Tourist Attractions(简单图论+bitset)
  8. python global和nonlocal的使用
  9. 第六周学习总结&amp;实验报告四
  10. asp label居中