Title: CS101 Lecture 12
1Lecture 12
2What will I learn in this lecture?
- 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 FER Chapter 3.6 6.3
3C Problem Example
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 Outputreal number 3. Develop
Algorithm (processing steps to solve problem)
4C Problem Example
Print enter value
Flowchart
Read value
False
True
value gt 0.0
Print sqrt(value)
5C Problem Example
/ C Program to compute the square root of a
positive number / include ltstdio.hgtinclude
ltmath.hgt 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 gt 0.0) printf("square_root(lf) lf
\n", value , sqrt(value))
6Selection Structures (Decision Statements)
if Selection Structure
if(expression) statement
- if expression evaluates to true, the statement
is executed otherwise execution passes to the
next statement in the program.
/ Error! Dont put semicolon here / / This is
an example of a logical error /
if(value gt 0.0) printf("square_root(lf) lf
\n", value,sqrt(value))
7C Problem Example (modified)
1. Problem Definition Modify the previous
program to notify the user when the input is
invalid.
8C Problem Example (modified)
Print enter value
Flowchart
Read value
False
True
value gt 0.0
Print sqrt(value)
Print invalid input
9C Problem Example (modified)
/ C Program to compute the square root of a
positive number / include ltstdio.hgtinclude
ltmath.hgt 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 gt 0.0)
printf("square_root(lf) lf \n",
value,sqrt(value)) else printf("invalid
user input, please enter non-negative value\n")
10Math Library in C
- 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. gt gcc
file.c -lm
See next page
11 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 gt x floor (x) - largest integer lt
x
12if - else Selection Structure
if (expression) statement1 else statement2
- if expression evaluates to true, statement1 is
executed and execution skips statement2 - If expression evaluates to false, execution skips
statement1 , statement2 is executed
13if - else Selection Structure
We can also execute multiple statements when a
given expression is true
if (expression) statement1 statementn e
lse statement1 statementm
if (expression) statement1 statement2 stat
ementn
or
. ..
. ..
Example -
(what does this code do?)
if(b lt a) temp a a b b temp
. ..
14C Problem Example
1. Problem Definition Modify the previous program
to compute the followingYou must check that
the value is legal, i.e. value gt 1.0 or value
lt -1.0
15C Problem Example
Print enter value
Flowchart
Read value
value gt 1.0 or value lt -1.0
False
True
Print sqrt(valuevalue -1.0)
Print invalid input
16C Problem Example (modified)
/ Compute the square root of valuevalue-1.0
/ include ltstdio.hgtinclude ltmath.hgt void
main(void) double value / Declare
variables. / / request user input/
printf("Please enter value gt 1.0 or lt -1.0
") scanf("lf", value) / read value /
/ Output the square root. / if ((value gt
1.0) (value lt -1.0)) printf("square_root(
f) f \n", value,sqrt(valuevalue - 1.0))
else printf("invalid user
input\n") printf("input should be a value
gt 1.0 or lt -1.0 \n")
17Logical Expressions (Relational Operations)
In logical expressions (which evaluate to true or
false), we can use the following Relational
operators
Relational Operator
Type of Test
equal to (dont use ) !
not equal to gt
greater than gt greater than or equal
to lt less than lt
less than or equal to
18Logical Operators-Truth Tables
19Logical Expressions
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 1
if ( 5 ) printf("True") / prints True /
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?/
20Logical Expressions
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) lt 1.0e-10 ) printf("equal!\n")
/ prints equal! / else printf(" not
equal!\n")
21Nested if - else Selection Structure
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 by9 - 10
A7 - 8 B5 - 6 C3 - 4
Dlt 3 F 2. Refine, Generalize,
Decompose the problem definition (i.e., identify
subproblems, I/O, etc.) Input integer
score Outputcharacter grade 3. Develop
Algorithm (processing steps to solve problem)
22C Problem Example
Print enter score
Flowchart
Read score
score 10 score 9
True
False
Print A
(continued on next slide)
(skip else part of statement)
23C Problem Example
False
score 8 score 7
False
True
Print B
(continued on next slide)
(skip else part of statement)
24C Problem Example
False
score 6 score 5
False
True
Print C
(continued on next slide)
(skip else part of statement)
25C Problem Example
False
score 4 score 3
False
True
Print D
Print F
26C Problem Example
/ C Program to compute the letter grade for a
quiz. / include ltstdio.hgt 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 /
27C Problem Example
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") else if
((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.
28switch Selection Structure
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 score score 9,10 print
A score 7,8 print B score 5,6
print C score 3,4 print D otherwise
print F
29C Problem Example
/ C Program to compute the letter grade for a
quiz. / include ltstdio.hgt 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 /
30C Problem Example
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 /
31switch Selection Structure
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 defau
lt_statement(s)
(controlling expr. must evaluate to an integer or
a character value each case should end with a
break stmt.)
. ..