Objective-c Language Control statements

Source: Internet
Author: User
Tags goto



• Branch statements
• Looping statements
• Jump Statements



The control statements in Objective-c have the following categories:
• Branching statements: if-else, switch
• Looping statements: While, Do-while, for
• Jump statements related to program transfer: Break, continue, goto






Branching statements provide a control mechanism that allows the execution of a program to skip execution of certain statements and to execute specific statements.



1. Conditional Statement If-else
2. Multi-branch Statement switch






Conditional statement If-else, the basic syntax form is as follows, where the section "[]" can be omitted.



if (boolean-expression)
Statement1;
[Else if (boolean-expression) Statement2;]
[Else Statement3;]


int number1 = 0;
int number2 = 1;
int max = 0;
if (number1> number2) {
max = number1;
} else {
max = number2;
}
// print max
NSLog (@ "The maximum is% i", max); 





Multi-branch Statement switch its syntax is as follows, where the part of "[]" can be omitted.


switch (expression) {
Case value1: statement1;
Break;
Uh ...
Case valueN: statemendN;
Break;
[Default: defaultStatement;]
}
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); 





Use the switch statement to be aware that the return value type of expression expressions must be integers or types that can be automatically converted to integers, so it can be _bool, char, short int, enum type, int, long int, Longlong, and their unsigned types. But it cannot be float types such as floats and double. The value in the case clause Valuen 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 cause the program to jump out of the switch statement after executing a case branch, terminating the switch statement execution. In some special cases, multiple different case values perform a set of identical operations, which can be done without a break.






The function of a looping statement is to execute a piece of code repeatedly until the condition of the terminating loop is met. The looping statements provided in the Objective-c language are:



while statements
do-while Statements
for statements



The while statement, which has the following grammatical form, where the section "[]" can be omitted.



[Initialization]
while (termination) {
Body
[Iteration;]
}



• The following code calculates the number of daffodils within 1000.


 
int i = 100; int r, s, t;
r = 0;s = 0;t = 0; while (i < 1000) {
r = i / 100;
s = (i - r * 100) / 10;
t = i - r * 100 - s * 10; if (i == r * r * r + s * s * s + t * t * t) {
NSLog(@" %i " , i);
}
i = i + 1;
}

 





The Do-while statement, which has the following grammatical form, where the section "[]" can be omitted.



[Initialization]
do {
Body
[Iteration;]
} while (termination);



• Calculate the number of daffodils within 1000


 
int i = 100; int r, s, t;
r = 0;s = 0;t = 0; do {
r = i / 100;
s = (i - r * 100) / 10;
t = i - r * 100 - s * 10; if (i == r * r * r + s * s * s + t * t * t) {
NSLog(@" %i " , i);
}
i = i + 1;
} while (i < 1000);


For statement, the syntax is as follows:



for (initialization; termination; iteration) {
Body
}



The following code calculates 0~8 squared, cubic numbers, and outputs:


int i = 8;
int r, s;
r = 0;
s = 0;
for (int j = 0; j <= i; j ++) {
r = j * j;
s = j * j * j;
NSLog (@ "integer:% i square sum% i:
Corresponding cubic sum:% i ", j, i, s);
} 





There are generally 3 statements inside the "()" of the For Loop statement, initialization initialization statements, termination terminating conditional statements, iteration iteration statements, where all 3 statements can be omitted. For example, the initialization of J is placed outside the loop body, the code is as follows:


int j = 0;
for (; j <= i; j ++) {
r = j * j;
s = j * j * j;
NSLog (@ "integer:% i square sum% i:
Corresponding cubic sum:% i ", j, i, s);
} 





The jump with the program transfer has break, continue, goto statement, break and continue are mainly with the loop, goto statement in C, it is unconditional jump can completely replace break and continue, generally should be used with caution.






The break statement was used in switch to terminate the execution of the switch statement. The break statement can also be used in the loop body, terminating the current loop, and executing at the first statement immediately following the loop block. For example, the following code snippet:



for (int i = 0; i < i++) {
if (i = = 3)
Break
NSLog (@ "I =%i", i);
}
NSLog (@ "Game over!");






The continue statement is used to end the loop, skipping statements that have not yet been executed in the loop body, and then judging the termination condition to determine whether to continue the loop. For a For statement, the iteration statement is executed before the termination condition is judged. For example, the following code snippet:



for (int i = 0; i <; i++) {
if (i% 10 = = 0)
Continue
NSLog (@ "%i", I);
}






Goto statements are unconditional jumps and can completely replace break and continue. If it is a nested loop, you can use the goto statement to specify a label to change the process of the program, such as the following code snippet.



for (int j = 0; J <; J + +) {
for (int i = 0; i < i++) {
if (i = = 3)
Goto label;
NSLog (@ "I =%i", i);
}
}
Label
NSLog (@ "Game over!");






The use of break and continue in the inner loop can only jump out of the inner loop, the goto statement does not have this restriction, label: is the label definition, the goto statement is followed by a label.






Objective-c Language Control statements


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.