Lecture #11: Statements 2

 

Bob Pitha

bpitha@cs.ucsd.edu

http://www-cse.ucsd.edu/classes/fa99/cse5a

what’s a statement?

•      A specification of some action to be performed

•      Executed sequentially

what kinds of statements do we have?

•      Expression statements

•      Compound statements

•      Selection statements

•      Iteration statements

•      Jump statements

what kinds of statements do we have?

•      Expression statements

•      Compound statements

•      Selection statements

•      Iteration statements

•      Jump statements

example

•      printing powers of 2

Text Box: #include <stdio.h>

int main (void)
{
    int i = 0;
    int power = 1;

    printf (“2 to the %d power is %d\n”, i, power);
    power *=2;
    i++;
    printf (“2 to the %d power is %d\n”, i, power);
    power *=2;
    i++;
    printf (“2 to the %d power is %d\n”, i, power);
    return 0;
}
 


what kinds of statements do we have?

•      Expression statements

•      Compound statements

•      Selection statements

•      Iteration statements

•      Jump statements

compound statements

•      Another example:

Text Box: #include <stdio.h>

int main (void)
{
    int i;
    int power = 1;

    printf (“2 to the %d power is %d\n”, i, power);

    for (i = 0; i < 10; ++i) {
        power *=2;
        printf (“2 to the %d power is %d\n”,
                i, power);
    }
    return 0;
}
 


what kinds of statements do we have?

•      Expression statements

•      Compound statements

•      Selection statements

•      Iteration statements

•      Jump statements

what’s a selection statement?

•      Selects statements to execute based on the value of an expression

–   the expression is sometimes called the controlling expression

•      These are more interesting statements

•      selection statements:

–   if statement (§3.2-3.3)

–   switch statement (§3.4)

selection statements: if

•      Used to execute conditionally a statement or block of code

•      Critical to writing interesting programs - otherwise, the code always follows the same path.

simple if statement

if ( expression ) statement

•      Expression must have a scalar type

–   can’t be an array or complex data structure

•   but we don’t know about those anyway

•      If the expression evaluates to true (i.e. non-zero) then statement is executed

•      Remember what we said about compound statements

–   they are valid anywhere a single statement is valid

–   so you can use a block here

if statement examples

Text Box: if (grade >= 90)
    printf (“Congratulations!\n”);
printf (“Your grade is %d\n”, grade);
 

Text Box: /*
 * calculate the absolute value of a
 * number
 */
int abs (int number)
{
	if (number < 0)
	    number = -number;

	return number;
}
 


if statement examples

Text Box: int i, j;
int min;
…
if (i < j) {
   min = i;
   printf (“i is less than j\n”);
}
if (j < i) {
   min = j;
   printf (“j is less than i\n”);
}
 


if-else statement

if ( expression ) statement1 else statement2

•      If expression evaluates to true, statement1 is executed.

•      If expression evaluates to false (0), statement2 is executed

•      Both statements can be (and very often are) compound statements

if-else example

Text Box: int min;
int i, j;
…
if (i < j) {
   min = i;
   printf (“i is less than j\n”);
} else {
   min = j;
   printf (“j is less than i\n”);
}
 


a comment on style

•      I wrote the generic syntax as:

    if ( expression ) statement1 else statement2

    and your book says:

    if ( expression )

       statement1

    else

       statement2

•      What’s the difference?

–   there isn’t any, to the compiler

if statement style

•      Wherever a space is valid, you can also use a newline

–   so the two statements are identical

–   but it’s easier to read the second one

Text Box: int min;
int i, j;
…
if (i < j) min = i; else min = j;
 


if statement style

•      It’s also easier to read code when nested blocks are indented

Text Box: int min;
int i, j;
…
if (i < j) {
min = i;
printf (“i is less than j\n”);
} else {
min = j;
printf (“j is less than i\n”);
}
 


if statement style

•      It’s also easier to read code when nested blocks are indented

Text Box: int min;
int i, j;
…
if (i < j) {
   min = i;
   printf (“i is less than j\n”);
} else {
   min = j;
   printf (“j is less than i\n”);
}
 


nesting of if statements

•      You can use any statement as the body of an if

–   even another if statement

Text Box: if (month == FEB) {
    if (is_leap_year (year)) {
        if (day > 29)
            legal = FALSE;
    } else
        if (day > 28)
            legal = FALSE;
}
 


discussion of if statements

•    if statements let you distinguish between exactly 2 cases, and do something different for each.

•      They are very powerful

–   many computer programs need to react to changing circumstances or different input

–   computer programs need to check for bad data or error conditions

•   before the error or the bad data cause the program to crash

Text Box: int i, j, k;
…
if (j != 0)
    k = i / j;
else
    printf (“Error: division by 0!\n”);
 


but what about 3 or more cases?

•      We can simulate this using nested ifs:

Text Box: if (grade >= 90)
    printf (“A\n”);
else
    if (grade >= 80)
        printf (“B\n”);
    else
        if (grade >= 70)
            printf (“C\n”);
        else
            if (grade >= 60)
                printf (“D\n”);
            else
                printf (“F\n”);
 


but we can write this more readably

•      We’ll just break our new style rules

Text Box: if (grade >= 90)
    printf (“A\n”);
else if (grade >= 80)
    printf (“B\n”);
else if (grade >= 70)
    printf (“C\n”);
else if (grade >= 60)
    printf (“D\n”);
else
    printf (“F\n”);
 


–   There’s a good example of this in the book in section 3.3