for
for(init_expr; test_expr; update_expr)
1. init_expr:使用C++的风格,只能初始化同一类型的一个或多个变量,使用C风格的初始化方式可以初始化多个类型的变量
2. test_expr:通常是一个逻辑表达式,但是可以是任何的表达式。不过如果表达式是非逻辑表达式,系统会认为表达式返回true
for循环会进入死循环。如果要插入多个表达式,中间用逗号分隔,系统会用最后一个表达式的值做为整个test_expr的返回值。
3. update_expr:通常会为变量递增或递减,但是可以是任何的表达式,多个表达式之间用逗号分隔。
4. 三部分都可以没有表达式,但分号必须要加上
int m;
for(int i = 0; cout << "hello world" << endl,i<3;i++,cout << "xyz" << endl,cin >>m)
{
cout << i << endl;
}
for(;false;)
{
}
for-in
vector<int> codes{100,200,300};
for(auto code:codes)
{
cout << code << " ";
}
while
string s = "hello world";
// <>
unsigned long size = s.size();
int i = -1;
while(cout << "x" << endl,(++i) < size)
{
if(s[i] != ' ')
{
cout << "<" << s[i] << ">";
}
else
{
cout << s[i];
}
//i++; // 最好先编写控制变量值的代码
}
cout << endl;
string ss = "I love you";
auto p = begin(ss) - 1;
auto endP = end(ss);
while((++p) != endP)
{
if(*p != ' ')
{
cout << "<" << *p << ">";
}
else
{
cout << *p;
}
// p++;
}
cout << endl;
do…while
string s = "hello world";
unsigned long size = s.size();
int i = 0;
do
{
if(s[i] != ' ')
{
cout << "<" << s[i] << ">";
}
else
{
cout << s[i];
}
//i++;
}
while(cout << "abc" << endl,(++i) < size);
cout << endl;
string ss = "I love you";
auto p = begin(ss);
auto endP = end(ss);
do
{
if(*p != ' ')
{
cout << "<" << *p << ">";
}
else
{
cout << *p;
}
// p++;
}
while((++p) != endP);
cout << endl;
集合与指针
// 集合与指针(begin&&end)
int arr_int[5]{1,2,3,4,5};
// 通过指针对数组进行循环
int *endP = arr_int + sizeof(arr_int)/sizeof(int);
for(int *p = arr_int; p != endP;p++)
{
cout << *p << endl;
}
for(auto p = begin(arr_int); p != end(arr_int);p++)
{
cout << *p << endl;
}
// vector、array
vector<int> codes = {200,300,400};
// for(auto p = codes.begin(); p != end(codes);p++)
for(auto p = begin(codes); p != end(codes);p++)
{
cout << *p << endl;
}
array<int, 3> arr_codes{123,456,789};
//for(auto p = arr_codes.begin();p!=arr_codes.end();p++)
for(auto p = begin(arr_codes);p!=end(arr_codes);p++)
{
cout << *p << endl;
}
break、 continue
break:跳出循环
continue: 跳出当前循环,即当前循环条件内,continue后剩余语句不执行,直接执行下一次循环
自增、自减、组合操作运算符
int m = 20;
int n = 30;
cout << "m = " << m <<endl;
cout << "m++ = " << m++ << endl;
cout << "m = " << m <<endl;
cout << "n = " << n << endl;
cout << "++n = " << ++n << endl;
cout << "n = " << n << endl;
int a = 40;
cout << "a = " << a << endl;
cout << "a-- = " << a-- << endl;
cout << "--a = " << --a << endl;
int x = 10;
/*
2 * x++ = 20 x = 11
2 + --x = 12
*/
x = 2 * x++ *(2 + --x); // 并不建议在表达式中过多地使用++和--
long codes[] = {1,2,3};
long *pCodes = codes;
// pCodes = pCodes + 1;
cout << *pCodes << endl;
cout << pCodes << endl;
pCodes++;
cout << *pCodes << endl;
cout << pCodes << endl;
/*
0x7fff5fbffabc bc+1 = bd + 1 = be + 1 = bf + 1 = c0
0x7fff5fbffac0 int为4个字节,地址加4
*/
/*
a = a + 4; a += 4;
+=、-=、*=、/=、%=
*/
int y = 10;
//y = y + 20;
y *= 20;
goto与循环
goto语句
1. 跳出深层循环
int intArray[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int colSize = sizeof(intArray[0])/sizeof(int);
int rowSize = sizeof(intArray)/sizeof(int)/colSize;
cout << colSize << endl;
cout << rowSize << endl;
// 第1层循环对行进行循环
for(int i = 0; i < rowSize;i++)
{
// label: // 容易引起死循环
// 第2层循环对列进行循环
for(int j = 0; j < colSize;j++)
{
if(j == 2)
{
goto label;
//break;
}
cout << intArray[i][j] << " ";
}
label:
cout << endl;
}
2. 实现循环(与汇编的实现方法类似)
// 用goto语句实现循环
int i = 0;
loop:
cout << i << endl;
i++;
if(i < 10)
goto loop;
for(int i =0; i < 10;i++)
{
cout << i << endl;
}```