【C++】学习笔记二十一——if语句
2016-12-23 11:03
274 查看
if (test-condition) statement
程序6.1
#include<iostream> int main() { using namespace std; char ch; int spaces = 0; int total = 0; cin.get(ch); while (ch != '.') { if (ch == ' ') ++spaces; ++total; cin.get(ch); } cout << spaces << " spaces, " << total; cout << " characters total in sentence\n"; system("pause"); return 0; }
1. if else语句
if (test-condition) //如果测试条件为true或非0,将执行statement1,跳过statement2 statement1 else //如果测试条件为false或0,将执行statement2 statement2
程序6.2
#include<iostream> int main() { using namespace std; char ch; cout << "Type, and I shall repeat.\n"; cin.get(ch); while (ch!='.') { if (ch == '\n') cout << ch; else cout << ++ch; cin.get(ch); } cout << "\nPlease excuse the slight confusion.\n"; system("pause"); return 0; }
if else中的两种操作都必须是一条语句,如果需要多条语句,需要用大括号将它们括起来,组成一个块语句。
2. if else if else结构
程序6.3#include<iostream> const int Fave = 27; int main() { using namespace std; int n; cout << "Enter a number in the range 1-100 to find "; cout << "my favorite number: "; do { cin >> n; if (n < Fave) cout << "Too low - - guess again: "; else if (n > Fave) cout << "Too high - - guess again: "; else cout << Fave << " is right!\n"; } while (n != Fave); system("pause"); return 0; }
相关文章推荐
- C++学习笔记 5th —— 万能流程控制if语句 续篇:扩展与实际应用
- C++学习笔记(二) if语句
- C++学习笔记 4th —— 万能流程控制if语句
- shell脚本 if语句学习笔记
- C++学习笔记-条件操作符&&开关语句
- shell脚本编程之if语句学习笔记
- C++学习日记--分支结构(if语句)
- C++学习笔记序列之返回语句区别总结
- python 学习笔记 if语句
- 【学习笔记】再次给if语句后面的分号弄到崩溃
- C++Primer第五版【学习笔记】——第五章 语句
- shell脚本编程之if语句学习笔记
- C++学习笔记五-语句
- Windows 8学习笔记(二十一)--C++ 文件异步操作
- c++学习笔记-------《c++自学通》第四章 表达式与语句
- C++ If学习笔记(4)
- 2012/1/14 《C++ Primer Plus》第六章:分支语句和逻辑操作符 学习笔记
- 【菜鸟C++学习笔记】14.for语句
- 【C++学习笔记】08_流程控制语句
- C++ 学习笔记 1.0 : 基本语言(变量和基本类型,标准库类型,表达式,语句,函数,标准IO库)