Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 6 rOperators l Relational Operators l Equality Operators l Logical Operators rIf statement l One Alternative l With Compound Statement l Nested If.

Similar presentations


Presentation on theme: "Lab 6 rOperators l Relational Operators l Equality Operators l Logical Operators rIf statement l One Alternative l With Compound Statement l Nested If."— Presentation transcript:

1 Lab 6 rOperators l Relational Operators l Equality Operators l Logical Operators rIf statement l One Alternative l With Compound Statement l Nested If NOTE: Be Careful Read Chapter 4(All Self-Check exercises and all remaining exercises). C KEYWORDS and format must be learned by heart

2 Review of Rules of Evaluating Expressions Exercise 1 : Include parentheses to show the order of performing the expressions. ra+b*c ra*b+c%d r-c%d r-a +c%d r-a /-b +c r-a/-(b+c)

3 Simple Conditions Exercise 2: Provide the English Meaning and Values. Given: x=-5, power=1024,y=7, MAX_POWER=1024,mor_or_dad=‘M’ SENTINEL=999 rx<=0 rx>=MAX_POWER rmor_or_dad == ‘M’ rx != SENTINEL

4 Operator Precedence ------------------------------------------- Function calls highest ! + - & (UP) * / % + - >= == != % || = Lowest

5 Exercise3 (Step by Step Evaluation) rProvide the English Meaning and Values. Given :x=3.0, y=4.0, z =2.0, and flag =5 r!flag rx + y / z <= 3.5 r !flag || (y+z >= x-z) r!(flag || (y+z >= x-z)

6 rShort-circuit evaluation : stopping evaluation of a logical expression as soon as its value can be determined. l Exp : a ||b, a&&b rLogical Assignments : int age, char gender, int senior_citizen, in_range and is_letter, n, even l scanf (" %d ",&age); l senior_citizen=(age>=65); l ! senior_citizen; l senior_citizen && gender ==‘M’; l is_range = (n>-10 && n<10); is_letter = ( (‘A’ <= ch && ch<=‘Z’) || (‘A’ <= ch && ch<=‘Z’) ); even = (n%2 == 0) ; Short-circuit evaluation and Logical Assignments

7 Complementing a Condition and Morgan’s Theorem r!(item ==SENT) r!(status==‘s’ && age > 25) rMorgan’s Theorem l The complement of expr1 && expr2 as comp1 || comp2 l The complement of expr1 || expr2 is comp1 && comp2 rExercise4 :Given x=15.0 and y = 25.0, evaluate the following expressions : l x!=y l x<x l x >= y l x == y +x –y

8 rExercise5 : Given a =5, b=10, c=15 and flag =1. Use the short circuit evaluation : l c == a+b || !flag l a!= 7 && flag || c>=6 l !(b<= 12) && a%2 ==0 l !(a > 5 || c < a+b) l Complement each expression. l What is the value assigned to ans of type int if p=100 and q=50 ans = (p>95) +(q<95);

9 Programming rWrite an expression to test the following : l age is from 18 to 21 inclusive. l water is less than 1.5 and also greater than 0.1 l year is divisible by 4. l speed is not greater than 55. l y is greater than x and less than z. l w is either equal to 6 or not greater than 3. l Assign 1 to between if n is the range –k through +k l Assign 1 to uppercase if ch is uppercase letter; otherwise assign a value of 0. l Assign a value of 1 to divisor if m is divisor of n; otherwise assign it the value of 0.

10 If Statement rWhat do the statements display ? l If(12 <12) printf (" less" ); else printf (" not less" ); l var1= 25.12, var2= 15.00 if( var1 <= var2) printf (" less or equal" ); else printf (" greater than" ); rWhat is the value assigned to x when y = 15.0 ? l x =25.0; If (y!= (x-10.0)) x=x-10.0; else x=x/2.0;

11 rWrite C statements : l If item is nonzero, then multiply product by item and save the result in product, otherwise skip the multiplication. In either case, print the value of product. l If x is 0, add to zero_count. If x is negative, add x to the minus_sum. If x is greater than 0, add x to plus_sum. rCompute the population growth from yesterday to today as a percentage of yesterday’s population. rTrace the following program given x =12.5, y=5.0, temp=?: if (x>y) { temp=x; x=y; y=temp;}

12 Exercise6 rWrite an if statement that might be used to compute and display the average of a set of n numbers whose sum is stored in variable total. This average should be only if n>0, otherwise an error message should be displayed.

13 Multiple-Alternative Decision rWrite a C program that shows the relationship between noise and human perceptions of noises. Loudness in Decibels (db) Perception ------------------------------------------------------------------ 50 or lower quiet 51-70 intrusive 71-90 annoying 91-110 very annoying above 110 uncomfortable

14 Exercice7 rA store clerk gains a raise of 10% of his salary if all the sales he has performed exceed 30,000 DH worth of the merchandise each month, an only 5% raise if the sales are below 30,000 DH. Write a C program that shows the clerk man how to figure out what is his raise. rExplain the difference between : If( x >= 0) x = x+1 else if (x>=1) x= x+2 And If (x >= 0) x = x+1 if (x>=1) x= x+2

15 Exercise 8 Write a program to show the expected brightness of a standard light bulb given its wattage. Display Unknown bulb if the watts input is not in the table. Watts Brightness (in Lumens) --------------------------------------------- 15 125 25 215 40 500 60 880 75 1000 100 1675


Download ppt "Lab 6 rOperators l Relational Operators l Equality Operators l Logical Operators rIf statement l One Alternative l With Compound Statement l Nested If."

Similar presentations


Ads by Google