Upcoming SlideShare
×

# C: Operators and Keywords

231 views

Published on

Published in: Education, Technology, Travel
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

Views
Total views
231
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
13
0
Likes
0
Embeds 0
No embeds

No notes for slide

### C: Operators and Keywords

1. 1. Lecture 3Lecture 3 Version 1.0Version 1.0 Arithmetic OperatorArithmetic Operator Relational OperatorRelational Operator Decision MakingDecision Making KeywordsKeywords
2. 2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh Arithmetic OperatorsArithmetic Operators  Most C programs perform arithmetic calculationsMost C programs perform arithmetic calculations
3. 3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh Arithmetic operatorsArithmetic operators  The arithmetic operators are allThe arithmetic operators are all binary operatorsbinary operators  The expression 3+7 contains binary operator +The expression 3+7 contains binary operator + and twoand two operandsoperands 3 and 73 and 7  Integer division yields an integer result. 17/4 is 4Integer division yields an integer result. 17/4 is 4  The modulus operator means the remainderThe modulus operator means the remainder after a division operation. 17%4 is 1after a division operation. 17%4 is 1
4. 4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh Rules of Operator PrecedenceRules of Operator Precedence 1.1. Expressions or portion of expressionsExpressions or portion of expressions contained in pairs of parentheses are evaluatedcontained in pairs of parentheses are evaluated first. In case of nested parentheses, the innerfirst. In case of nested parentheses, the inner most parentheses will be evaluated first.most parentheses will be evaluated first.
5. 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh Rules of Operator PrecedenceRules of Operator Precedence 2.2. Multiplication, division and modulusMultiplication, division and modulus operations are evaluated next. If an expressionoperations are evaluated next. If an expression contains several *, / or % operations,contains several *, / or % operations, evaluations proceed from left to right. Theseevaluations proceed from left to right. These operators are on the same level of precedence.operators are on the same level of precedence.
6. 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh Rules of Operator PrecedenceRules of Operator Precedence 3.3. Addition and subtraction are evaluated last. IfAddition and subtraction are evaluated last. If an expression contains several + or -an expression contains several + or - operations, evaluations proceed from left tooperations, evaluations proceed from left to right.right.
7. 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh Brain Storm IBrain Storm I  m=(a+b+c+d+e)/5m=(a+b+c+d+e)/5  m=a+b+c+d+e/5m=a+b+c+d+e/5  What are the differences here?What are the differences here?
8. 8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh Brain Storm IIBrain Storm II  y=mx+cy=mx+c  How do you write this expression in C? AnyHow do you write this expression in C? Any parenthesis required?parenthesis required?
9. 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh Brain Storm IIIBrain Storm III  z=pr%q+w/x-yz=pr%q+w/x-y  It is in algebraic format. How will it be written inIt is in algebraic format. How will it be written in C? What will be the order of precedence givenC? What will be the order of precedence given by the C compiler while it is written in Cby the C compiler while it is written in C format?format?
10. 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh Decision Making: RelationalDecision Making: Relational OperatorsOperators  Executable C expressions either doExecutable C expressions either do actionsactions (calculations(calculations or input or output) or make aor input or output) or make a decisiondecision  For example, in a program where the user will put hisFor example, in a program where the user will put his mark and want to know if he passed or failed, you willmark and want to know if he passed or failed, you will have to do three things-have to do three things- 1. Take the mark from the user.1. Take the mark from the user. ActionAction 2. Compare the mark with a predefined pass mark range.2. Compare the mark with a predefined pass mark range. DecisionDecision 3. Provide that decision to user.3. Provide that decision to user. ActionAction
11. 11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh Decision Making: RelationalDecision Making: Relational OperatorsOperators  The decision in C is done by a control structureThe decision in C is done by a control structure calledcalled ifif  It allows a program to make a decision based onIt allows a program to make a decision based on the truth or falsity of some statement of factthe truth or falsity of some statement of fact calledcalled conditioncondition  If the condition is true,If the condition is true, the body statementthe body statement will bewill be executed otherwise not- it will execute the nextexecuted otherwise not- it will execute the next statement after thestatement after the ifif structurestructure
12. 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh Decision Making: RelationalDecision Making: Relational OperatorsOperators  Conditions inConditions in ifif structures are formed by usingstructures are formed by using relational operatorsrelational operators  The relational operators have the same level ofThe relational operators have the same level of precedence and they associate left to rightprecedence and they associate left to right  OnlyOnly equality operatorsequality operators have a lower level ofhave a lower level of precedence than others and they also associateprecedence than others and they also associate left to rightleft to right
13. 13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh Decision Making: RelationalDecision Making: Relational OperatorsOperators
14. 14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh Decision Making: RelationalDecision Making: Relational OperatorsOperators  There is a subtle difference between == and =.There is a subtle difference between == and =. In case of =, it is associated from right to left.In case of =, it is associated from right to left. a=b means the value of b is assigned to aa=b means the value of b is assigned to a
15. 15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh Decision Making: RelationalDecision Making: Relational OperatorsOperators #include <stdio.h>#include <stdio.h> #include <conio.h>#include <conio.h> void main(){void main(){ clrscr();clrscr(); int num1, num2;int num1, num2; printf(“Enter two numbers to compare: ”);printf(“Enter two numbers to compare: ”); scanf(“%d %d”, &num1, &num2);scanf(“%d %d”, &num1, &num2); if(num1 == num2)if(num1 == num2) printf(“%d and %d are equal”, num1, num2);printf(“%d and %d are equal”, num1, num2); if(num1 != num2)if(num1 != num2) printf(“%d and %d are not equal”, num1, num2);printf(“%d and %d are not equal”, num1, num2); if(num1 < num2)if(num1 < num2) printf(“%d is less than %d”, num1, num2);printf(“%d is less than %d”, num1, num2); if(num1 > num2)if(num1 > num2) printf(“%d is greater than %d”, num1, num2);printf(“%d is greater than %d”, num1, num2); if(num1 <= num2)if(num1 <= num2) printf(“%d is less than or equal to %d”, num1, num2);printf(“%d is less than or equal to %d”, num1, num2); if(num1 >= num2)if(num1 >= num2) printf(“%d is greater than or equal to %d”, num1, num2);printf(“%d is greater than or equal to %d”, num1, num2); getch();getch(); }}
16. 16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh KeywordsKeywords  intint andand ifif areare keywordskeywords oror reserved wordsreserved words in Cin C  You cannot use them as variable names as CYou cannot use them as variable names as C compiler processes keywords in a different waycompiler processes keywords in a different way
17. 17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh KeywordsKeywords
18. 18. 18Rushdi Shams, Dept of CSE, KUET, Bangladesh QsQs 1.1. Every C program must have a function. What is its name?Every C program must have a function. What is its name? 2.2. What symbols are used to contain the body of the function?What symbols are used to contain the body of the function? 3.3. With what symbol every statement ends with?With what symbol every statement ends with? 4.4. Which function displays information on the screen? WhichWhich function displays information on the screen? Which header file is required for that function?header file is required for that function? 5.5. What does n represents?What does n represents? 6.6. Which function takes input from the user?Which function takes input from the user? 7.7. Which specifier is required to represent integer?Which specifier is required to represent integer? 8.8. Which keyword is used to make decision?Which keyword is used to make decision? 9.9. What are used to make a program readable?What are used to make a program readable?