Wild brush Daguai articles on King · C ++ · · of 9 cycles & Analyzing

Reference links

RUNOOB.COM

Judge

In judging statements are mainly two: if ... else and switch two statements. Here we mainly through the code to illustrate:

A look to see if ... else statement:

#include <iostream>

using namespace std;
int main()
{
	float iVar1 = 1, iVar2 = 2;
	
	if(iVar1 > iVar2)                     //判断语句
		cout << "iVar1 大于 iVar2 " << endl; 
	else if(iVar1 < iVar2)
		cout << "iVar1 小于 iVar2 " << endl; 
	else if (iVar1 == iVar2)
		cout << "iVar1 等于 iVar2 " << endl; 
	else if (iVar1 != iVar2)
		cout << "iVar1 不等于 iVar2 " << endl; 
	else if (iVar1 <= iVar2)
		cout << "iVar1 小于等于 iVar2 " << endl; 
	else if (iVar1 >= iVar2)
		cout << "iVar1 大于等于 iVar2 " << endl;
	else
		cout << "error" << endl;
	getchar();
}

operation result

iVar1 小于 iVar2

The code above is mentioned a few before, but was mainly introduce the operator , and now we can really talk about the above program. In statements to determine if there are three:

//方式一
if(判断语句)
{
    /*语句*/
}
//方式二
if(判断语句)
{
    /*语句*/
}
else
{
    /*语句*/
}
//方式三
if(判断语句)
{
    /*语句*/
}
else if(判断语句)
{
    /*语句*/
}
else if(判断语句)
{
    /*语句*/
}
else
{
    /*语句*/
}

Description : When only a statement can not add {} . And internal statements can be embedded if the judge sentences, explain here, if there is a sentence as long as the judge sentences the establishment, which will execute the program, without the judge's sentence will not be judged. determining if statement is judged one by one from top to bottom.

Let's look at a switch statement

#include <iostream>

using namespace std;
int main()
{
	int iVar1 = 1, iVar2 = 2;

	switch (iVar1)
	{
		case 1:
			cout << "iVar1" << endl;
			break;                      //程序执行到此处后,会跳出switch语句块;如果不添加break,则会顺序执行。
		case 2:
			cout << "iVar2" << endl;
			break;
		default :                      //在上述所有情况不满足的时,则进入到default
			cout << "None" << endl;
			break;
	}
	getchar();
}

operation result

iVar1

Note : In the above description, we can see the switch to determine if there are certain differences. In the case of switch judgment satisfied the conditions, if you do not add a break statement, it will order the implementation of the program until it encounters a break or switch statement runs end. The switch statement also meet internal nested switch, let's look at its use:

switch(/*整型或枚举型*/)
{
    case 常量1:
        /*语句*/
        break;   //可选
    case 常量2:
        /*语句*/
        break;  //可选
    default :
        /*语句*/
        break;  //可选
}

cycle

In fact, this would include three, respectively, for, while, do ... while loop. Or explain one by one through the code directly:

for loop

#include <iostream>

using namespace std;
int main()
{

	for (int iId = 0; iId < 10; iId++)   //for循环语句
	{
		cout << "iId = " << iId << endl; //打印dos界面
	}
	getchar();
}

operation result

iId = 0
iId = 1
iId = 2
iId = 3
iId = 4
iId = 5
iId = 6
iId = 7
iId = 8
iId = 9

Note : for loop is relatively simple, as long as the understanding of the meaning of which can be internal for, let's look at the syntax is:

for(初始化;判断;数值改变)
{
    /*核心语句*/
}

In a for loop value changes can be placed in the core statement in other formats should remain unchanged.

while statement

#include <iostream>

using namespace std;
int main()
{
	int iId = 0;
	while(iId < 10) //while语句 
	{
		cout << "iId = " << iId << endl;
		iId++;
	}
	getchar();
}

operation result

iId = 0
iId = 1
iId = 2
iId = 3
iId = 4
iId = 5
iId = 6
iId = 7
iId = 8
iId = 9

Note : while the program from the point of view of grammar is simpler than a for loop, let's look at it while syntax:

while(判断条件)
{
    /*核心语句*/
}

do ... while statement

#include <iostream>

using namespace std;
int main()
{
	int iId = 11;
	do
	{
		cout << "iId = " << iId << endl;
		iId++;
	} while (iId < 10); //while语句 
	getchar();
}

Note : We see from the program than the above two do ... while loop is slightly more complex, but do ... while this guy more cattle, is the first once, then making this judgment (above two loop after the first execution judgment). Below us, what do ... while syntax:

do
{
    /*核心语句*/
}while(条件)   

Loop control

Finished the loop, we must talk about a few keywords cycle control: the Continue , BREAK , GOTO .

Continue : Because multiple loop would operate within, but need not be circular in some cases, it is necessary to skip this cycle.

#include <iostream>

using namespace std;
int main()
{

	for (int iId = 0; iId < 10; iId++)
	{
		if (iId == 5)      //在iId等于5的情况下,跳过一次循环
			continue;
		cout << "iId = " << iId << endl;
	}
	getchar();
}

operation result

iId = 0
iId = 1
iId = 2
iId = 3
iId = 4
iId = 6
iId = 7
iId = 8
iId = 9

NOTE : The results can be seen from the above procedure is run, in the case of iId equal to 5, the cycle is skipped.

BREAK : In the loop statement, to meet certain conditions, to exit the loop, this time we need to use break.

#include <iostream>

using namespace std;
int main()
{

	for (int iId = 0; iId < 10; iId++)
	{
		if (iId >= 5)  //在iId大于等于5的情况下,直接跳出循环
			break;
		cout << "iId = " << iId << endl;
	}
	getchar();
}

operation result

iId = 0
iId = 1
iId = 2
iId = 3
iId = 4

NOTE : The results can be seen from the above procedure is run, in the case of less than 5 iId directly out of the loop.

goto : where in certain circumstances, it is necessary to deal with the current program, and jump directly to the upper or lower sentence, this time need to use the goto statement.

#include <iostream>

using namespace std;
int main()
{

	int iVar = 1;
Loop:

	iVar++;
	cout << "iVar = " << iVar << endl;
	if(iVar < 3)    //在变量小于3的情况下,直接跳转到Loop当前的语句
		goto Loop;
	getchar();
}

operation result

iVar = 2
iVar = 3

Note : We can see from the results, in the case of less than 3, the jump is taken. But give us some suggestions, minimize the use of the goto statement. goto statement syntax

定义的变量:
     /*核心语句*/
     goto 定义的变量

 

Published 176 original articles · won praise 24 · views 50000 +

Guess you like

Origin blog.csdn.net/CSS360/article/details/104108417