CHAPTER 4 Selection Making Decision

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
©Brooks/Cole, 2001 Chapter 5 Selection-- Making Decision.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Conditional Statement
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving,
Introduction to Computer Algorithmics and Programming Ceng 113 Program Control Statements.
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Programming Fundamentals by Dr. Nadia Y. Yousif1 Control Structures (Selections) Topics to cover here: Selection statements in the algorithmic language:
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
ECE Application Programming
Decision making If.. else statement.
Chapter 3 Selection Statements
ECE Application Programming
‘C’ Programming Khalid Jamal.
Chapter 3 Control Statements
EKT120 COMPUTER PROGRAMMING
Selection (also known as Branching) Jumail Bin Taliba by
LESSON 4 Decision Control Structure
Decisions Chapter 4.
How tax is calculated on your Taxable income Example Your gross income = $126,000 Your deduction = $ 6,000 Taxable income = $120,000.
Chapter 4: Making Decisions.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Selection—Making Decisions
ECE Application Programming
Chapter 4 (Conditional Statements)
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Variables A piece of memory set aside to store data
Week 4 – Repetition Structures / Loops
Condition Statements.
Chapter 4: Making Decisions.
Iteration statement while do-while
Chapter 4: Making Decisions.
Control Statement Examples
Introduction to Programming
Decision Making.
Selection-- Making Decision
Compound Assignment Operators in C++
Additional Control Structures
Chapter 2.1 Repetition.
Decision making If statement.
Control Structure: Selection (Part 2)
CPS125 Week 5.
Week 3 – Program Control Structure
Topics discussed in this section:
EECE.2160 ECE Application Programming
Chapter 4: Selection Structures: if and switch Statements
EECE.2160 ECE Application Programming
Selection Control Structure
EECE.2160 ECE Application Programming
CSCE 206 Lab Structured Programming in C
Presentation transcript:

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

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

Logical operator truth table DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

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

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

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

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

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision Nested If statement DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

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

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision Dangling else problem DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

Dangling else solution DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

Conditional Expression expression ? expression1 : expression2; a == b ? c-- : c++; DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

Example- calculate tax bracket taxable tax rate ===================== 1 <= 10000 2% 2 10001 – 20000 5% 3 20001 – 30000 7% 4 30001 – 40000 10% 5 > = 50001 15% Income 23000 in bracket Rate Tax 2% 200 10000 5% 500 7% 210 Total 910 Income 18000 in bracket Rate Tax 2% 200 8000 5% 400 Total 600 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

Example- calculate tax DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

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 3 tax due = total tax – taxes paid End calcTaxes DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision #include <stdio.h> #define LOWEST 0.0 #define HIGHEST 1000000.0 #define LIMIT1 10000.0 #define LIMIT2 20000.0 #define LIMIT3 30000.0 #define LIMIT4 50000.0 #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

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

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

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision Figure 5-20 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision Figure 5-21 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision Figure 5-22 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision Figure 5-23 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision Figure 5-24 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

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

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision Figure 5-27 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

DDC 1123 Programming 1(C) Chap 4 Selection Making Decision Figure 5-28 DDC 1123 Programming 1(C) Chap 4 Selection Making Decision

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

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

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

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

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

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

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

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