Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Problem Solving and Program Design in C

(5th Edition)

by Jeri R. Hanly and Elliot B. Koffman

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 

 To have more than one statement execute after an if


statement that evaluates to true, use braces. Anything inside
braces is called a compound statement, or a block. For
example: 

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“);

Question: Draw the flowchart for the two programs?


IF Statements:
IF-ELSE
 Introduction:
 often need two alternatives in an if statement
 want to execute certain code if expression is true
 want to execute different code if expression is false
 the if-else statement lets you do this
 can use single statement or block of code for either part
 should indent code to aid program readability
 Prototype:
if ( TRUE ) 

   // Execute these statements if TRUE 

else 

   // Execute these statements if FALSE 

IF Statements:
IF-ELSE
 Example:

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”); 
}

Question: Draw the flowchart for this part of the program?


IF Statements:
Nested IF
 Introduction:
 often need to check one condition within another
 can nest if statements to accomplish this
 need to take care when matching up { } brackets
 use indentation to reflect nesting and aid readability
IF Statements:
Nested IF
 Prototype:
if ( Boolean expression 1 ) 
 { 
   // execute if expression 1 is true 
   if ( Boolean expression 2 ) 
   { 
      // execute if expression 2 is true 
   } 
   else 
   { 
      // execute if expression 2 is false 
      if ( Boolean expression 3 ) 
      { 
         // execute if expression 3 is true 
      } 
   } 

else 

   // execute if expression 1 is false 
   if ( Boolean expression 4 ) 
   { 
      // execute if expression 4 is true 
   } 
}
IF Statements:
Nested IF
 Prototype:
if ( Boolean expression 1 )  F T
Exp. 1

   // A. execute if expression 1 is true  E A
   if ( Boolean expression 2 ) 
   { 
      // B. execute if expression 2 is true 
   }  T F T
Exp. 4 Exp. 2
   else 
   {  C B
      // C. execute if expression 2 is false  F
      if ( Boolean expression 3 ) 
      { 
         // D. execute if expression 3 is true  T
      } 
Exp. 3
   } 

else  D

   // E. execute if expression 1 is false 
   if ( Boolean expression 4 ) 
   { 
      // F. execute if expression 4 is true 
   } 
}
IF Statements:
Nested IF
 Example:

if (a > 0) 

   if (b < 0) 
   { 
      a = 3 * b; 
      c = a + b;
   }

else

   a = 2 * a; 
   c = b / a;

Question: Draw the flowchart for this part of the program?


Switch
 Introduction:
 switch statement convenient for handling multiple if-else cases
 need to use single value as decision variable
 need to identify code to be executed for each case
 it is essential to end each case with break command
 can use default for all cases not specifically labeled
 Prototype:
switch ( decision value ) 

   case label1 : statements; 
      break; 
   case label2: statements; 
      break; 
   ... 
   default: statements; 
}
Switch
(Example)
int command; 
int money; 
int balance = 0; 

// 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

You might also like