Practical C Programming Chapter 6
Branching statements cause on section of code to be executed or not executed, depending on a conditional clause.
Looping statements are used to repeat a section of code a number of times or until some condition occurs.
1. if statement
relational operators
Operator Meaning
<= Less than or equal to
< Less than
> Greater than
>= Greater than or equal to
== Equal
!= Not equal
Multiple statements may be grouped by putting them inside curly braces ({}).
2. else statement
3. How not to use strcmp
the function strcmp compares two strings, and then returns zero if they are equal or nonzero if they are different
4. Looping statements
5. while statements
6. break statement
suppose we want to add a series of numbers, but we don't know how many numbers are to be added together.
we use the statement:
7. continue statement
the continue statement is very similiar to the break statement, except that instead of terminating the loop, continue starts re-executing the body of the loop from the top.
8. Assignment anywhere side effect
c allows the use of assignment statements almost anywhere.
however, avoid it.
Note: programmers frequently have to modify code that someone else wrote. A good exercise is to take someone else's code and then modify it.
Branching statements cause on section of code to be executed or not executed, depending on a conditional clause.
Looping statements are used to repeat a section of code a number of times or until some condition occurs.
1. if statement
if (condition)
statement;
if the condition is true (nonzero), the statement will be executed, otherwise not.
relational operators
Operator Meaning
<= Less than or equal to
< Less than
> Greater than
>= Greater than or equal to
== Equal
!= Not equal
Multiple statements may be grouped by putting them inside curly braces ({}).
2. else statement
if (condition)
statement;
else
statement;
write code as clearly and simply as possible
3. How not to use strcmp
the function strcmp compares two strings, and then returns zero if they are equal or nonzero if they are different
/* check to see if string1 == string2 */
if (strcmp(string1, string2) == 0) {
statements;
} else {
statements;
}
not to use
if (strcm(string1, string2)) {
statements;
}
4. Looping statements
5. while statements
while (condition) {
statements;
}
The program will repeatedly execute the statement inside the while until the condition becomes false (0)
6. break statement
suppose we want to add a series of numbers, but we don't know how many numbers are to be added together.
we use the statement:
if (condition) {
break;
}
to exit the loop
7. continue statement
the continue statement is very similiar to the break statement, except that instead of terminating the loop, continue starts re-executing the body of the loop from the top.
8. Assignment anywhere side effect
c allows the use of assignment statements almost anywhere.
however, avoid it.
Note: programmers frequently have to modify code that someone else wrote. A good exercise is to take someone else's code and then modify it.