Control statements for C Language Learning

Source: Internet
Author: User
Tags exit in

1. if statement
. If statement
If (x % 2)
Y + = x/2;
If the expression in the brackets is true, run the y + = x/2 Statement.
. If else statement
If (x % 2 = 0)
Y + = x/2;
Else
Y + = (x + 1)/2;
If the expression in if is true, execute the statement after if. Otherwise, execute the statement after else.

. If else if statement
If (x % 2 = 0)
Y + = x/2;
Else if (x % 4 = 1)
Y + = 2 * (x + 3)/4 );
Else
Y + = (x + 1)/2;

2. switch statement
Switch (ch ){
Case 'y ':
Break;
Case 'N ':
Break;
Default:
Break;
}
If the match is successful, the statement after case is executed. If there is no break statement after case, it will continue to be executed until the entire statement ends or encounters break. Sometimes we can use this to deliberately not write a break statement. For example:
Switch (ch ){
Case 'y ':
Case 'y ':
Break;
}
Default is optional and can be written or not written.

3. while statement
While (/* condition */)
/* Loop body */

4. for statement
Int factorial (int n)
{
Int I, j = 1;
For (I = 1; I <= n; I ++)
J * = I;
Return j;
}
The for statement contains three parts:
Initialization part: I = 1
Condition section: I <= n
Adjustment part: I ++
Each part of the three parts is optional, but the semicolon cannot be omitted.
The initialization part is executed only once when the loop starts.
In fact, the for statement can be exchanged with the while statement. for example, the previous example can be rewritten as follows using the while statement:
Int factorial (int n)
{
Int j = 1;
Int I = 1;/* initialation */
While (I <= n) {/* condition */
J * = I;
I ++;/* increment */
}
Return j;
}
For compound expressions in for, use commas to separate them.
Int factorial (int n ){
Int I, j;
For (I = 1, j = 1; I <= n; j * = I, I ++)
;
Return j;
}
Select the for statement or the while statement based on the actual situation. If you want to perform simple initialization and incremental processing, you 'd better select the for statement and place the loop control part on the top, it is easy to understand.

5. do while statement
Different from while, it must first execute the body part of a loop and then perform conditional judgment.

6. break and continue keywords
The break statement is used to roll out the inmost loop or exit in the swith statement.
The continue statement indicates that the part of the loop body is skipped and directly adjusted to the condition part of the loop to execute the next loop.


From Tracy Mcgrady's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.