第一章 C++ 我来了——1.3 关于注释 & 1.4 While, For and If
2017-01-12 16:07
766 查看
1.3 注释
注释嘛咱就不多说了,照着葫芦画瓢吧!喏,葫芦就在下面:#include <iostream> /* I am block comments * The soft engineers like to employ the "*" as the head of the line to indicate * this line is still a part of the block comments */ int main() { //I am single line comments, I can only protect the content in this line return 0; }
1.4 While, For and If
1.4.1 While
while (condition) while_body-statement;
#include <iostream> int main () { int sum = 0, val = 1; while (val <= 10) { sum += val; ++val; } std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl; return 0; }
Tips:
在使用控制结构进行迭代时,总会需要对
condition进行判断。如果
condition为真时,则执行
while_body_statement。请注意,表达式求值不为
0时,
condition为
TRUE
1.4.2 For
#include <iostream> int main () { int sum = 0; for (int val = 0; val <= 10; ++val) sum += val; std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl; return 0; }
1.4.3 If
#include <iostream> //Calculate the sum of the numbers between v1 and v2 int main () { std::cout << "Enter two numbers: " <<std::endl; int v1, v2; std::cin >> v1 >> v2; int lower, upper; if(v1 <= v2){ lower = v1; upper = v2; } else{ lower = v2; upper = v1; } int sum = 0; for (int i = lower; i <= upper; ++i){ sum += i; } std::cout << sum <<std::endl; return 0; }
1.4.4 读入未知数目的输入
OK,终于有点新东西了,看,那个人好像条狗啊!#include <iostream> int main () { int cout = 0, value; while(std::cin >> value){ if(value < 0){ ++cout; } } std::cout << cout <<std::endl; return 0; }
Tips:
在使用
std::cin >> value进行判断时,有两种情况可以终止循环。
遇到文件结束符(end-of-file)。不同的操作系统使用不同的值作为文件结束符,
Windows系统使用同时键入
ctrl和
z作为文件结束符;
Unix系统下则使用
control-d。
遇到无效输入。如上述
code我们输入非整数值。
注意:上述
code在使用非整数负数进行结束时,该负数会被计入
cout变量。
Postscript:
关于本章
1.5和
1.6两个关于
类的简介,就不再继续展开了,在以后的章节中具体学习。
相关文章推荐
- python if语句、while&for语句
- Struts2中关于"There is no Action mapped for namespace / and action name"的总结
- Struts2中关于"There is no Action mapped for namespace / and action name"的总结
- ActionScript 3.0 Step By Step系列(三):学学流程控制,编编if-else & switch & while & for
- 关于"No result defined for action com.ccxe.credit.action.ReportAction and result input"的错误
- C++编译错误"a storage class can only be specified for objects and functions"解决方法
- JavaSE: 关于 for&if 结合的经典例题:求num1-num2之间的素数
- ActionScript 3.0 Step By Step系列(三):学学流程控制,编编if-else & switch & while & for
- Java基础第一阶段——03_Java语言基础_运算符&if&while&switch&for语句
- Struts2中关于"There is no Action mapped for namespace / and action name"的总结
- J2EE Struts2关于"There is no Action mapped for namespace / and action name UserAction"的调试
- C++中四大语句总结(while、for、if、switch)
- Struts2中关于"There is no Action mapped for namespace / and action name"的总结 今天在调试一个基础的Struts2框架小程
- c++学习 day2 循环学习(while do while for) 分支学习(if ; if else; if else if; switch的应用 )
- "getline" bug fix for Microsoft Visual C++ 6.0 关于VC6的getline输入需要两个回车才结束的BUG修改方法
- ActionScript 3.0 Step By Step系列(三):学学流程控制,编编if-else &amp; switch &amp; while &amp; for
- Struts2中关于"There is no Action mapped for namespace / and action name"的总结
- 【python】if&&for&&while语句
- C++常考笔试题:不用if,while,do-while,for,打印出所有大于0小于k的整数.函数原型void printLess(int k);
- Java 笔记 之 流程控制语句( if & switch & for & while)