IPC144- Class 7

Basic Program Control (Continued)

Agenda: (We want to cover pages 32 - 35 in the Evan Weaver Notes)

1. We do not cover de Morgan’s Law. 2. The for statement. 3. The switch statement. ------2a). the for Statement:

Commonly used when we want to count the number of times we go through a loop.

The for statement performs in a similar way to the while statement.

i) Example of a while loop to add 1 + 2 + 3 + 4 + … + 9 + 10 :

total = 0; n = 1; while( n < =10 ) { total = total + n; printf ( “ Total so far is : %d \n”, total); n = n + 1; }

ii) New we will show the same calculation using a for statement.

total = 0; for ( n = 1; n < =10 ; n = n + 1) { total = total + n; printf ( “ Total so far is : %d \n”, total); }

The advantage of the for loop is that all the elements that go into the decisions for looping are clearly seen on one line.

------2b). Steps in the execution of a for statement.

Line #

1: total = 0; 2: for ( n = 1; n < =10 ; n = n + 1) 3: { 4: total = total + n; 5: printf ( “ Total so far is : %d \n”, total); 6: } 7: printf( “ The for loop has terminated. \n”);

Steps

1) In line 1 we initialize total to 0. 2) In line 2 we initialize n to 1. 3) In line 2: Before going into the loop, we check the condition: Is n <= 10? If true, we enter the body of the loop. If false we skip out of the loop to line 7. 4) Inside the loop we do line 4 … add n to the accumulating total. 5) We also do line 5: print the value of total so far. 6) Line 6 the end of the loop tells us to go back to line 2 and do the third part of the for statement: n will increase by 1.

We repeat steps 3 to 6 until the second part of the for loop becomes false. This will occur when n = 11.

At that point the program will continue at line 7.

------

2c). Sample program to be written in class:

Write a program to count backwards from 51 to 0 in steps of 3.

Is it possible to write this program with a while statement? ___ Y ___ N An if statement? ___ Y ___ N An if / else ? ___ Y ___ N A do / while? ___ Y ___ N 3a) The switch statement:

This statement is used when we have many unique cases, each of which is identified by a particular constant.

Each case is associated with a code block.

------

3b) Consider the situation of opening a new bank account.

char account_type; printf(“ Type of account? c … chequing\n”); printf(“ s … spouse checking\n”); printf(“ g … savings\n”); printf(“ p … personal line of credit\n”);

scanf(“ %c”, &account_type);

switch (account_type) { case ‘c’: annual_int_rate = 0.005; opening_gift = 50.00; break;

case ‘s’: annual_int_rate = 0.005; opening_gift = 50.00; break;

case ‘g’: annual_int_rate = 0.025; opening_gift = 100.00; break;

case ‘p’: annual_int_rate = -0.07; opening_gift = 150.00; break;

default: annual_int_rate = 0.0; opening_gift = 50.00; } 3c). We can join cases together if the code block is the same.

Notice that case ‘c’ chequing and case ‘s’ spouse chequing have identical code blocks.

The switch statement could be written as:

switch (account_type) { case ‘c’: case ‘s’: annual_int_rate = 0.005; opening_gift = 50.00; break;

case ‘g’: annual_int_rate = 0.025; opening_gift = 100.00; break;

case ‘p’: annual_int_rate = -0.07; opening_gift = 150.00; break;

default: annual_int_rate = 0.0; opening_gift = 50.00; } 3d). Here is a poor use of switch. How can we code this better?

This is the situation of a person going to a movie theatre.

printf(“ Enter age of client: “);

scanf( “%d”, &age);

switch (age) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: printf(“You may attend any ‘G’ movie\n”); break;

case 13: case 14: case 15: case 16: printf(“You may attend any ‘G’ movie\n”); printf(“You may also attend any ‘PG-13’ movie\n”); break;

case 17: case 18: case 19: printf(“You may attend any ‘G’ movie\n”); printf(“You may also attend any ‘PG-13’ movie\n”); printf(“You may also attend ‘R’ movies\n”); break;

default: printf(”You may edify yourself with any wholesome movie or \n”); printf (“watch whatever garbage you desire \n”); } 3e). What is a better way to code the above situation?

3f). Summary of steps taken by the switch statement.

1) evaluate the expression.

2) search to find matching case. (must be a constant, range or list not allowed)

3) if a match is found, execute the instructions until…

- a break statement is reached - or a default statement is reached - or a } brace ending the switch statement is reached.

At that point go to the statement following the switch statement.

4) If there is no match and there is a default code block, perform the default code block.

5) if there is no match and there is no default code block, exit the switch statement and continue program execution with the first statement following the switch statement.

Notes:

1) The default section is optional. 2) break is needed to prevent one case from running into an other. 3) no code whatsoever will be performed if the is no match and there is no default section.

3g). Discussion Questions:

Is switch preferable to a nested if ?

Readability Switch / Case is usually better.

Power: If / Else / Else If using ANDs/ ORs is usually better.

Flexibility: If / Else / Else If using ANDs/ ORs is usually better.