Documente Academic
Documente Profesional
Documente Cultură
While writing a program: written with only three constructs or types of instructions:
Sequences: Built into C. Programs executed sequentially by default
Know what “building blocks” are available
(sequential operations)
Use good programming principles
Selection: C has three types: if , if… else , and sw itch
Computing problems (conditional operations)
All can be solved by executing a series of actions in a specific order Repetition: C has three types: w hile , do… w hile and for
Algorithm: procedure in terms of (iterative operations)
Actions to be executed Today, virtually all programming languages offer structured
The order in which these actions are to be executed programming capabilities.
Program control The second of the structured programming constructs is
Specify order in which statements are to be executed selection. Selection allows you to choose between two or more
alternatives: It allows you to make decisions.
3 4
Two-Way Selection if… else Statement
The basic decision statement in the computer is the two-way selection. C implements two-way selection with the if…else statement.
The decision is described to the computer as a conditional statement that An if…else statement is a composite statement used to make a
can be answered either true or false. decision between two alternatives.
If the answer is true, one or more
action statements are executed.
If the answer is false, then a
different action or a set of actions
is executed.
Regardless of which set of action
is executed, the program
continues with the next statement
after the selection
Diamond symbol (decision symbol).
19 20
Two-way Selection Example Nested if Statements
/* ****************************************************************
** This program is provided to the professors and students **
** using "Com puter Science: A Structured Approach U sing C, **
For the if…else, the statements may be any statement, including
** Second Edition." Allreprints in any form m ust cite this ** another if…else.
** copyright. **
** C opyright(c) 2001 by Brooks/Cole **Results: When if…else is included within an if…else, it is known as a nested if
** A division of Thom son Learning **
Please enter tw o
*****************************************************************/
integers: 3 5 statement.
3 <= 5
# include < stdio.h>
int m ain (void) { There is no limit
/*LocalD efi nitions */ to how many
int a; Results:
levels can be
int b; Please enter tw o integers: 5 3
5> 3 nested, but if
/*Statem ents */ there are more
printf("Please enter tw o integers: ");
scanf ("% d% d",& a,& b);
than three, they
Results:
can become
Please enter tw o integers: 5 5
if (a < = b)
5 <= 5
difficult to read.
printf("% d < = % d\n",a,b);
else
printf("% d > % d\n",a,b);
return 0;
} /* m ain */
21 22
27 28
The switch Statement The switch Statement
The format must include at least one case As a result of the switch evaluation,
statement. one and only one of the cases will be
Each case expression is associated with a enabled (the switch closed), so that
constant. The keyword case together with there will be a path for the program
its constant are known as a case-labeled to follow. (If none of the switches is
statement. closed, then the statement is skipped
The label is a syntactical identifier that C
and the program continues with the
uses to determine which statement is the next statement after the switch.
starting point in the switch statement. What happens once the program flow
31 32
Multivalued case Statements Summary of switch Statement Rules
/* M ultiple cases for one set of statem ents dem onstration */ 1. The control expression that follows the keyword switch must be
sw itch (printFlag) an integral type.
{
case 1: 2. The expression followed by each case label must be a constant
case 3: printf(“H i,\n"); expression. A constant expression is an expression that is
printf(“this is an odd case!\n"); evaluated at compilation time, not run time.
break;
case 2: 3. No two case labels can have the same value.
case 4: printf(“H i,\n");
4. But two case labels can be associated with the same statements.
printf(“this is an even case!\n");
break; 5. The default label is not required. If the value of the expression
default:printf(“This isn’t a num ber betw een 2 and 4!\n"); does not match with any label, the control transfers outside the
printf(“Bye!\n");
switch statement.
break;
} /* sw itch */ 6. The switch statement can include at most one default label. The
default label may be coded anywhere, but it is traditionally
coded last.
33 34
Do not forget to use a break statement when the cases in a switch statement statements: if...else and switch. for selection in a switch statement.
are exclusive. The if...else construct is used for A default-labeled statement is used
The most common C error is using the assignment operator (=) in place of selection between two alternatives. as the last statement in a switch
the equal operator (= =). You can swap the statements in the
statement, to be executed when
true and false branches if you use the none of the case alternatives match
It is a compile error to use a variable rather than an integral constant as the
complement of an expression in an the tested value.
value in a case label.
if...else statement. Indenting the controlled
It is a compile error to use the same constant in two case labels.
Multiway selection can be
statements in C is good style that
It is generally a logic error to use side effects in the second operand in a enhances the readability of a
logical binary expression, such as (a+ + & & --b) , because the second accomplished using either the switch
statement or an else-if format. program.
operand may not be evaluated.
35 36