Do not repost the labor results of developers
The control statements in objective-C include the following:
? Branch statement: If-Else, switch
? Loop statement: while, do-while,
? Jump statements related to program transfer: Break, continue, Goto
The ability to make judgments is a basic feature for any programming language.
The IF-else statement branch statement provides a control mechanism that allows program execution to skip some statements without executing them, but instead execute specific statements.
1. conditional statement if-Else
2. Switch multiple branch statements
The basic syntax of the Condition Statement if-else is as follows. The section in "[]" can be omitted.
If (Boolean-expression)
Statement1;
[Else if (Boolean-expression) statement2;]
[Else statement3;]
Example: int number1 = 0;
Int number2 = 1;
Int max = 0;
If (number1> number2 ){
Max = number1;
} Else {
Max = number2;
}
// Print the maximum value Max
Nslog (@ "the maximum is % I", max );
For the if statement, the following code is very valuable:
Int numerator;
Int denominator;
-(Double) converttonum
{
If (denominator! = 0) {// it is necessary to prevent the denominator from being 0.
Return (double) numerator/denominator; // it is necessary to convert the double type to the double type. Otherwise, the decimal point value is lost.
} Else {
Return Nan; // here Nan represents a number, which is defined in the system header file math. h.
}
}
Judge whether the integer can be divided by 2
Remainder = number_to_test % 2;
If (remainder = 0 ){
// Division 2
Xxoo
}
Finally, the conditional expression of if must be enclosed in parentheses and should not be overly dependent on priority.
Predict situations where the program may fail or produce unexpected results as much as possible, and then take preventive measures to cope with these situations,
Is a necessary part of writing excellent and reliable programs.
In fact, the write performance of this conditional statement depends on the meticulous logic thinking.
Objective-C has two built-in features that make it easier to use Boolean variables.
A special type is bool, which can be used to declare variables whose values are not true or false.
The other is the predefined values yes and no.
If the value is not zero, the value is satisfied and the value is zero, indicating that the value is not satisfied.
The syntax of the switch statement is as follows.
Switch (expression ){
Case value1: statement1;
Break;
............
Case valuen: statemendn;
Break;
[Default: defaultstatement;]
}
Example: int score = 0;
Scanf ("% I", & score );
Int scoreval = score/10;
Char reschar = '';
Switch (scoreval ){
Case 9:
Reschar = 'a ';
Break;
Case 8:
Reschar = 'B ';
Break;
Case 7:
Reschar = 'C ';
Break;
Case 6:
Reschar = 'E ';
Break;
Default:
Reschar = 'F ';
}
Nslog (@ "your score is: % C", reschar );
Note that the return value type of expression must be an integer or can be automatically converted to an integer, therefore, it can be _ bool, Char, short int, enumeration type, Int, long int, Longlong, and their unsigned types. But it cannot be float or double. The value valuen In the case clause must be a constant, and the values in all case clauses should be different. The default clause is optional. The break statement is used to exit the switch statement after a case Branch is executed, that is, to terminate the execution of the switch statement. In some special cases, a group of identical operations must be performed for multiple different case values. In this case, break is not required.
Objective-C magic path [5-select structure]