1 / 24

CSCI 171

CSCI 171. Presentation 3. Operators. Instructs C to perform some operation Assignment = Mathematical Relational Logical. Relational Operators. Used to compare expressions Equal = = Greater than > Greater than or equal to >= Less than < Less than or equal to <= Not equal !=.

Télécharger la présentation

CSCI 171

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 171 Presentation 3

  2. Operators • Instructs C to perform some operation • Assignment • = • Mathematical • Relational • Logical

  3. Relational Operators • Used to compare expressions • Equal = = • Greater than > • Greater than or equal to >= • Less than < • Less than or equal to <= • Not equal !=

  4. Tip • Do not confuse = (assignment) with = = (logical comparison of equality) • Common errors: • x = = z + 2; • if (x = 3) ...

  5. if statement • General format 1: • if (expression) • statement; • General format 2: • if (expression) { • statement 1; • statement 2; • …. • Statement n; • }

  6. Example if • scanf(“%f”, &salary) • if (salary > 0) { • net = salary - (salary * tax); • printf(“The net salary is %f”, net); • }

  7. Sample if with else • scanf(“%f”, &salary) • if (salary > 0) { • net = salary - (salary * tax); • printf(“The net salary is %f”, net); • } • else • printf(“Incorrect input”);

  8. Sample Program 3.1 #include <stdio.h> int main( void ) { int y = 0; printf("Please enter a value for y: "); scanf("%d", &y); if (y >= 1000) printf("\nYou have entered a large number."); printf("\nIt is at greater than or equal to 1000."); return 0; }

  9. The conditional operator compared to the if statement • z = (x < y) ? x : y; • Is equivalent to: • if (x < y) • z = x; • else • z = y;

  10. Sample Program 3.2 #include <stdio.h> int main( void ) { int y = 0; printf("Please enter a value for y: "); scanf("%d", &y); !(y%4)? printf(“y is divisible by 4") : printf(“y is not divisible by 4"); }

  11. Relational Expressions • Relational expressions evaluate to: • 0 (false) • 1 (true) • Ex: • x = (5 = = 5) • x holds the value 1

  12. Example • x = (12 < 62); • printf(“%d”, x); • x = (5 != 3); • printf(“ %d”, x); • x = (12 < 62) + (5 != 3) + (5 < 3); • printf(“ %d”, x); • Output is as follows: • 1 1 2

  13. Precedence of Relational Operators • Relational operators have lower precedence than mathematical operators • if ((x + 2) > y) is the same as if (x + 2 > y)

  14. Tip • Whenever possible, avoid the not operator • Ex: • if (x != 5) • statement1; • else • statement2; • Is equivalent to: • if (x == 5) • statement2; • else • statement1;

  15. Logical Operators • AND && if (exp1 && exp2) • True if both exp1 and exp2 are true • OR || if (exp1 || exp2) • True if either exp1 or exp2 is true • NOT ! if (!exp1) • True if exp1 is false • Precedence • NOT, AND, OR

  16. Logical Operator Examples • (3 != 5) || (6 < 8) true • (3 != 5) || (6 > 8) && (9 = = 3) true • ((3 != 5) || (6 > 8)) && (9 = = 3) false • ((3 != 5) || (6 > 8)) && !(9 = = 3) true • 3 true • 0 false • !3 false

  17. Sample Program 3.3 #include <stdio.h> //1. Figure the output for input of a = 3, b = 5, c = 3 //2. Is there anyway for both blocks 3 and 4 to be entered? //3. Rewrite the first if statement using DeMorgan's law int main( void ) { int a = 0, b = 0, c = 0; printf("Enter a, b, and c (separated by commas): "); scanf("%d, %d, %d", &a, &b, &c); if (!((a < 5) && (b > 3))) printf("\nBlock 1 entered"); else printf("\nBlock 2 entered"); if ((a < 5) || (b > 3) && (c == 2)) printf("\nBlock 3 entered"); if (((a < 5) || (b > 3)) && (c == 2)) printf("\nBlock 4 entered"); }

  18. The switch statement • Program control based on an expression with more than 2 possible values • Similar functionality to if statement

  19. General form for a switch • General form of switch statement: • switch(expression) • { • case template1: • statements; • case template2: • statements; • … • case templaten: • statements; • default: • statements; • }

  20. Concrete example of switch • switch(i) { • case 1: • printf(”The number is 1”); • case 2: • printf(“The number is 2”); • case 3: • printf(”The number is 3”); • default: • printf(”The number is not 1, 2, or 3"); • }

  21. Evaluation of a switch statement • If expression matches a template, control passes to first statement within that template • If no match, control passes to first statement within default • If no match and no default, control passed to first statement after switch structure

  22. Output of switch statement • switch(i) { • case 1: • printf(”The number is 1\n”); • case 2: • printf(“The number is 2\n”); • case 3: • printf(”The number is 3\n”); • default: • printf(”The number is not 1, 2, or 3\n"); • } • If i = 1 • Output: • The number is 1 • The number is 2 • The number is 3 • The number is not 1, 2, or 3

  23. Correct way to code a switch • switch(i) { • case 1: • printf(”The number is 1\n”); • break; • case 2: • printf(“The number is 2\n”); • break; • case 3: • printf(”The number is 3\n”); • break; • default: • printf(”The number is not 1, 2, or 3\n"); • }

  24. Sample Program 3.4 //Run the following program and fix any errors //Once the program compiles cleanly and runs correctly, //change it to allow for all 4 arithmetic operations #include <stdio.h> int main( void ) { char operator = 0; int a = 0, b = 0; printf("Enter the first integer: "); scanf("%lf", &a); printf("Enter the second integer: "); scanf("%lf", b); print("Enter the calculation you would like (+ or -): ") scanf(" %c", &operator); switch(operator) { case '+': printf("\na + b is: %d", a + b); case '-': printf("\na - b is: %d", a - b); default: printf("\nSorry, you did not enter a valid operator."); } }

More Related