12-2 know how if and switch c statements control the sequence of execution of statements. be able to...

31

Upload: richard-lloyd

Post on 23-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional
Page 2: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-2

• Know how if and switch C statements control the sequence of execution of statements.

• Be able to use relational and logical operators in the conditional part of an if or a switch statement.

• Related Chapters: ABC Chapter 4.1-4.7 & 4.16

Page 3: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-3

1. Problem Definition

Write a program that reads a number and computes the square root if the number is non-negative.

2. Refine, Generalize, Decompose the problem definition

(i.e., identify subproblems, I/O, etc.)

Input = real number

Output=real number

3. Develop Algorithm

(processing steps to solve problem)

Page 4: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-4

Flowchart

Print “enter value”

Read value

value >= 0.0

Print sqrt(value)

TrueFalse

Page 5: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-5

/* C Program to compute the square root of a positive number */

#include <stdio.h>#include <math.h>

void main(void)

{

double value; /* Declare variables. */

/* request user input */

printf("Please enter a non-negative number :");

/* read value */ scanf("%lf", &value);

/* Output the square root. */ if (value >= 0.0)

printf("square_root(%lf) = %lf \n", value , sqrt(value));

}

Page 6: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-6

if(expression)

statement;

• if expression evaluates to true, the statement is executed; otherwise execution passes to the next statement in the program.

• if Selection Structure

if(value >= 0.0);printf("square_root(%lf) = %lf \n", value,sqrt(value));

/* Error! Don’t put semicolon here *//* This is an example of a logical error */

Page 7: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-7

1. Problem Definition

Modify the previous program to notify the user when the input is invalid.

Page 8: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-8

Flowchart

Print “enter value”

Read value

value >= 0.0

Print sqrt(value);

TrueFalse

Print “invalid input”

Page 9: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-9

/* C Program to compute the square root of a positive number */

#include <stdio.h>#include <math.h>

void main(void)

{

double value; /* Declare variables. */ /*request user input*/

printf(”Please enter a non-negative number :”);

scanf("%lf", &value); /* read value */

/* Output the square root. */ if (value >= 0.0) printf("square_root(%lf) = %lf \n", value,sqrt(value)); else printf("invalid user input, please enter non-negative value\n");

}

Page 10: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-10

- in header file math.h

Arguments (parameters) for each of the following functions are assumed to be of type double. If not, a type double copy is made for use in the function. To compile a program that contains math functions you need to use the -lm (Lm not 1m )option for gcc.

> gcc file.c -lm

See next page

Page 11: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

fabs (x) - |x| (not the same as the abs(x) function)

sqrt (x) - square root of x

pow (x, a) - xa

exp (x) - ex (e = 2.718281828 …)

log (x) - ln x = loge x

log10 (x) - log10 x

sin (x) - sine function (x in radians)

cos (x) - cosine function (x in radians)

tan (x) - tangent function (x in radians)

ceil (x) - smallest integer >= x

floor (x) - largest integer <= x

Page 12: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-12

if (expression)statement1;

elsestatement2;

-if expression evaluates to true, statement1 is executed and execution skips statement2

-If expression evaluates to false, execution skips statement1 , statement2 is executed

Page 13: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-13

We can also execute multiple statements when a given expression is true:

if (expression)

{statement1;statement2;

statementn;}

Example -if(b < a){

temp = a;a = b;b = temp;

}

or

...

if (expression) {

statement1;

statementn;}else { statement1;

statementm;}

(what does this code do?)

...

...

Page 14: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-14

1. Problem Definition

Modify the previous program to compute the following:

You must check that the value is legal, i.e. value >= 1.0 or value <= -1.0

0.12 value

Page 15: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-15

Flowchart

Print “enter value”

Read value

value >= 1.0 or value <= -1.0

Print sqrt(value*value -1.0);

TrueFalse

Print “invalid input”

Page 16: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-16

/* Compute the square root of value*value-1.0 */

#include <stdio.h>#include <math.h>

void main(void)

{

double value; /* Declare variables. */ /* request user input*/ printf("Please enter value >= 1.0 or <= -1.0 :"); scanf("%lf", &value); /* read value */

/* Output the square root. */ if ((value >= 1.0) || (value <= -1.0)) printf("square_root(%f) = %f \n", value,sqrt(value*value - 1.0)); else { printf("invalid user input\n"); printf("input should be a value >= 1.0 or <= -1.0 \n");

}}

