1 / 15

Lab 6

Lab 6. Operators Relational Operators Equality Operators Logical Operators If statement One Alternative With Compound Statement 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.

deusebio
Télécharger la présentation

Lab 6

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. Lab 6 • Operators • Relational Operators • Equality Operators • Logical Operators • If statement • One Alternative • With Compound Statement • 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. • a+b*c • a*b+c%d • -c%d • -a +c%d • -a /-b +c • -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 • x<=0 • x>=MAX_POWER • mor_or_dad == ‘M’ • x != SENTINEL

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

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

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

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

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

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

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

  11. Write C statements : • 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. • 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. • Compute the population growth from yesterday to today as a percentage of yesterday’s population. • Trace the following program given x =12.5, y=5.0, temp=?: if (x>y) { temp=x; x=y; y=temp;}

  12. Exercise6 • Write 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 • Write 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 • A 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. • Explain 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) • --------------------------------------------- • 125 • 215 • 500 • 60 880 • 75 1000 • 100 1675

More Related