Dark Horse Programmer---C basic 3 "Variable error" "Program structure" "If statement" "Switch statement"

Source: Internet
Author: User
Tags case statement

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

"Easy error of variable"

1. Why is the variable initialized to 0

int sum,a=3;

sum = Sum+a

If uninitialized, it becomes an indeterminate variable, and the result is indeterminate and error prone.

2. Conversions between different types of variables

    remember int a=1,b=0;b=1-1.5; where b is an integer all the results are reserved for the integer part of 0, not 0.5, and because 0 has no positive and negative points, all the saved results are b=0;

3. A quick comment plugin for Xcode

Shortcut key://

"Program Structure"

1. What is the program structure

The concept was first proposed by E.w.dijikstra in 1965 and is an important milestone in software development. Its main point of view is the use of top-down, gradual refinement and modularization of the program design method, using three kinds of basic control structure construction program, any program can be ordered, select, cycle three basic control structure. The main emphasis of structured programming is the readability of the program.

2, the structure of the program has three kinds

Sequential structure

The sequential structure represents that the actions in the program are performed in the order in which they appear .

Select structure

The process step for selecting a struct represents a branch, which requires that one of the branches be selected according to a particular condition. The choice structure has the single choice, the double choice and the multi-choice three kinds of forms.

Loop structure

A looping structure indicates that a program performs one or more operations repeatedly until a condition is false (or true) to terminate the loop. The main thing in the loop structure is: Under what circumstances does the loop execute? What operations need to be executed in a loop? There are two basic forms of cyclic structure: the type loop and the until type loop.

When the type of cycle : the first to determine the conditions, when the given conditions to execute the loop body, and at the loop terminal process automatically return to the loop inlet; If the condition is not satisfied, exit the loop body directly to the process exit. Because it is "executing the loop when the condition is satisfied", that is, it is executed after judgment, so it is called a type loop.

Until the type of loop: From the structure of the entrance to execute the loop body, at the end of the loop to determine the condition, if the condition is not satisfied, return to the entrance to continue to carry out the loop body, until the condition is true and then exit the loop to reach the exit of the process, is first executed after judgment Because it is " until the condition is true ", it iscalled until the type loop.

"If statement"

1, if statement introduction

If (expression/constant/variable)

{

Statement 1

}

Subsequent statements

Executes statement 1 if the expression is true (not 0), otherwise it is not executed;

Note the point:

if (condition); You can write a semicolon ";" to indicate that nothing is done, empty statements

if (condition) return 0; direct exit;

if (1), if (0), Forever true, forever false; {Curly braces are always executed, never executed}

The IF statement has a scope problem if the statement is also a block of code that belongs to a local variable.

If there is only one sentence behind the IF statement, you can omit the curly brace

Do not omit the curly braces casually!

"Switch Statement"

1. Format:

switch (expression) {

case constant Expression 1: statement 1;

case constant Expression 1: statement 1;

case constant Expression 1: statement 1;

case constant Expression 1: statement 1;

....

Default: Statement n+1;

}

Case penetrating:

When a case statement executes, it does not end but executes the other case statements directly;

How to prevent case penetration: Add a break after each case statement, and jump out of the switch statement;

2, the use of switch statements Note:

Switch condition Type:

Expression type:

Variables of integer type, constants of integral type;

The return value is an expression of integral type; 5+5

An expression converted to type int;

A case statement can be followed by a multiline statement, which can omit curly braces; spaces and colons, which can be constants, can be constant expressions 5+5 (a+5 not), and cannot be real

The first sentence of the case statement cannot define a variable; the first sentence can be defined by a curly brace;

Compiler bug

The default statement can be placed anywhere on the switch, and break can be omitted; The default statement will also penetrate!

Case penetrates, if default is not penetrated prior

3. If statement and switch statement selection

It is best to use if when branching is more and cannot be exhausted or a large number of enumerations are used.

If the number of branches is not many, but the use if code code is larger than the time, you can choose switch;

Dark Horse Programmer---C basic 3 "Variable error" "Program structure" "If statement" "Switch statement"

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.