Professional Documents
Culture Documents
STATEMENTS
LVENUGOPALREDDY
Lect.InComputerScience
Flow of Control
Unless specified otherwise, the order of statement
execution through a function is linear: one statement
after another in sequence
Some programming statements allow us to:
decide whether or not to execute a particular
statement
execute a statement over and over, repetitively
These decisions are based on boolean expressions (or
conditions) that evaluate to true or false
The order of statement execution is called the flow of
control
Conditional Statements
A conditional statement lets us choose which statement
will be executed next
Therefore they are sometimes called selection
statements
Conditional statements give us the power to make basic
decisions
The C conditional statements are the:
if statement
if-else statement
switch statement
Logic of an if statement
condition
evaluated
true
statement
false
The if Statement
The if statement has the following syntax:
if is a C
reserved word
false
statement2
Boolean Expressions
A condition often uses one of C's equality operators or relational
operators, which all return boolean results:
==
!=
<
>
<=
>=
equal to
not equal to
less than
greater than
less than or equal to
greater than or equal to
Note the difference between the equality operator (==) and the
assignment operator (=)
Boolean Expressions in C
C does not have a boolean data type.
Therefore, C compares the values of variables and expressions
against 0 (zero) to determine if they are true or false.
If the value is 0 then the result is implicitly assumed to be false.
If the value is different from 0 then the result is implicitly assumed
to be true.
Block Statements
Several statements can be grouped
together into a block statement delimited
by braces
if (total > MAX)
A block statement
can be used wherever a
{
printf
("Error!!\n");
statement is
called
for in the C syntax
errorCount++;
rules }
Block Statements
In an if-else statement, the if portion, or the
else portion, or both, could be block statements
if (total > MAX)
{
printf("Error!!");
errorCount++;
}
else
{
printf ("Total: %d, total);
current = total*2;
}
Nested if Statements
The statement executed as a result of an if statement
or else clause could be another if statement
These are called nested if statements
An else clause is matched to the last unmatched if (no
matter what the indentation implies)
Braces can be used to specify the if statement to which
an else clause belongs
switch ( expression )
{
case value1 :
statement-list1
case value2 :
statement-list2
case value3 :
statement-list3
case ...
}
If expression
matches value2,
control jumps
to here
Repetition in Programs
In most software, the statements in the
program may need to repeat for many times.
e.g., calculate the value of n!.
If n = 10000, its not elegant to write the code as
1*2*3**10000.
519
e.g.,readthecontentinafile
e.g.,calculatethevalueofn!
Copyright2004PearsonAddisonWesley.
Allrightsreserved.
520
When to Use
We know how many loop
repetitions will be needed
in advance.
C Structure
while, for
while, for
521
Repeated processing of
data until a desired
condition is met.
while, for
522
Statement
Statement
525
Compound Assignment
Operators
count_emp += 1;
time -= 1;
product = product *
item;
product *= item;
total = total /
number;
total /= number;
n = n % (x+1);
n %= x+1;
527
Thevalue
ofjis
increased
Thevalue
ofjisnot
increased
Copyright2004PearsonAddisonWesley.
Allrightsreserved.
531
Nested Loops
Nested loops consist of an outer loop with
one or more inner loops.
loops
e.g.,
Outerloop
for (i=1;i<=100;i++){
for(j=1;j<=50;j++){
Innerloop
}
}
The above loop will run for 100*50 iterations.
532
534