第六章 分支语句和逻辑运算符

if...

if...else...

if...else...if......

表达式==常常反转防止失误。

逻辑表达式 or ||  C++规定 ||运算符是个顺序点。也就是说先修改左侧的值再对右侧进行判断。

例如 i++<6||i==j; i==j比较时i为11.另外 左侧表达式为true那么就不会判断右侧表达式。

and && 同样的左侧为false则不会判断右侧。

not ! 

cctype 一个库 用于判断字符是否为空白字符等等

?: 可以用来代替if else 3>2?i=1:i=2 注意没有; 此时i=1.

switch语句 可以用枚举量做标签

break和continue...continue用于for循环时 跳到更新表达式处。

简单文件输入输出 具体可以参考http://www.runoob.com/cplusplus/cpp-files-streams.html

必须包含头文件fstream 头文件定义了处理输出的ofstream类

ofstream outFile;

outFile.open("fish.txt");

i=2;

outFile<<i; //将2写入fish.txt

下面实例引自菜鸟教程

#include <fstream>

#include <iostream>

using namespace std;

int main (){   

  char data[100];

  // 以写模式打开文件  ofstream outfile;

  outfile.open("afile.dat");

  cout << "Writing to the file" << endl;

  cout << "Enter your name: ";

  cin.getline(data, 100);

  // 向文件写入用户输入的数据  outfile << data << endl;

  cout << "Enter your age: ";

  cin >> data;

  cin.ignore();


  // 再次向文件写入用户输入的数据  outfile << data << endl;

  // 关闭打开的文件  outfile.close();

  // 以读模式打开文件  ifstream infile;

  infile.open("afile.dat");

  cout << "Reading from the file" << endl;

  infile >> data;

  // 在屏幕上写入数据  cout << data << endl;


  // 再次从文件读取数据,并显示它  infile >> data;

  cout << data << endl;

  // 关闭打开的文件  infile.close();

  return 0;}

(完)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容