Download presentation
Presentation is loading. Please wait.
1
CHAPTER 4 Selection Making Decision
Logical Data and Operator Two Way Selection Multi way Selection More Standard Library Function A Menu Program DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
2
4.1 Logical Data and Operator
how logical data is represented in C Operator Meaning Precedence ============================= ! Not 15 && Logical AND 5 || Logical OR 4 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
3
Logical operator truth table
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
4
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
5
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
6
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Original Expression Simplified ============================ !(x < y) x >= y !(x > y) x <= y !(x != y) x == y DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
7
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
4.2 Two Way Selection DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
8
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
9
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Null else statement Null if statement DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
10
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Nested If statement DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
11
Example of nested if statement
if ( a <= b) { if( a < b) printf(“%d < %d”, a, b); else printf(“%d == %d”, a, b); } printf(“%d > %d”, a, b); if ( a <= b) if( a < b) printf(“%d < %d”, a, b); else printf(“%d == %d”, a, b); printf(“%d > %d”, a, b); DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
12
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Dangling else problem DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
13
Dangling else solution
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
14
Conditional Expression
expression ? expression1 : expression2; a == b ? c-- : c++; DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
15
Example- calculate tax
bracket taxable tax rate ===================== 1 <= % – % – % – % 5 > = % Income 23000 in bracket Rate Tax 2% 200 % 500 7% 210 Total 910 Income 18000 in bracket Rate Tax 2% 200 % 400 Total 600 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
16
Example- calculate tax
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
17
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Program Calculate Taxes Calculate taxes based on marginal tax brackets. 1 Get income data (total income, taxes paid, dependencies) 2 Calculate taxes (total tax, tax due) 3 Print information End Calculate Taxes ==================== calcTaxes ================== calcTaxes 1 taxable income = total income – dependent exemptions 2 total tax = tax for bracket 1 + tax for bracket 2 + tax for bracket 3 + tax for bracket 4 + tax for bracket 5 tax due = total tax – taxes paid End calcTaxes DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
18
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
#include <stdio.h> #define LOWEST 0.0 #define HIGHEST #define LIMIT #define LIMIT #define LIMIT #define LIMIT #define RATE1 02 #define RATE2 05 #define RATE3 07 #define RATE4 10 #define RATE1 15 #define DEDN_PER_DPNDNT 1000 /*Prototype*/ void getData(double *totalIncome, double *taxpaid, int *numofDpndnts); void calcTaxes(double totalIncome, double taxPaid, int numOfDpndnts, double *taxableIncome, *totalTax, double *taxDue); void printInformation(double totalIncome, double taxpaid, int numofDpndnts, double totalTax,paidTax, taxDue); double bracketTax(double income, double startLimit, double stopLimit, int rate); int main() { int numOfDpndnts; double taxDue,taxPaid,totalIncome, taxableIncome, totalTax; getData(&totalIncome,&taxPaid, &numOfDpndnts); calcTaxes(totalIncome,taxPaid, numOfDpndnts, &taxableIncome, &totalTax, &taxDue); printInformation(totalIncome,taxableIncome,numOfDpndnts,totakTax, taxPaid,taxDue); return 0; } DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
19
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
void getData(double *totalIncome, double *taxpaid, int *numofDpndnts) { printf(“Enter total income for last year:”); scanf(“%lf”,totalIncome); printf(“Enter total payrol deduction:”); scanf(“%lf”,taxPaid); printf(“Enter no of dependants:”); scanf(“%lf”,numDpndnts); return; } =========================== void calcTaxes(double totInc, double taxPaid, int numOfDpndnts, double *taxableIncome, *totalTax, double *taxDue) *taxableInc = totInc – ( numOfDpndnts * DEDN_PER_DPNDNT); *totTax = bracketTax(*taxableIncome, LOWEST, LIMIT1, RATE1) + bracketTax(*taxableIncome, LIMIT1, LIMIT2, RATE2) LIMIT2, LIMIT3, RATE3) LIMIT3, LIMIT4, RATE4) LIMIT4, HIGHEST, RATE5); *taxDue = * totTax – taxPaid; return; } double bracketTax(double income,double startLimit,double stopLimit,int rate); { double tax; if(income <= startLimit) tax = 0.0; else if(income > startLimit && income <=stopLimit) tax = (income – startLimit) * rate /100.0; tax = (stopLimit – startLimit) * rate / 100.0; return tax; DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
20
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Figure 5-19 4.3 Multi Way Selection DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
21
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Figure 5-20 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
22
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Figure 5-21 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
23
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Figure 5-22 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
24
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Figure 5-23 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
25
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Figure 5-24 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
26
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Example: get grade if ( mark >= 90) grade = ‘A’; else if(mark >= 80) grade = ‘B’; else if(mark >= 70) grade = ‘C’; else if(mark >= 60) grade = ‘D’; else grade = ‘F’; printf(“Your grade is: %c”, grade); if ( cgpa >= 3.0) printf(“Very Good”); else if(cgpa >= 2.0) printf(“OK”); else if (cgpa >=1.7) printf(“You want to leave?”); else printf(“Bye”); DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
27
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Figure 5-27 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
28
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Figure 5-28 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
29
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Figure 5-29 T F DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
30
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Example: char letter; printf(“enter letter:”); scanf(“%c”,&letter); if((letter== ‘a’ )|| (letter== ‘e’)||( letter == ‘i’)) printf(“”vokal); else printf(“konsonen”); printf(“enter letter:”); scanf(“%c”,&letter); switch(value) { case ‘a’: case ‘e’: case ‘i’: printf(“vokall”); break; default: printf(“konsonan”); } DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
31
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Example: printf(“enter letter:”); scanf(“%c”,&letter); switch(value) { case ‘a’: case ‘e’: case ‘i’: case ‘A’: case ‘E’: case ‘I’: printf(“vokall”); break; default: printf(“konsonan”); } printf(“enter letter:”); scanf(“%c”,&letter); letter = toupper(letter); switch(value) { case ‘A’: case ‘E’: case ‘I’: printf(“vokall”); break; default: printf(“konsonan”); } DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
32
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Example: choice = getSelection(); switch(choice) { case ‘1’: checkBalance(); break; case ‘2’: cashWithdrawal(); default: printf(“Good Bye”); } int getSelection(void) { int select; printf(“\n1. Check Balance.”); printf(“\n2. Withdraw Cash.”); printf(“\n3. Quit“); printf(“\nenter selection:”); scanf(“%c”,& select); return select; } void checkBalance(void) .. DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
33
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Figure 5-26 int getOption(void); void getData(float *num1, float *num2); float calc(int option ,float num1, float num2); float add(float num1,float num2); float sub(float num1,float num2); DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
34
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
A MENU PROGRAM pg 207 int getOption(void) { int option; printf(“\n1.ADD”); printf(“\n2.SUBTRACT”); printf(“\n3.MULTIPLY”); printf(“\n4.DIVIDE”); printf(“\nEnter your selection:”); scanf(“%d”,&option); return option; } intmain(void) { int option; float num1,num2,result; option = getOption(); getData(&num1,&num2); result=calc(option,num1,num2); printResult(num1,num2,result,option); return 0; } DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
35
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
float calc(int option, float num1, float num2) { float result; switch(option) case 1:result = add(num1,num2); break; case 2:result = sub(num1,num2); case 3:result = mul(num1,num2); case 4: if(num2 == 0.0) printf(“Error divide by 0); exit(1); } else result = dvd(num1,num2); float add(float num1, float num2) { float res; res = num1 + num2; return res; } DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
36
DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
KUIZ 2 Pilihan Tuliskan sebuah aturcara yang dapat menerima input satu digit(0 hingga 9) dan kemudiannya mencetak nilainya dalam perkataan. contoh: Jika input 5 Maka output lima DDC 1123 Programming 1(C) Chap 4 Selection Making Decision
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.