control structures in c overview
play

Control Structures in C Overview 1)Logic & Logical Operators - PDF document

Control Structures in C Overview 1)Logic & Logical Operators 2)Selection Mechanisms if elseif else switch 3)Loop Mechanisms 1 Logic and Selection In English (Natural Language) we can make statements such as: If it snows I stay


  1. Control Structures in C Overview 1)Logic & Logical Operators 2)Selection Mechanisms – if elseif else – switch 3)Loop Mechanisms 1

  2. Logic and Selection In English (Natural Language) we can make statements such as: If it snows I stay home otherwise I go out. If I feel energetic and it’s Tuesday I come to class. • Logical expression must be unambiguous – Either do it – Or do not do it – No maybe • So we have logical expressions or Boolean expressions which evaluate to true or false 2

  3. Some logical expressions in c. Remember they have to have a value true or false. x > 7 salary < 1000 date ==30 note: equals is == letter != ‘c’ note != is not equals amount >= 3.50 Relational Operators: The operators that can be used to make simple logical expressions 3

  4. The “if” statement int x=5; if ( x>3) printf(“x is greater than 3\n”); Effect: if x>3 is true it prints otherwise it just goes on to next statement Another Example int main(void) { char character; printf("\n Please enter a single character\n"); scanf("%c",&character); if ( character == ‘y') printf("\n you typed the character '%c'\n",character); return 0; } 4

  5. Another Example int main(void) { char character; printf("\n Please enter a single character\n"); scanf("%c",&character); if ( character == 121) printf("\n you typed the character '%c'\n",character); return 0; } Logical Data • In C the Boolean type doesn’t exit. – 0 is false – Everything else is true 5

  6. A common mistake which may drive you crazy. instead of if ( character == 'c')… you type if ( character = 'c')… This is legal but is wrongly evaluated (logically) INDENTING • Note indentation of code is ignored by compiler • Essential for readability char character; printf("\n Please enter a single character\n"); scanf("%c",&character); if ( character == 'c') printf("\n you typed the character '%c'\n",character); return 0; 6

  7. So the general form of the if is: if ( logical expression) statement; Note no semi-colon on the “if”. The whole thing is one statement. if-else General format: if ( logical statement) statement; /* if true….note indent*/ else statement; /* if false….note indent*/ 7

  8. int main(void) { char character; printf("\n Please enter a single character\n"); scanf("%c",&character); if ( character == ‘y') printf("\n you typed a '%c'\n",character); else printf("\n I did not expect a %c\n",character); return 0; } Syntax : if-else 8

  9. Flow charts- a graphical view of if-else Compound statements. “if” if ( condition) if ( x>5) statement; x++; if ( condition) if (i ==10) { statement; { x = x+5; statement; y = x = 5; } } 9

  10. Compound statements if -else if ( condition) { statement; statement; } else {statement; statement; } Note that no braces are needed for a single statement. #include <stdio.h> int main ( void ) { const float min_pst= 4.00; const float gst = 0.05; /*2008 rates */ const float pst = 0.08; /*2008 rates */ float bill, total; printf("Please input the amount of the bill\n"); scanf("%f",&bill); if (bill < min_pst) total = bill * (1.0 + gst); else total = bill * (1.0 + gst + pst); printf ("The total bill is: %10.2f\n",total); return 0;} 10

  11. #include <stdio.h> int main ( void ) { const float min_pst= 4.00; const float gst = 0.05; /*2008 rates */ const float pst = 0.08; /*2008 rates */ float bill, total; printf("Please input the amount of the bill\n"); scanf("%f",&bill); if (bill < min_pst) total = bill * (1.0 + gst); else printf(“in else statement bill=%f\n”,bill); total = bill * (1.0 + gst + pst); printf ("The total bill is: %10.2f\n",total); return 0;} Another Example - Multiple ifs #include <stdio.h> /* calculate the maximum value of three numbers */ int main ( void ){ int number1, number2, number3; int max; printf(“Please input three numbers:\n”); scanf(“%d%d%d”,&number1,&number2,&number3); 11

  12. Another Example - Multiple ifs max = number1; /* From Previous Slide */ if ( number2 > max) max = number2; if (number3 > max) max = number3; printf("The maximum of the three numbers: %d,%d,%d is = %d\n", number1, number2, number3, max); return(0); } Selecting from among many things. Example: If I type in a letter grade, namely A,B,C,D,F, I want to print out the range of numerical equivalences. e.g. C is between 60 and 69.9% We can do this correctly but not in the best way as follows: 12

  13. #include <stdio.h> int main(void){ char grade; printf("please enter a letter grade\n"); scanf("%c",&grade); if (grade =='A') printf("your grade is between 80 and 100%%\n"); if (grade =='B') printf("your grade is between 70 and79.9%%\n") if (grade =='C') printf("your grade is between 60 and69.9%%\n"); if (grade =='D') printf("your grade is between 50 and59.9%%\n"); if (grade =='F') printf("your grade is less than 50%%\n"); return 0; Expanding the logical expression There are 3 more boolean or logical operators Which allow for more complicated expressions. They are: || logical OR && logical AND ! Logical NOT 13

  14. These allow for making compound logical expressions. Is x between 2 and 5 inclusive? 2 5 (x>=2) && (x<=5) Note: for the AND to be true both propositions must be true. The && (AND) Proposition1 && Proposition2 Proposition1 Proposition2 RESULT True True True True False False False True False False False False So for the result to be true both propositions must be true 14

  15. Logical Operators Operators : “Short Circuit” 15

  16. Another Example - Multiple ifs #include <stdio.h> // Assign a letter grade to a mark out of 100 : Inefficient*/ int main ( void ){ int grade; char letter_grade; printf("Please enter a mark out of 100: "); scanf(“%d”,&grade); if ( grade >= 80 ) letter_grade = 'A'; /* Continues on Next Slide */ if ( grade >= 70 && grade < 80 ) /*After Previous Slide */ letter_grade = 'B'; if ( grade >= 60 && grade < 70 ) letter_grade = 'C'; if ( grade >= 50 && grade < 60 ) letter_grade = 'D'; if (grade < 50) letter_grade = 'F'; printf(" A mark of %d is an %c\n”, grade, letter_grade); return(0); } 16

  17. Same Example - ifs and elseifs #include <stdio.h> /* Assign a letter grade to a mark out of 100 */ /* Efficient using nested ifs (else if's) ; Note indenting */ int main ( void ) { int grade; char letter_grade; printf("Please an integer mark out of 100: "); scanf(“%d”, &grade); if (grade > 100 || grade <0) printf(" I'm afraid you have made an error in input \n"); else if ( grade >= 80) letter_grade = 'A'; /* Continues on Next Slide */ Same Example - ifs and elseifs else if ( grade >= 70) /* After Previous slide */ letter_grade = 'B'; else if ( grade >= 60) letter_grade = 'C'; else if ( grade >= 50 ) letter_grade = 'D'; else letter_grade = 'F'; if (grade >= 0 && grade <= 100 ) printf(" A grade of %d is an %c\n”, grade, letter_grade); return(0); } 17

  18. Compound logical expressions with || (OR). Is today the first or 15 th of the month? (dayOfMonth == 1) || (dayOfMonth == 15) This is true is either propostion is true: Obviously in this case both can’t be true. The || (OR) Proposition1 || Proposition2 Proposition1 Proposition2 RESULT True True True True False True False True True False False False So for the result to be false both propositions must be false. 18

  19. The NOT operator (!) inverts the value of a logical expression. It is a unary operator. x=7; The expression !(x==7) is False since the expression (x==7) is true. The NOT is usually confusing and should be used sparingly Nested if else “an if within an if” 19

  20. Example: Printing out the number of days in each month. Assume that the months are integers from 1 to 12. For now just do february. int year, month, numberOfdays; if (month == 2) {if ((year%4) == 0) numberOfDays=29; else numberOfDays=28; } Dangling elses…..danger Suppose I write the last code without braces:To which if does the else belong. The indenting is irrelevant if (month == 2) if ((year%4) == 0) numberOfDays=29; else numberOfDays=28; Rule: the else belongs to the nearest if. Better use braces For nested ifs 20

  21. Suppose I write this without braces: if (expression) if ( expression) statement; else statement; In fact this means the same as before so use braces. if (expression) {if ( expression) statement;} else {statement;} Unambiguous to user. 21

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend