Download presentation
Presentation is loading. Please wait.
1
Chapter 4 (Conditional Statements)
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS 202 1431/1432 – Term 1
2
CHAPTER 4 - Conditional Statements
# CHAPTER 4 - Conditional Statements Simple Logic Expression Complex Logic Expression Evaluation Tree Conditional Statements IF Statement Switch Statement Common Errors in Conditions ATM Simulation 1 ATM Simulation 2 column shows the topics index. column shows the programs index.
3
Simple Logic Expression
1 Simple Logic Expression Introduction often need to look at data values and make choices logical expressions are true/false statements of data relationships Prototype variables or constants relational or equality variables or constants data operator data Simple Logic Expression Operator Less than < Larger than > Less than or equal <= Simple Logic Expression Operator Larger than or equal >= Equal == Not equal !=
4
Simple Logic Expression
1 Simple Logic Expression Example let a = 17 and b = 42 (a < b) True (a > b) False (a <= b) True (a >= b) False (a == b) False (a != b) True Statement has ! may confuse you, so read it without ! and flip the result
5
Complex Logic Expression
2 Complex Logic Expression Introduction combining more than one simple logic expressions make it a complex logical expression Prototype expression operator expression AND && OR || NOT ! Logic Expression Result TRUE && TRUE TRUE TRUE && FALSE FALSE FALSE && TRUE FALSE && FALSE Logic Expression Result TRUE || TRUE TRUE TRUE || FALSE FALSE || TRUE FALSE || FALSE FALSE Logic Expression Result ! TRUE FALSE ! FALSE TRUE Imagine 1 light with 2 switches; the operator between them is &&, ||, or !
6
Complex Logic Expression
2 Complex Logic Expression Example let x = 3.14 and y = 7.89 ((x < 4)&&(y < 8)) True (both halves are true) ((x > 3)&&(y > 8)) False (second half is false) ((x < 4)||(y > 8)) True (first half is true) ((x < 3)||(y < 8)) True (second half is true) ((x > 4)||(y > 8)) False (both halves are false) !(x < y) False !(x >= y) True !(a == b) True !(a != b) False
7
Evaluation Tree (Step-by-Step)
3 Evaluation Tree (Step-by-Step) Introduction a way to solve a logic expression Way to Do solve the simple logic expression first Example ((x < 5)&& !(y > 9)) (x=4 ; y=5) T < > F T ! && T
8
Evaluation Tree (Step-by-Step)
3 Evaluation Tree (Step-by-Step) Introduction a way to solve a logic expression Way to Do solve the simple logic expression first Example ((x < 5)&& !(y > 9)) (x=4 ; y=5) TRUE T T F SECOND way
9
Conditional Statements
4 Conditional Statements Overview control the flow of your program based on True or False allow selecting an action based on condition two types of the conditional statements available in C: if statements Switch statements
10
Conditional Statements – IF Statements
Overview three types of the IF statements: IF (allows the flow of the program to be changed if the IF statement condition is True) IF-ELSE (gives an alternative path to be executed if the IF statement condition is False) Nested IF (IF statements insides IF statements)
11
Conditional Statements – 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 no need for semi-colon after IF statements 1 2 3 Condition 5 6 7 4 T
12
Conditional Statements – IF Statements (IF)
Prototype If you have only one statement after an if statement, you do not need to (but you can) put the statement in braces. For example: To have more than one statement execute after an if statement that evaluates to true, use braces. For example: 1 2 Condition T 3 2. 3. 4. if ( TRUE ) Statement executes if condition is True Statement executes if condition is True/False 4 1 2 Condition T 2. 3. 4. 5. 6. if ( TRUE ) { All the statements in this block execute if the condition is True } 3 4 5 Statements between braces are called a compound statement, or a block
13
Conditional Statements – IF Statements (IF)
Example 1-2 1. 2. 3. 4. 5. scanf(“%d”, &a); scanf(“%d”, &b); if (b > a) printf ("B is larger than A\n“); printf (“Done...\n“); 3 T 4 5 1. 2. 3. 4. 5. 6. 7. 8. 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“); 1-2 3 T 5-6 8 In a diagram, you can merge the sequential statements in one block
14
Conditional Statements – 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 1 2 3 Condition F T 4 5 6 7 8
15
Conditional Statements – IF Statements (IF-ELSE)
Prototype if ( condition 1 ) { // A. Execute these statements // if condition 1 is TRUE } else if ( condition 2 ) { // B. Execute these statements // if condition 2 is TRUE } else if ( condition 3 ) { // C. Execute these statements // if condition 3 is TRUE } else { // D. Execute these statements // if condition 1, 2 and 3 are FALSE } Cond.1 F A T Cond.2 F Cond.3 B T F D C T The same The compiler looks at the entire program in one line unless there is ;
16
Conditional Statements – IF Statements (IF-ELSE)
Example 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. printf(“Enter the 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(“UNSATISFACTORY.\n”); } Quiz: draw the flow diagram for this segment?
17
Conditional Statements – 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
18
Conditional Statements – IF Statements (Nested IF)
Prototype if (expression 1) { // A. statements if (expression 2) { // B. statements } else { // C. statements if (expression 3) { // D. statements } } } else { // E. statements if (expression 4) { // F. statements } } Exp. 1 T F E Exp. 4 F T A Exp. 2 T F C Exp. 3 D T B
19
Conditional Statements – IF Statements (Nested IF)
How many PATHs you have? 1, 3, 5, 8 1, 3, 5, 7, 9, 10 1, 3, 5, 7, 9 1, 2, 4, 6 1, 2, 4 1 F T 2 3 4 5 T F T 7 8 6 9 T 10
20
Conditional Statements – IF Statements (Nested IF)
Example 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. if (a > 0) { if (b < 0) { a = 3 * b; c = a + b; } } else { a = 2 * a; c = b / a; } Quiz: draw the flow diagram for this segment?
21
Conditional Statements – IF Statement
Conclusion 1 1 1 2 2 2 3 Condition Condition T F T 3 4 3 4 5 4 5 6 5 6 7 6 7 Sequential IF IF / ELSE In a diagram, you can merge the sequential blocks in one block
22
P1 ATM Simulation 1 Write a program that simulates an ATM, where they are three main options: Deposit Money Withdraw Money Print Balance Assume the balance in the account is Zero Use if to choose an option from the Main Menu
23
ATM Simulation 1 P1 Input Output Formula An option from the main menu
Amount of money to deposit if the user choose option 1 Amount of money to withdraw if the user choose option 2 Output The balance if the user choose option 3 Formula Balance = Balance + Deposit if the user choose option 1 Balance = Balance – Withdraw if the user choose option 2 Think about the input/formula/output for each option in the main menu
24
ATM Simulation 1 P1 Initial the balance balance = 0
Ask the user to choose one of the 3 options command Deposit Money Get the amount of money that wants to deposit money Calculate the balance balance = balance + money Withdraw Money balance = balance - money Display the balance balance 1 2 3
25
P1 ATM Simulation 1 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include <stdio.h> int main(void) { int command; // Input: an option from the main menu int money; // Input: withdraw or deposite money int balance; // Output: Display the balance /* 1. Initial the balance */ /* 2. Ask the user to choose one of the 3 options */ if (command == 1) /* 2.1 Deposit Money */ } else if (command == 2) /* 2.2 Withdraw Money */ else if (command == 3) /* 2.3 Print Balance */ return(0);
26
ATM Simulation 1 P1 1. #include <stdio.h> 2. 3. int main(void)
{ int command; // Input: an option from the main menu int money; // Input: withdraw or deposite money int balance; // Output: Display the balance /* 1. Initial the balance */ balance = 0; /* 2. Ask the user to choose one of the 3 options */ printf(" Main Menu\n"); printf(" \n"); printf(" 1 - Deposit money\n"); printf(" 2 - Withdraw money\n"); printf(" 3 - Print balance\n"); printf("Enter command number: "); scanf("%d", &command); if (command == 1) /* 2.1 Deposit Money */ printf("Enter deposit amount: "); scanf("%d", &money); balance = balance + money; } else if (command == 2) /* 2.2 Withdraw Money */ printf("Enter withdraw amount: "); balance = balance - money; else if (command == 3) /* 2.3 Print Balance */ printf("Current balance = %d\n", balance); return(0); 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39.
27
P1 ATM Simulation 1 Useless program because it doesn’t have a loop to see the new balance
28
P1 ATM Simulation 1 Validation Check: If a user put an invalid input, you need to display an error message Maintain the program to validate the inputs? input has to be 1,2,3 only
29
ATM Simulation 1 P1 #include <stdio.h> int main(void) {
int command; // Input: an option from the main menu int money; // Input: withdraw or deposite money int balance; // Output: Display the balance /* 1. Initial the balance */ balance = 0; /* 2. Ask the user to choose one of the 3 options */ printf(" Main Menu\n"); printf(" \n"); printf(" 1 - Deposit money\n"); printf(" 2 - Withdraw money\n"); printf(" 3 - Print balance\n"); printf("Enter command number: "); scanf("%d", &command); if (command == 1) /* 2.1 Deposit Money */ printf("Enter deposit amount: "); scanf("%d", &money); balance = balance + money; } else if (command == 2) /* 2.2 Withdraw Money */ printf("Enter withdraw amount: "); balance = balance - money; else if (command == 3) /* 2.3 Print Balance */ printf("Current balance = %d\n", balance); else /* Otherwise, display an error */ printf("Error choice...\n"); return(0); 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42.
30
P1 ATM Simulation 1
31
Conditional Statements – Switch Statement
4b Conditional Statements – Switch Statement Introduction switch statement convenient for handling multiple if-else cases need to use single value as decision variable (called: controlling expression) of type: int or char, but not of type double 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; }
32
Conditional Statements - Switch Statement
4b Conditional Statements - Switch Statement Examples switch (command) { case 1: printf(“Light is ON.\n"); break; case 2: printf(“Light is OFF.\n"); default: printf("Error Option!\n"); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 3 4 T F 6 7 10 switch (command) { case 1: printf(“Light is ON.\n"); break; case 2: printf(“Light is OFF.\n"); default: printf("Error Option!\n"); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 3 4 T F 6 7 10 switch (command) { case 1: printf(“Light is ON.\n"); break; case 2: printf(“Light is OFF.\n"); default: printf("Error Option!\n"); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 3 4 T F 6 7 10 32
33
P2 ATM Simulation 2 Write a program that simulates an ATM, where they are three main options: Deposit Money Withdraw Money Print Balance Assume the balance in the account is Zero Use if to choose an option from the Main Menu Validate the input; if a user choose a wrong option, display an error message As same as Program 1, but use Switch statement instead of IF
34
ATM Simulation 2 P2 Input Output Formula An option from the main menu
Amount of money to deposit if the user choose option 1 Amount of money to withdraw if the user choose option 2 Output The balance if the user choose option 3 An error message if the user choose a wrong option Formula Balance = Balance + Deposit if the user choose option 1 Balance = Balance – Withdraw if the user choose option 2 Think about the input/formula/output for each option in the main menu
35
ATM Simulation 2 P2 Initial the balance balance = 0
Ask the user to choose one of the 3 options command Deposit Money Get the amount of money that wants to deposit money Calculate the balance balance = balance + money Withdraw Money balance = balance - money Print Balance Display the balance balance Display an error message 1 2 3 Error Option
36
P2 ATM Simulation 2 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include <stdio.h> int main(void) { int command; // Input: an option from the main menu int money; // Input: withdraw or deposite money int balance; // Output: Display the balance /* 1. Initial the balance */ /* 2. Ask the user to choose one of the 3 options */ switch (command) case 1: /* 2.1 Deposit Money */ break; case 2: /* 2.2 Withdraw Money */ case 3: /* 2.3 Print Balance */ default: /* Otherwise, Error Message */ } return(0);
37
ATM Simulation 2 P2 #include <stdio.h> int main(void) {
int command; // Input: an option from the main menu int money; // Input: withdraw or deposite money int balance; // Output: Display the balance /* 1. Initial the balance */ balance = 0; /* 2. Ask the user to choose one of the 3 options */ printf(" Main Menu\n"); printf(" \n"); printf(" 1 - Deposit money\n"); printf(" 2 - Withdraw money\n"); printf(" 3 - Print balance\n"); printf("Enter command number: "); scanf("%d", &command); switch (command) case 1: /* 2.1 Deposit Money */ printf("Enter deposit amount: "); scanf("%d", &money); balance = balance + money; break; case 2: /* 2.2 Withdraw Money */ printf("Enter withdraw amount: "); balance = balance - money; case 3: /* 2.3 Print Balance */ printf("Current balance = %d\n", balance); default: /* Otherwise, Error Message */ printf("Error choice...\n"); } return(0); 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41.
38
P2 ATM Simulation 2
39
Common Errors in Conditions
5 Common Errors in Conditions 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”); Make sure to indent the block of statements to clear the readability
40
Evaluation Homework hw
The 2nd example in IF-ELSE described transferring the number grades to letter grades. Write a program for this problem using a Switch statement without any IF statement? The result of the following logic expression is: flag && !(y + z >= x – z) where (flag=0; x=3; y=4; z=2) True False The output of the following flow diagram if (X=1) is: 1 2 3 13 23 X>2 printf(“2”); printf(“1”); printf(“3”); T F
41
Evaluation Homework hw
Type Program 2 and display the result when case = 2 (Print a copy of the code and a snapshot of the output) , and then remove the break from the second case. (Display the output only in the same page when case=2) Describe what happened when break was removed? (put the answer at the end of the same page) Re-write the following program them after fixing the errors? Show the output and draw the flow diagram? 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. double fee(int speed) { IF (speed > 160) money = ; elseif (speed > 140) money = ; Else money = ; return (money); } int main(void) { printf(“What is the speed of the car:”); scanf(“%d”, &speed); printf(“Speed = %d, Ticket = %f\n”, speed, fee(speed));
42
CHAPTER 4 - Conditional Statements
# CHAPTER 4 - Conditional Statements Simple Logic Expression Complex Logic Expression Evaluation Tree Conditional Statements IF Statement Switch Statement Common Errors in Conditions ATM Simulation 1 ATM Simulation 2 column shows the topics index. column shows the programs index.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.