LECTURE 16

SWITCH STATEMENT :

• Use a switch statement when one of several actions is taken depending on a particular value:
```switch(expression)
{
case value :
statement;
break;

.
.
.
case value :
statement;
break;

default :

statement;
break;

}

```
• expression must be char, int, short or long.
• value must be a compatible constant expression.
• Braces not needed for one compound statements.
• default covers anything not covered by a case.
• Use break to exit.

SWITCH EXAMPLE

```switch (user_input)
{

/* A simple four functions calculator */

case '+':
c = a + b;
break;
case '-':
c = a - b;
break;
case '*':
case 'X':
case 'x':
c = a * b;
break;
case '/':
c = a / b;
break;
default:
printf ("Illegal keystroke!\n");
break;
}

```

ADVANTAGES OF SWITCH :

• Easier to read than equivalent if-else statement
• More efficient than equivalent if-else statement (destination can be computed by looking up in table)

DISADVANTAGES :

• Doesn't work with floats, strings, etc.
• Doesn't work with ranges (unless explicitly enumerated)
• Doesn't work with variable conditions

CONTINUE STATEMENT IN A WHILE LOOP

GENERAL CASE

Is logical expression_1 true?

• If YES :

Insertion of if structure

if (expression_2) continue ;

/* Then, is logical expression_1 true? (back to beginning) */

else statement ;

/* Then, is logical expression_1 true? (back to beginning) */

• If NO :

exit the loop.

Use of the continue statement

EXAMPLE

```/* function to count letters in a string */

void char_count ( char *s, int counts[ ] )
{
/* loop through entire string */

for ( ; *s != '\0' ; s++ )
{
if ( !isalpha(*s)) continue ; /* if not a letter, continue loop */
else
{
if ( isupper(*s))
counts [*s - 'A']++ ;
else
counts [ *s - 'a ']++ ;
}
}
}

```

BREAK OUT OF WHILE LOOP

GENERAL CASE

Is logical expression_1 true?

If YES :

Insertion of if structure

if (expression_2) break ;

/* break out of the loop */

else statement ;

/* Then, is logical expression_1 true? (back to beginning) */

…

If NO :

exit the loop.

Use of break statement

EXAMPLE

```#include <stdio.h>

main ( )
{
char id [ 10 ] ;
int digit, spaces, number ;

while ( scanf (''%s'', id ) = = 1 )
{
if ( strlen (id) != 7 )
{
printf (''%s is not a proper number - I quit ! \n'', id ) ;
break ;
}
for ( digit = 0 ; digit < 7 ; digit += 1 )
{
for ( spaces = 1 ; spaces < = digit ; spaces += 1 )
{
putchar (' ') ;
}
number = id [ digit ] - '0' ;
printf (''%1d \n'', number) ;
}
}
}

```

General Form of a Conditional Expression

TRUE     FALSE

expression_1 ? expression_2 : expression_3 ;

Use of conditional operator and the equivalent if statement

/* sample use of conditional operator */

answer = ( A < B ) ? A : B ;

/* equivalent of conditional operator using logical if statement */

if ( A < B )

answer = A ;

else

answer = B ;

Use of statement Labels and the goto statement

EXAMPLE :

multiply_by_two : /* Label */

a = 2*a ;

other statements ;

.

.

.

goto multiply_by_two ; /* Go back to multiply_by_two label */

Examples of Array Initialization

int grade [ 6 ] = { 65, 55, 70, 65, 95 }

float sales_in_million [ 12 ] = { 50.5 , 12.8 , 7.4 , 9.9 , 46.9 }

char month [ 12 ] = {

''JAN'',''FEB'',''MAR'',''APR'',''MAY'',''JUN'',

''JUL'',''AUG'',''SEP'',''OCT'',''NOV'',''DEC'' }

Example showing equivalent string initializations

char str [ ] = { 'i','n','i','t','i','a','l','i','z','a','t','i','o','n','\0' }

/* simpler initialization of a string */

char str [ ] = ''initialization'' ;

Example showing array advantage on regular variables

Averaging 10 floats using regular variables

float x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 ;

float average = 0.0 ;

average = ( x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 ) / 10.0 ;

Now computing the average of 500 floats using an array

int i ;

float x [ 500 ] ;

float average = 0.0 ;

for ( i = 0 ; i < 500 ; i++ ) average + = x[i] ;

average = average / 500 ;

Go back to lecture menu

Go back to main page

Copyright © 1996 McGill University. All rights reserved.