Академический Документы
Профессиональный Документы
Культура Документы
UP ITTC
AY 2009-2010
Control Structures
Lesson 4
Control Structures
Kinds of Control Structures
Sequence Control Structures
Selection Control Structures
if
if-else
switch
Repetition Control Structures
while
do-while
for
Sequential Program
statement1
int main() {
statement1;
statement2;
statement2
….
statementN;
}
statementN
if Statement
used to conditionally execute a statement or block of statement.
if (expression)
statement;
True or False
In C, every expression has a numeric value
An expression is ‘true’ when its value is non-
zero
it is false it evaluates to zero.
Therefore, in the following –
if (expression)
statement
if (expression)
statement1;
else
statement2;
TRUE
if (expression1) expression1 statement1
statement1; FALSE
else if (expression2) TRUE
expression2 statement2
statement2;
else FALSE
statement3; statement3
rest of program
The conditional or ternary
operator: ‘?: ‘
The ?: operator is a more efficient form for expressing simple if
statements.
It simply states:
if (<expression1>)
<expression2>;
else
<expression3>;
The conditional or ternary
operator: ‘?: ‘
Example:
Suppose we want to assign the maximum of a and
b to z. Using the ? operator, we have the following
statement:
z = (a > b) ? a : b;
where,
The boolean_expression in the syntax can be any
expression that evaluates to TRUE or FALSE only.
The statement can be either a simple statement or
a compound statement.
while loop
FALSE
expression
There
are some things you have to
remember when using the while loop.
Firstand foremost is that the while loop
must terminate eventually. This means that
you have to make sure that the boolean
expression must evaluate to FALSE at some
foreseeable point. Loops that do not
terminate are called infinite loops.
do-while loop
statement/s
TRUE
do {
statement(s); condition
} while ( condition );
FALSE
rest of program
Similar to while loops
Except the condition is evaluated after the loop body
i=1;
do {
printf("%d", i);
i++;
} while (i<=5);
for loop
initialization
for ( <initialization>; <condition>;<increment> ) {
statement(s);
} condition
statement(s)
increment
rest of program
break and continue
Lesson 5
Introduction
C provided subprograms
Procedures
Scope of identifiers
Parameters
Functions
Subprograms
A subprogram is a sequence of program
statements that have common goals.
There are two types of subprograms:
Procedures
Functions
The difference between a procedure and a
function is that a function returns a value (of a
specific type) after it finishes executing.
C-provided Subprograms
C provides us with a lot of procedures and
functions that we can use for our different
purposes.
All we need to do is to “include” the
header where the procedure/function
belongs to by using the #include
precompiler directive.
C-provided Subprograms
Hereare some of the most popular C
procedures and functions:
printf – prints formatted text on screen
(stdio.h)
scanf – read formatted text from
keyboard(stdio.h)
C-provided Subprograms
Hereare some of the most popular C
procedures and functions:
strcpy – copies one string to another
(string.h)
strcmp – compares two strings
(string.h)
strlen – gets the length of a string
(string.h)
Procedures
The syntax for definition of a procedure:
void <procedure_name>
(<formal_parameter_list>)
{
<constant/variable_declaration>
<statements>
}
Procedures
A procedure should be called by a running
function before it will be executed. This is
also true for functions.
To call:
<procedure_name>(<actual_parameter_list>);
Procedures
Example:
void p(int n) {
}
TO CALL:/*placed in function that will
run*/
p(1);
p(i); /* assume: int i = 1; */
Scope of identifiers
In C, there is such a thing as the scope of an
identifier.
The scope of an identifier is confined in the
block where it is declared.
All the variables/constants that are declared at
the start of a block is said to be local with
respect to everything outside the block, but is
said to be global with respect to everything
inside the block.
Parameter Passing
Parameter passing
A way of passing information from one
subprogram to another.
Parameter Passing
There are two types of parameters:
Formal parameters
Actual parameters
Functions
Aswas said before, functions are different
from procedures only in the sense that,
when called, it returns a value of a
specific type to whatever procedure or
function that called it. Otherwise, they are
very much the same.
Functions
Here is the syntax for the definition of
functions:
<return_type><func_name>(<formal_param_list>)
{
<constant/variable_declarations>
<statements>
}
Procedure/Function
Declaration
The following are the syntax for procedure and
function declaration, respectively.
void<procedure_name>(<formal_parameter_list>);
<return_type> <func_name>(<formal_param_list>);
Lesson 6
Introduction
Arrays
Strings
Pointers
Arrays
C provides us with a way to declare a group of data items and
give them a common name. This is called array.
Example:
float grade[10] = {83.5, 97.5, 71.0,
88.0};
Strings
When declaring an array of characters, add one to
num_elements to accommodate the ‘\0’ (null terminator
character) which marks the end of the string.
For example, if you want to store a name that can be at most
30 characters, the declaration should be:
char name[31];
when you use scanf to let the user enter a string of
characters.
scanf("%s", name);
scanf appends ‘\0’ to the end of the string inputted.
Strings
You may also initialize an array of characters with a string
constant. So instead of doing it like this,
char name[31] = {'V', 'a', 'n'};
you can do it like this,
char name[31] = "Van";
but you can’t assign a string constant to it with an
assignment statement.
name = "Van"; /* Error: ... */
Strings
C provides you with the functions to
manipulate strings. Whenever you want to
use these functions, don’t forget to add
the preprocessor directive #include
<string.h>.
Strings
To assign a string to a string variable, you must use strcpy.
char name[31];
strcpy (name, "van");