Chapter 4 - Flow Control

来源:互联网 发布:青丘狐花月的头饰淘宝 编辑:程序博客网 时间:2024/05/16 11:15
1. Why control program flow?

Computer program will be limited in what to do if program executes in pure top-down order. Things get better if code can be executed conditionally and repeatedly.


2. Why need Boolean Logic?

Boolean Logic provides a way to evalute outcome in a way of "YES" or "NO" which assists the flow control essentially.


3. What is boolean comparison?

Boolean comparison requires the use of boolean comparison operators to compare two values of many types. Supported comparisons in terms of operators are:
==, !=, <, >, <=, >=


4. What is boolean operation?

Boolean operation can specifically work with boolean values. Supported boolean operators are:
!, &, &&, |, ||, ^

Boolean operators can be combined with assignments. Supported boolean assignment operators are:
&=, |=, ^=

Notice that && is used more often than & for both performance purpose (lazy evaluation) and logical reason.


5. What are bitwise operators?

Bitwise operators work on numeric values in the bitwise level. Supported bitwise operators are:
&, |, ^, ~, >>, <<

Bitwise operators can be combined with assignments. Supported bitwise assignment operators are:
&=, |=, ^=, ~=, >>=, <<=


6. Why need bitwise operator?

6.1 Mathematical operation

6.2 Information Storage

Storing information in a 32-bit integer can be very efficient, because the bitwise operations can be used to check the values of multiple bits simultaneously. | is often used to store a bit and & is often used to remove or check a bit.

int myColor = 0;bool containsRed;myColor = myColor | 2; // Add green bit, myColor now stores 010myColor = myColor | 4; // Add red bit, myColor now stores 110containsRed = (myColor & 4) == 4; // Check value of red bit


7. How to use GOTO statement?

7.1 What is GOTO for?

GOTO enables the program to label lines of code to which execution can jump directly.

7.2 Never use GOTO

Because GOTO makes things very confusing and code becomes very difficult to read.


8. What is ternary operator?

Ternary operator performs a boolean comparison to create a conditional statement to choose one from two operands as the result.

<test> ? <resultIfTrue> : <resultIfFalse>


9. What is IF statement?

9.1 Definition

IF statement produces no result but controls the program flow to conditionally execute one or two different blocks of code based on a boolean test.

9.2 Motivation

IF statement provides a more versatile way to make decisions than ternary operators in terms of selecting to execute multiple statements.

9.3 Format

if (<test>)    //<1 line of code executed if <test> is true>;else    //<1 line of code executed if <test> is false>;
or
if (<test>){    //<code executed if <test> is true>;}else{    //<code executed if <test> is false>;}

9.4 Consecutive IF Statement

When there are multiple conditions to check, here comes the consecutive if statement scenario. The format is as follows:

if (<test1>){    //<code executed if <test1> is true>;}else if (<test2>){    //<code executed if <test2> is true and <test1> is false>;}else if (<test3>){    //<code executed if <test3> is true and <test1> and <test2> are both false>;}...else{    //<code executed if <test1>, <test2> and <test3> are all false>;}


10. What is SWITCH statement?

10.1 Definition

SWITCH statement executes code conditionally by testing a variable for multiple values in one go.

10.2 Format

switch (<test1>){    case <val1>:        <code to execute if <test> == <val1>        break;    case <val2>:        <code to execute if <test> == <val2>        break;    ...    case <valN>:        <code to execute if <test> == <valN>        break;    default:        <code to execute if <test> != <vals>        break;}

10.3 About "<vals>"

Each of the comparison values <val i> must be a constant value, which can be provided as a literal value or a constant variable.To define a constant variable, keyword "const" is added before a normal variable declaration and the value must be assigned at the same time.

const int intTwo = 2;

10.4 About "break"

The block of the switch statement is executed consecutively and it is illegal for flow of execution to reach a second case. Therefore, the break statement is required to terminate the switch statement within a single case.

10.5 Multiple "case"

You can place multiple case statements together where the code after them is executed if any of these conditions is met.


11. How does DO loop work?

11.1 Mechanism

DO loop first executes the code block and then performs a boolean test. This procedure repeats until the test evalutes to false.

11.2 Format

do{    //<code to be looped>} while (<test>);


12. How does WHILE loop work?

12.1 Mechanism

WHILE loop first performs a boolean test and then executes the code block if test evalutes to true. This procedure repeats until the test evalutes to false.

12.2 Format

while (<test>){    //<code to be looped>}

12.3 Slight Difference

WHILE loop works similarly to DO loop but has a slight difference: the boolean test takes place at the first beginning and thus the code block might never get executed if it evaluates to false.


13. How does FOR loop work?

13.1 Purpose

FOR loop is used to execute a code block for a certain number of times and maintains its own counter.

13.2 Format

for (<initialization>; <condition>; <operation>){    <code to be looped>}

13.3 Mechanism

FOR loop works identically to the following WHILE loop.

<initialization>;while (<condition>){    //<code to be looped>    <operation>;}


14. What is loop control?

14.1 Definition

C# provides serveral command statements to control the loop.

14.2 Available Commands

14.2.1 break

This command causes the loop to end immediately and the execution jumps to the next statement after the loop.

14.2.2 continue

This command causes the current loop cycle to end immediately and the next cycle starts.

原创粉丝点击