240 likes | 255 Views
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 !=.
E N D
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 !=
Tip • Do not confuse = (assignment) with = = (logical comparison of equality) • Common errors: • x = = z + 2; • if (x = 3) ...
if statement • General format 1: • if (expression) • statement; • General format 2: • if (expression) { • statement 1; • statement 2; • …. • Statement n; • }
Example if • scanf(“%f”, &salary) • if (salary > 0) { • net = salary - (salary * tax); • printf(“The net salary is %f”, net); • }
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”);
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; }
The conditional operator compared to the if statement • z = (x < y) ? x : y; • Is equivalent to: • if (x < y) • z = x; • else • z = y;
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"); }
Relational Expressions • Relational expressions evaluate to: • 0 (false) • 1 (true) • Ex: • x = (5 = = 5) • x holds the value 1
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
Precedence of Relational Operators • Relational operators have lower precedence than mathematical operators • if ((x + 2) > y) is the same as if (x + 2 > y)
Tip • Whenever possible, avoid the not operator • Ex: • if (x != 5) • statement1; • else • statement2; • Is equivalent to: • if (x == 5) • statement2; • else • statement1;
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
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
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"); }
The switch statement • Program control based on an expression with more than 2 possible values • Similar functionality to if statement
General form for a switch • General form of switch statement: • switch(expression) • { • case template1: • statements; • case template2: • statements; • … • case templaten: • statements; • default: • statements; • }
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"); • }
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
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
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"); • }
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."); } }