Control Structures
| (9 intermediate revisions by one user not shown) | |||
| Line 1: | Line 1: | ||
| − | {| | + | {{tl|1|}} |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | |} | + | |
| − | + | ||
| − | + | ||
| − | + | ||
| Line 13: | Line 5: | ||
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | {| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | ||
|- | |- | ||
| − | | | + | | class="otherType"| <span class="bigKeywordBI">do</span><span class="bigCodePunc">{ </span><span class="bigCodeBasic">statements </span><span class="bigCodePunc">}</span><span class="bigKeywordBI">while</span><span class="bigCodePunc">(</span><span class="bigCodeBasic">expression</span><span class="bigCodePunc">)</span> |
|- | |- | ||
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|The do-while loop will run the statements between the braces over and over as long as the expression results in a non zero value (true). Once the expression results false, the program will skip beyong the while loop and move on. Since the do-while loop always checks the condition of the expression last, the do-while will ALWAYS run the statments at least once. | | style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|The do-while loop will run the statements between the braces over and over as long as the expression results in a non zero value (true). Once the expression results false, the program will skip beyong the while loop and move on. Since the do-while loop always checks the condition of the expression last, the do-while will ALWAYS run the statments at least once. | ||
| − | + | |- | |
| − | + | | | |
{| | {| | ||
|- | |- | ||
| Line 42: | Line 34: | ||
|- | |- | ||
|} | |} | ||
| − | + | <br /> | |
== for == | == for == | ||
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | {| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | ||
|- | |- | ||
| − | | | + | | class="otherType"| <span class="bigKeywordBI">for</span><span class="bigCodePunc">(</span><span class="bigCodeBasic">expression1</span><span class="bigCodePunc">; </span><span class="bigCodeBasic">expression2</span><span class="bigCodePunc">; </span><span class="bigCodeBasic">expression3</span><span class="bigCodePunc">){ </span><span class="bigCodeBasic">statements </span><span class="bigCodePunc">}</span> |
|- | |- | ||
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|The for loop runs the code between its braces as long as expression2 is true (non zero). It is a neat and compact solution to looping. | | style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|The for loop runs the code between its braces as long as expression2 is true (non zero). It is a neat and compact solution to looping. | ||
| − | + | |- | |
| − | + | | | |
{| | {| | ||
|- | |- | ||
| Line 74: | Line 66: | ||
|- | |- | ||
|} | |} | ||
| − | + | <br /> | |
== if else == | == if else == | ||
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | {| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | ||
|- | |- | ||
| − | | | + | | class="otherType"| <span class="bigKeywordBI">if</span><span class="bigCodePunc">(</span><span class="bigCodeBasic">expression</span><span class="bigCodePunc">){ </span><span class="bigCodeBasic">statement1 </span><span class="bigCodePunc">} </span><span class="bigKeywordBI">else </span><span class="bigCodePunc">{ </span><span class="bigCodeBasic">statement2 </span><span class="bigCodePunc">}</span> |
|- | |- | ||
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|In the <span class="keywordBI">if</span> statement, if the expression in parentheses is nonzero (true), control passes to statement 1. If the <span class="keywordBI">else</span> clause is present and the expresssion is zero (false), control will pass to statement 2. The <span class="keywordBI">else</span> part is optional, and if absent, a false expression will simply result in skipping over the statement 1. An <span class="keywordBI">else</span> always matches the nearest previous unmatched <span class="keywordBI">if</span>; braces may be used to override this when necessary, or for clarity. | | style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|In the <span class="keywordBI">if</span> statement, if the expression in parentheses is nonzero (true), control passes to statement 1. If the <span class="keywordBI">else</span> clause is present and the expresssion is zero (false), control will pass to statement 2. The <span class="keywordBI">else</span> part is optional, and if absent, a false expression will simply result in skipping over the statement 1. An <span class="keywordBI">else</span> always matches the nearest previous unmatched <span class="keywordBI">if</span>; braces may be used to override this when necessary, or for clarity. | ||
| − | + | |- | |
| − | + | | | |
{| | {| | ||
|- | |- | ||
| Line 98: | Line 90: | ||
|- | |- | ||
|} | |} | ||
| − | + | <br /> | |
== switch case== | == switch case== | ||
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | {| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | ||
|- | |- | ||
| − | | | + | | class="otherType"| <span class="bigKeywordBI">switch</span><span class="bigCodePunc">(</span><span class="bigCodeBasic">expression</span><span class="bigCodePunc">){ </span><span class="bigKeywordBI">case </span><span class="bigCodeBasic">label1</span><span class="bigCodePunc">: </span><span class="bigCodeBasic">statements1</span><span class="bigCodePunc">; </span><span class="bigKeywordBI">case </span><span class="bigCodeBasic">label2</span><span class="bigCodePunc">: </span><span class="bigCodeBasic">statements2</span><span class="bigCodePunc">; </span><span class="bigKeywordBI">break</span><span class="bigCodePunc">; </span><span class="bigKeywordBI">default</span><span class="bigCodePunc">: </span><span class="bigCodeBasic">statements3</span><span class="bigCodePunc">; }</span> |
|- | |- | ||
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|The switch statement causes control to be transferred to one of several statements depending on the value of an expression, which must have integral type. The Substatement controlled by a switch is typically compound. Any statement within the substatement may be labeled with one or more case labels, which consist of the keyword case followed by a constant expression and then a colon (:). | | style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|The switch statement causes control to be transferred to one of several statements depending on the value of an expression, which must have integral type. The Substatement controlled by a switch is typically compound. Any statement within the substatement may be labeled with one or more case labels, which consist of the keyword case followed by a constant expression and then a colon (:). | ||
No two of the case constants associated with the same switch may have the same value. There may be at most one default label associated with a switch - if none of the case labels are equal to the expression in the parentheses following switch, control passes to the default label, or if there is no default label, execution resumes just beyond the entire construct. Switches may be nested; a case or default label is associated with the innermost switch that contains it. Switch statements can "fall through", that is, when one case section has completed its execution, statements will continue to be executed downward until a break; statement is encountered. Fall-through is useful in some circumstances, but is usually not desired. In the preceding example, if label2 is reached, the statements statements 2 are executed and nothing more inside the braces. However if label1 is reached, both statements 1 and statements 2 are executed since there is no break to separate the two case statements. | No two of the case constants associated with the same switch may have the same value. There may be at most one default label associated with a switch - if none of the case labels are equal to the expression in the parentheses following switch, control passes to the default label, or if there is no default label, execution resumes just beyond the entire construct. Switches may be nested; a case or default label is associated with the innermost switch that contains it. Switch statements can "fall through", that is, when one case section has completed its execution, statements will continue to be executed downward until a break; statement is encountered. Fall-through is useful in some circumstances, but is usually not desired. In the preceding example, if label2 is reached, the statements statements 2 are executed and nothing more inside the braces. However if label1 is reached, both statements 1 and statements 2 are executed since there is no break to separate the two case statements. | ||
| − | + | |- | |
| − | + | | | |
{| | {| | ||
|- | |- | ||
| Line 133: | Line 125: | ||
|- | |- | ||
|} | |} | ||
| − | + | <br /> | |
== while == | == while == | ||
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | {| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0" | ||
|- | |- | ||
| − | | | + | | class="otherType"| <span class="bigKeywordBI">while</span><span class="bigCodePunc">(</span><span class="bigCodeBasic">expression</span><span class="bigCodePunc">){ </span><span class="bigCodeBasic">statements </span><span class="bigCodePunc">}</span> |
|- | |- | ||
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|The while loop will run the statements between the braces over and over as long as the expression results in a non zero value (true). Once the expression results false, the program will skip beyong the while loop and move on. Since the while loop always checks the condition of the expression first, it is possible that the while loop never runs the statements within the braces. | | style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|The while loop will run the statements between the braces over and over as long as the expression results in a non zero value (true). Once the expression results false, the program will skip beyong the while loop and move on. Since the while loop always checks the condition of the expression first, it is possible that the while loop never runs the statements within the braces. | ||
| − | + | |- | |
| − | + | | | |
{| | {| | ||
|- | |- | ||
| Line 160: | Line 152: | ||
|- | |- | ||
|} | |} | ||
| + | <br /> | ||
Latest revision as of 13:44, 18 April 2012
|
[edit] do while
| do{ statements }while(expression) | |
| The do-while loop will run the statements between the braces over and over as long as the expression results in a non zero value (true). Once the expression results false, the program will skip beyong the while loop and move on. Since the do-while loop always checks the condition of the expression last, the do-while will ALWAYS run the statments at least once. | |
|
[edit] for
| for(expression1; expression2; expression3){ statements } | |
| The for loop runs the code between its braces as long as expression2 is true (non zero). It is a neat and compact solution to looping. | |
|
[edit] if else
| if(expression){ statement1 } else { statement2 } | |
| In the if statement, if the expression in parentheses is nonzero (true), control passes to statement 1. If the else clause is present and the expresssion is zero (false), control will pass to statement 2. The else part is optional, and if absent, a false expression will simply result in skipping over the statement 1. An else always matches the nearest previous unmatched if; braces may be used to override this when necessary, or for clarity. | |
|
[edit] switch case
| switch(expression){ case label1: statements1; case label2: statements2; break; default: statements3; } | |
| The switch statement causes control to be transferred to one of several statements depending on the value of an expression, which must have integral type. The Substatement controlled by a switch is typically compound. Any statement within the substatement may be labeled with one or more case labels, which consist of the keyword case followed by a constant expression and then a colon (:).
No two of the case constants associated with the same switch may have the same value. There may be at most one default label associated with a switch - if none of the case labels are equal to the expression in the parentheses following switch, control passes to the default label, or if there is no default label, execution resumes just beyond the entire construct. Switches may be nested; a case or default label is associated with the innermost switch that contains it. Switch statements can "fall through", that is, when one case section has completed its execution, statements will continue to be executed downward until a break; statement is encountered. Fall-through is useful in some circumstances, but is usually not desired. In the preceding example, if label2 is reached, the statements statements 2 are executed and nothing more inside the braces. However if label1 is reached, both statements 1 and statements 2 are executed since there is no break to separate the two case statements. | |
|
[edit] while
| while(expression){ statements } | |
| The while loop will run the statements between the braces over and over as long as the expression results in a non zero value (true). Once the expression results false, the program will skip beyong the while loop and move on. Since the while loop always checks the condition of the expression first, it is possible that the while loop never runs the statements within the braces. | |
|