Summary
If conditions are used to execute statement or a group of statements based upon logically tested values.
Alternative to if condition is switch statement.
Both if and switch statements form logical structure of a program.
70 trang |
Chia sẻ: thucuc2301 | Lượt xem: 637 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Selection - Lecture 25, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
SelectionLecture 25Summary of previous lectureIn the previous lecture we have been covered,Algorithms and ProgramsA C programming languageHistory of CC, A High level languageHow to get started with C.Basic Structure of a C programData Storage and Data TypesVariables, Keywords, identifiers, AssignmentSummary of previous lectureconstant variableprintf() and scanf() functions and usagePrecedence int and floatUnary operationsIncrement and decrement operationsCommentsError and its TypesToday’s TopicsThe if statementThe else statementCascaded ifNested ifSwitch statementConditionsWe come across conditions in daily life, For exampleIf you will get less than 50 marks then you will fail.If team scored less than 250 runs, it will be out of the tournament.If the temperature is greater than 50 degree Celsius then wear light dress.Conditions in C ProgrammingSame if structure is used in c programming to test the condition.It has two partsConditional or antecedent or LHS part and Conclusion or action or consequent or RHS part.The if statementDetermines whether a statement or block is executed.Implements the selection instructions within an algorithm.Decides what to do by evaluating a Boolean expression.If the expression is true (non-zero), the statement or block is executed. if ( expression ) statementWhat is a statement?Statements are lines of instructions in our programs ending with a semicolon (;).A compound statement or block is a series of statements surrounded by braces. { number = number + 1; printf("%d\n", number); }An empty statement is a single semicolon.E.g.Read in a number, and print it if it is odd.output “Enter an integer”input numberif (number is odd)then{ output the number}Example: oddnum.c #include /* Read in a number, and echo it if it is odd. */int main(){ return 0;}Read in a number, and print it if it is odd.output “Enter an integer”input numberif (number is odd)then{ output the number}Example: oddnum.c #include /* Read in a number, and echo it if it is odd. */int main(){ int number; printf("Enter an integer: "); scanf("%d", &number); return 0;}Read in a number, and print it if it is odd.output “Enter an integer”input numberif (number is odd)then{ output the number}Example: oddnum.c #include /* Read in a number, and echo it if it is odd. */int main(){ int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0;}Read in a number, and print it if it is odd.output “Enter an integer”input numberif (number is odd)then{ output the number}Example: oddnum.c #include /* Read in a number, and echo it if it is odd. */int main(){ int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0;}Read in a number, and print it if it is odd.output “Enter an integer”input numberif (number is odd)then{ output the number}Do not put “then” here!Example: oddnum.c #include /* Read in a number, and echo it if it is odd. */int main(){ int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0;}Read in a number, and print it if it is odd.output “Enter an integer”input numberif (number is odd)then{ output the number}Do not put semicolon here!Example: oddnum.c #include /* Read in a number, and echo it if it is odd. */int main(){ int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d\n", number); } return 0;}Read in a number, and print it if it is odd.output “Enter an integer”input numberif (number is odd)then{ output the number}Example: oddnum.c Notes on ifWhich of the following code fragments are equivalent? if (number % 2 != 0) { printf("%d", number); } printf(” is odd\n"); if (number % 2 != 0) printf("%d", number); printf(” is odd\n"); if (number % 2 != 0) { printf("%d", number); printf(” is odd\n"); }ABCNotes on ifWhich of the following code fragments are equivalent? if (number % 2 != 0) { printf("%d", number); } printf(” is odd\n"); if (number % 2 != 0) printf("%d", number); printf(” is odd\n"); if (number % 2 != 0) { printf("%d", number); printf(” is odd\n"); }ABCNotes on ifWhich of the following code fragments are equivalent? if (number % 2 != 0) { printf("%d", number); } printf(” is odd\n"); if (number % 2 != 0) printf("%d", number); printf(” is odd\n"); if (number % 2 != 0) { printf("%d", number); printf(” is odd\n"); }ABCA StatementA Compound StatementNotes on ifCommon mistake if (number % 2 != 0); { printf("%d is an odd ", number); } printf("number\n");Notes on ifCommon mistake if (number % 2 != 0); { printf("%d is an odd ", number); } printf("number\n");No semi-colon here!The semicolon is an empty statement.Notes on ifCommon mistake if (number = 0) { printf("%d\n", number); } printf("%d\n", number);Notes on ifCommon mistake if (number = 0) { printf("%d\n", number); } printf("%d\n", number);Should be ==The else statementCan only occur after an if statementIs only executed when the if block does not execute if ( expression ) statement1 else statement2#include /* Determine whether an input number is odd or even. */main(){ int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); }}Read in a number, and determine if it’s odd or even.output “Enter an integer”input numberif (number is odd)then{ output: number “ is an odd number”}else{ output: number “ is an even number”}Example: oddnum.c #include /* Determine whether an input number is odd or even. */main(){ int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); }}Example: oddeven.c Read in a number, and determine if it’s odd or even.output “Enter an integer”input numberif (number is odd)then{ output: number “ is an odd number”}else{ output: number “ is an even number”}#include /* Determine whether an input number is odd or even. */main(){ int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); }}Example: oddeven.c Read in a number, and determine if it’s odd or even.output “Enter an integer”input numberif (number is odd)then{ output: number “ is an odd number”}else{ output: number “ is an even number”}No semicolons here!#include /* Determine whether an input number is odd or even. */main(){ int number; printf("Enter an integer: "); scanf("%d", &number); if (number % 2 != 0) { printf("%d is an odd number\n", number); } else { printf("%d is an even number\n", number); }}Example: oddeven.c Read in a number, and determine if it’s odd or even.output “Enter an integer”input numberif (number is odd)then{ output: number “ is an odd number”}else{ output: number “ is an even number”}Output of a programCascaded if statementMultiple alternative blocks each with a Boolean expression.First expression which evaluates to true causes execution of the associated block.At most only one block will be executed.Example: months.c Determine the number of days in a given month:30 days = September, April, June and November.All the rest have 31,Excepting February alone,Which have 28 days clear,And 29 in each leap year.output “Enter an integer”input monthif (month is September, or April, or June, or November)then{ output “30 days”}else if (month is February){ output “28 or 29 days”}else{ output “31 days”}int main(){ return 0;}Example: months.c #include /*************************\Determine the number of days in a given month:30 days = September,April, June and November;All the rest have 31,Excepting February alone,And that has 28 days clearAnd 29 in each leap year.\*************************/const int September = 9;const int April = 4;const int June = 6;const int November = 11;const int February = 2;int main(){ int month; printf("Enter number of month: "); scanf("%d", &month); return 0;}Example: months.c #include /*************************\Determine the number of days in a given month:30 days = September,April, June and November;All the rest have 31,Excepting February alone,And that has 28 days clearAnd 29 in each leap year.\*************************/const int September = 9;const int April = 4;const int June = 6;const int November = 11;const int February = 2;int main(){ int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } return 0;}Example: months.c #include /*************************\Determine the number of days in a given month:30 days hath September,April, June and November;All the rest have 31,Excepting February alone,And that has 28 days clearAnd 29 in each leap year.\*************************/const int September = 9;const int April = 4;const int June = 6;const int November = 11;const int February = 2;#include /*************************\Determine the number of days in a given month:30 days hath September,April, June and November;All the rest have 31,Excepting February alone,And that has 28 days clearAnd 29 in each leap year.\*************************/const int September = 9;const int April = 4;const int June = 6;const int November = 11;const int February = 2;int main(){ int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } return 0;}Example: months.c Common mistake:if (month==September || April || June || November )int main(){ int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } return 0;}Example: months.c #include /*************************\Determine the number of days in a given month:30 days hath September,April, June and November;All the rest have 31,Excepting February alone,And that has 28 days clearAnd 29 in each leap year.\*************************/const int September = 9;const int April = 4;const int June = 6;const int November = 11;const int February = 2;int main(){ int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } else { printf("31 days\n"); } return 0;}Example: months.c #include /*************************\Determine the number of days in a given month:30 days hath September,April, June and November;All the rest have 31,Excepting February alone,And that has 28 days clearAnd 29 in each leap year.\*************************/const int September = 9;const int April = 4;const int June = 6;const int November = 11;const int February = 2;int main(){ int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } else { printf("31 days\n"); } return 0;}Example: months.c #include /*************************\Determine the number of days in a given month:30 days hath September,April, June and November;All the rest have 31,Excepting February alone,And that has 28 days clearAnd 29 in each leap year.\*************************/const int September = 9;const int April = 4;const int June = 6;const int November = 11;const int February = 2;“Default” block.int main(){ int month; printf("Enter number of month: "); scanf("%d", &month); if (month==September || month==April || month==June || month==November ) { printf("30 days\n"); } else if (month==February) { printf("28 or 29 days\n"); } else { printf("31 days\n"); } return 0;}Example: months.c #include /*************************\Determine the number of days in a given month:30 days hath September,April, June and November;All the rest have 31,Excepting February alone,And that has 28 days clearAnd 29 in each leap year.\*************************/const int September = 9;const int April = 4;const int June = 6;const int November = 11;const int February = 2;Notes on Cascaded ifif (letter >= ’a’){ printf(“S1\n”);}else if (letter = ’A’){ printf(“S3\n”);}else if (letter = ’a’){ printf(“S1\n”);}else if (letter = ’A’){ printf(“S3\n”);}else if (letter = ’a’ && ch = ’A’ && ch = ’0’ && ch int main(){int marks;printf("%s", "Enter yours Marks");scanf("%d",&marks); switch(marks) { case 100:case 99:case 98:case 97:case 96:case 95:case 94: case 93:case 92:case 91:case 90: printf("%s", "You got A grade"); break;Remember you need constants here, so explicitly write all statements for which this case may be executed.Switch statementcase 89:case 88:case 87:case 86:case 85:case 84: case 83:case 82:case 81:case 80: printf("%s", "You got B grade"); break;case 79:case 78:case 77:case 76:case 75:case 74:case 73:case 72:case 71:case 70: printf("%s", "You got C grade"); break;case 69:case 68:case 67:case 66:case 65:case 64:case 63:case 62:case 61:case 60: printf("%s", "You got D grade"); break;In case any of these cases execute, control from case statement will be returned.default: printf("%s", "You got F grade"); break; } return 0;}If user enters some wrong value default will be executed!Alternative If statements#includeint main(){int marks;printf("%s", "Enter yours Marks");scanf("%d",&marks);if (marks >=90){ printf("%s", "you got A grade");}else if (marks >=80)User inputs marksFirst Check, if true rest of the statements will not be executedIf first check is false, this statement will be checked and so on!{ printf("%s", "you got B grade");}else if (marks >=70){ printf("%s", "you got c grade");}else if (marks >=60){ printf("%s", "you got D grade");}Logical comparison in if statement, which is not permitted in caseelse printf("%s", "you got F grade");return 0;}Integer value is returned by this program!SummaryIf conditions are used to execute statement or a group of statements based upon logically tested values.Alternative to if condition is switch statement.Both if and switch statements form logical structure of a program.
Các file đính kèm theo tài liệu này:
- lecture_25_523_2027003.ppt