Objective-C magic path [5-select structure], objective-c5-
Master haomeng is devoted to his contribution and respects the author's Labor achievements. Do not repost them.
If the article is helpful to you, you are welcome to donate to the author, support haomeng master, the amount of donation is free, focusing on your mind ^_^
I want to donate: Click to donate
Cocos2d-X source code download: point I send
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, and 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.
Must the 5-wave structure in the 5-5-5 structure of the Sawtooth adjustment wave comply with the Three Laws of iron?
No, the timing is sometimes even more powerful, and the 5-wave structure is just a kind of elegant object,
Use MyEclipse to use the if selection structure: if you are 7 years old or 5 years old and gender is "male", you can move the table height
The following code: If you need to modify or have any questions, you can ask questions or hi
Public class T {
/**
* If you are 7 years old or 5 years old and have a gender of "male ",
* @ Param args
*/
Public static void main (String [] args ){
Required bytes = new bytes (System. in );
Int age = 0;
String sex = "";
While (true ){
System. out. print ("Enter age :");
Age = response. nextInt ();
If (age <0 | age> 100)
System. out. println ("age error! ");
Else
Break;
}
If (age> = 7 ){
System. out. println ("you can move the table ");
} Else if (age> = 5 ){
While (true ){
System. out. print ("Enter Gender :");
Sex = rule. next ();
If ("male". equals (sex) | "female". equals (sex ))
Break;
Else
System. out. println ("Gender error! ");
}
If ("male". equals (sex ))
System. out. println ("you can move the table ");
Else
System. out. println ("the table cannot be moved ");
} Else {
System. out. println ("the table cannot be moved ");
}
}
}