Professional Documents
Culture Documents
Problem Solving and Program Design in C: (5th Edition) by Jeri R. Hanly and Elliot B. Koffman
Problem Solving and Program Design in C: (5th Edition) by Jeri R. Hanly and Elliot B. Koffman
(5th Edition)
CP 202
Chapter 4
CHAPTER 4 – Outline 1/2
Simple Logic Expression
often need to look at data values and make choices in
programs
logical expressions are true/false statements of data
relationships
simple logical expressions are of the form: (data operator
data)
data terms are either variables or constants
operator terms are either relational or equality operators
Simple Logic Expression
For example:
let a = 17 and b = 42
LESS THAN:
(a < b) is true
LARGER THAN:
(a > b) is false
LESS THAN OR EQUAL:
(a <= b) is true, (a < b) is false, (a > b) is false
LARGER THAN OR EQUAL:
(a >= b) is false
EQUAL TO:
(a == b) is false
NOT EQUAL TO:
(a != b) is true
Complex Logic Expression
can combine expressions to get complex logical
expressions
useful for more realistic data comparison tasks
syntax of the form: (expression operator expression)
expression is a simple logical expression
operator is a logical operator
Complex Logic Expression:
AND &&
Introduction:
Think about a light with two switches. If the two switches are
ON, then the light will be ON. Otherwise, the light will be OFF.
Logic Result
Expression
TRUE && TRUE TRUE
TRUE && FALSE
FALSE
FALSE && FALSE
TRUE
FALSE && FALSE
FALSE
Example:
let x = 3.14 and y = 7.89
((x < 4) && (y < 8)) is true (because both halves are true)
((x > 3) && (y > 8)) is false (because second half is false)
Complex Logic Expression:
OR ||
Introduction:
Think about a light with two switches. If the one of the two
switches are ON, then the light will be ON.
Logic Result
Expression
TRUE || TRUE TRUE
TRUE || FALSE TRUE
FALSE || TRUE TRUE
FALSE || FALSE FALSE
Example:
let x = 3.14 and y = 7.89
((x < 4) || (y > 8)) is true (because first half is true)
((x < 3) || (y < 8)) is true (because second half is true)
((x > 4) || (y > 8)) is false (because both halves are false)
Complex Logic Expression:
NOT !
Introduction:
Logic Result
Expression
! TRUE FALSE
! FALSE TRUE
Example:
let a = 7 and b = 3
(a > b) is true !(a > b) is false
(a <= b) is false !(a <= b) is true
(a == b) is false !(a == b) is true
(a != b) is true !(a != b) is false
Evaluation Tree (Step-by-Step)
( ( x < 5 ) || ! ( y > 9) ) (x=4; y=5)
4 5 5 9
< >
T ! F
!
T
||
T
CHAPTER 4 – Outline 2/2
IF Statements
control the flow of your program
based on True or False
allow selecting an action based upon the user's input
three types of the IF statements:
IF:
allow the flow of the program to be changed
IF-ELSE:
gives an alternative path to be executed if the IF statement
condition is False.
Nested IF:
you will see a group of IF statements that each checks one
condition within another.
IF Statements
1 1 1
2 2 2
3 Conditio T F Conditio T
n n
3 4 3
4
5 4 5
6 5 6
7 6 7
Sequential IF IF / ELSE
IF Statements:
IF
Introduction:
can selectively execute code using if statement
when logical expression is true, selected code is executed
when logical expression is false, selected code is skipped
selected code can be either a single statement or a block of
statements which is enclosed in { } characters
should indent code to aid program readability
you only need semi-colon after single statements, not after { }
IF Statements:
IF
Prototype:
If you have only one statement after an if statement, you do
not need to put the statement in braces. For example:
if ( TRUE )
// Execute the next statement
if ( TRUE )
{
// Execute all statements inside the braces
}
IF Statements:
IF
Example:
scanf(“%d”, &a);
scanf(“%d”, &b);
if (b > a)
printf ("B is larger than A\n“);
printf (“Done...\n“);
scanf(“%d”, &a);
scanf(“%d”, &b);
if (a < b)
{
printf("A is smaller than B\n“);
printf("The difference is %d\n“, b–a);
}
printf (“Done...\n“);
if (a < b)
{
b = a * 3;
a = b 1;
}
else
a = b + 5;
Question: What are the values of the two variables a and b if:
2. a = 10, b = 5
3. a = 5, b = 10
IF Statements:
ELSE IF
Prototype:
if ( condition 1 )
{
// A. Execute these statements F T
Cond.1
// if condition 1 is TRUE
}
else F T
Cond.2 A
if ( condition 2 )
{
The same
// B. Execute these statements F T
// if condition 2 is TRUE Cond.3 B
}
else if ( condition 3 ) D C
{
// C. Execute these statements
// if condition 3 is TRUE
}
else
{
// D. Execute these statements
// if condition 1, 2, 3 and 4 are FALSE
}
IF Statements:
ELSE IF
Example:
printf(“Enter you grade for the course: ”);
scanf(“%d”, &grade);
if (grade >= 90)
printf(“GPA = A\n”);
else if (grade >= 80)
printf(“GPA = B\n”);
else if (grade >= 70)
printf(“GPA = C\n”);
else if (grade >= 60)
printf(“GPA = D\n”);
else
{
printf(“GPA = F\n”);
printf(“BAD STUDENT.\n”);
}
if (a > 0)
{
if (b < 0)
{
a = 3 * b;
c = a + b;
}
}
else
{
a = 2 * a;
c = b / a;
}
// print command prompt
printf("Enter command number:\n“);
printf(" 0 quit\n");
printf(" 1 deposit money\n");
printf(" 2 withdraw money\n");
printf(" 3 print balance\n");
scanf("%d", &command);
// handle banking command
switch (command)
{
case 0: // quit code
printf("See you later!\n");
break;
case 1: // deposit code
printf("Enter deposit amount: ");
scanf("%d", &money);
balance = balance + money;
break;
case 2: // withdraw code
printf("Enter withdraw amount: ");
scanf("%d", &money);
balance = balance money;
break;
case 3: // print code
printf("Current balance = %d", balance);
break;
default:
printf("Error\n");
}
Common Programming Errors
if (0 <= x <= 4)
printf(“Condition is true.\n”);
if (x = 10)
printf(“x is 10.\n”);
if (x > 0)
sum = sum + x;
printf(“Greater than zero.\n”);
else
printf(“Less than or equal to zero.\n”);
Conclusion