C # <basics> Study Notes (4) - [Flow Control]

(A) exception caught

Definition: no grammatical errors, while the program is running, for some reason an error, can not run normally

//程序中经常出现各种异常,为了让代码程序更加鲁棒,经常使用try-catch来进行异常捕获
try
{
	可能出行异常的代码;
}
catch
{
	出现异常后,执行的代码;
}
//执过程中,如果try中 代码未出现异常,catch中的代码不会执行;
//如果出行异常,后面的代码不再执行,转而执行catch中的代码

*** NOTE: *** variable scope of its general press statement parentheses begin, end brackets to the end of the corresponding brackets, in this position you can access this variable.

(B) switch-case structure

switch(变量或者表达式的值)
{
	case1: 要执行的代码;
	break;
	case2: 要执行的代码;
	break;
	case3case4:要执行的代码;
	break;
	default:要执行的代码;
	break;
}

and if-else if switch multibranched structure can be achieved, but the structure may if-else if processing range, but generally only switch-case structure for equivalence comparison;

(Ii) cyclic structure

(1) while loop

//当条件判断为真,就不断执行循环体
//防止死循环
while(循环条件)
{
	循环体;
}

(2) break usage

  1. Switch-case structure can jump;
  2. Can jump one cycle; generally not used alone, but used in conjunction with determining if;

(3) do-while loop

//不管怎么样,先做一遍,执行的结果是循环条件
//而while循环是先判断循环条件,再执行循环体
do
{
	循环体;
}while(循环条件);

(4) for circulation

//语法
//*快捷键:输入for以后连续输入两个Tab即可*
for(表达式1;表达式2;表达式3)
{
	循环体;
}
  1. The first expression: loop variable definitions; recording cycles;
  2. The second expression: The cycling conditions;
  3. A third expression: change the loop condition;
//常用于一直循环次数的循环
for(int i = 0; i < 0; i++)
{
	循环体;
}

(5) continue usage

An immediate end to this cycle, cycling back to determine the conditions, if set up, to enter the next cycle, or exit the loop;

And determining if conditions typically used in common;

(C) Debugging

  • After writing a while, you want to look at the process of implementation of this program
  • After writing a program to discover and does not go as they wish

Debugging method

  1. F11: Step Into debugging (stepping)
  2. F10: by-process debugging
  3. Breakpoint debugging

(1) F11 single-step debugging

  • Yellow arrow points, the current code is not executed, but the code to be executed;
  • Debug -> Windows -> Monitor to open the first

(2) breakpoint debugging

  • First, the problem of the estimated position code, insert a breakpoint, the breakpoint program runs (the previous step) is not executed down;
  • Then execute down, press F11

(D) a triplet of expressions

表达式1?表达式2:表达式3
//首先判断表达式1,结果为true,则结果为表达式2,否则结果为表达式3;
//注意:表达式2的结果类型,必须和表达式3的类型结果一致;
//并且跟整个三元表达式结果一致

Those who can use if-else can be used by a triplet of expressions

(E) generating a random number

//产生随机数
//1、创建能够产生随机数的对象;
Random r = new Random();
//2、 让产生随机数的对象调用方法来产生随机数;
int rNumber = r.Next(1, 10);//区间左闭右开
Published 34 original articles · won praise 0 · Views 1003

Guess you like

Origin blog.csdn.net/forever_008/article/details/104013742