Page 17: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-17

In logical expressions (which evaluate to true or false), we can use the following Relational operators:

RelationalOperator

Type of Test

== equal to (don’t use =)

!= not equal to

> greater than

>= greater than or equal to

< less than

<= less than or equal to

Page 18: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-18

A B A && B

A || B

TRUE TRUE TRUE TRUE

TRUE FALSE FALSE TRUE

FALSE TRUE FALSE TRUE

FALSE FALSE FALSE FALSE

A !A

TRUE FALSE

FALSE TRUE

Page 19: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-19

if ( 5 ) printf("True"); /* prints True */

In C the value for False is represented by the value zero and True is represented by any nonzero value. The value False can be any zero value such as the number 0 or 0.0 or null character ‘ \0 ’ or the NULL pointer.

Example 2:

int x = 0; /* x declared as an integer variable */ /* and initialized to the value 0 */

if (x = 0) /* note the error, == should be used */ printf(" x is zero\n"); /*message not printed, why?*/

Example 1:

Page 20: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-20

Avoid using == to test real numbers for equality!

Example

double x = .3333333333; /* ten digits of 3s */

if (x == 1.0/3.0) printf("equal!\n");else printf(" not equal!\n"); /* prints not equal! */

if ( fabs(x - 1.0/3.0) < 1.0e-10 ) printf("equal!\n"); /* prints equal! */

else printf(" not equal!\n");

Page 21: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-21

1. Problem Definition

Write a program that returns a letter grade based on a quiz score. The input will be the integer score from a 10 point quiz. The letter grades are assigned by:9 - 10 “A”7 - 8 “B”5 - 6 “C”3 - 4 “D”< 3 “F”

2. Refine, Generalize, Decompose the problem definition

(i.e., identify subproblems, I/O, etc.)

Input = integer score

Output=character “grade”

3. Develop Algorithm

(processing steps to solve problem)

Page 22: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-22

Flowchart

Print “enter score”

Read score

score == 10 || score == 9

Print “A”

TrueFalse

(continued on next slide)

(skip else part of statement)

Page 23: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-23

False

score == 8 || score == 7

Print “B”;

True

(skip else part of statement)

(continued on next slide)

False

Page 24: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-24

False

score == 6 || score == 5

Print “C”;

True

(skip else part of statement)

(continued on next slide)

False

Page 25: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-25

False

score == 4 || score == 3

Print “D”

TrueFalse

Print “F”

Page 26: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-26

/* C Program to compute the letter grade for a quiz. */

#include <stdio.h>

void main(void)

{

int score; /* Declare variables. */

/* request user input */ printf("Please enter a score :"); scanf("%i", &score); /* read value */

/* Output the grade *//* continued on the next slide */

Page 27: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-27

if ((score == 10) || (score == 9)) printf("A\n");else if ((score == 8) || (score == 7))

printf("B\n"); else

if ((score == 6) || (score == 5))printf("C\n");

elseif ((score == 4) || (score == 3))

printf("D\n");else

printf("F\n");

} /* end of program */

Unless { } are used the else matches the first if in the code above.

Page 28: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-28

1. Problem Definition

Redo the previous problem by using a switch statement rather than the nested if-else statement.Pseudo-code Algorithm

print “enter score”

read score

switch scorescore = 9,10 print “A”score = 7,8 print “B”score = 5,6 print “C”score = 3,4 print “D”

otherwise print “F”

Page 29: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-29

/* C Program to compute the letter grade for a quiz. */

#include <stdio.h>

void main(void)

{

int score; /* Declare variables. */

/* request user input */

printf("Please enter a score :");

scanf("%i", &score); /* read value */

/* Output the grade */ /* continued on the next slide */

Page 30: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-30

switch (score) {

case 10: case 9: printf("A\n");

break; case 8: case 7: printf("B\n");

break; case 6: case 5: printf("C\n"); break; case 4: case 3: printf("D\n"); break;

default: printf("F\n"); } /* end of switch */

} /* end of program */

Page 31: 12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional

12-31

The switch structure can be used when we have multiple alternatives based on a value of type int or type char (but not float or double). This structure has the general form:

switch (controlling expression) {

case label1:label1_statement(s);

case label2:label2_statement(s);

default:default_statement(s);

}

(controlling expr. must evaluate to an integer or a character value; each case should end with a break stmt.)...