Computers in Engineering - 308-208 Lecture 16
Computers in Engineering - 308-208

Lesson 16 - Learning Goals

16.3 Learn how to use the C break

SWITCH STATEMENT :

```          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;
}

```

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

• 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 )

else

```

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 ;
```

On to the next lecture