C: Operators and Keywords

18
Lecture 3 Lecture 3 Version 1.0 Version 1.0 Arithmetic Operator Arithmetic Operator Relational Operator Relational Operator Decision Making Decision Making Keywords Keywords

description

 

Transcript of C: Operators and Keywords

Page 1: C: Operators and Keywords

Lecture 3Lecture 3Version 1.0Version 1.0

Arithmetic OperatorArithmetic OperatorRelational OperatorRelational Operator

Decision MakingDecision MakingKeywordsKeywords

Page 2: C: Operators and Keywords

2Rushdi Shams, Dept of CSE, KUET, Bangladesh

Arithmetic OperatorsArithmetic Operators

Most C programs perform arithmetic Most C programs perform arithmetic calculations calculations

Page 3: C: Operators and Keywords

3Rushdi Shams, Dept of CSE, KUET, Bangladesh

Arithmetic operatorsArithmetic operators

The arithmetic operators are all The arithmetic operators are all binary binary operatorsoperators

The expression 3+7 contains binary The expression 3+7 contains binary operator + and two operator + and two operandsoperands 3 and 7 3 and 7

Integer division yields an integer Integer division yields an integer result. 17/4 is 4 result. 17/4 is 4

The modulus operator means the The modulus operator means the remainder after a division operation. remainder after a division operation. 17%4 is 1 17%4 is 1

Page 4: C: Operators and Keywords

4Rushdi Shams, Dept of CSE, KUET, Bangladesh

Rules of Operator Rules of Operator PrecedencePrecedence

1.1. Expressions or portion of Expressions or portion of expressions contained in pairs of expressions contained in pairs of parentheses are evaluated first. In parentheses are evaluated first. In case of nested parentheses, the case of nested parentheses, the inner most parentheses will be inner most parentheses will be evaluated first.evaluated first.

Page 5: C: Operators and Keywords

5Rushdi Shams, Dept of CSE, KUET, Bangladesh

Rules of Operator Rules of Operator PrecedencePrecedence

2.2. Multiplication, division and Multiplication, division and modulus operations are evaluated modulus operations are evaluated next. If an expression contains next. If an expression contains several *, / or % operations, several *, / or % operations, evaluations proceed from left to evaluations proceed from left to right. These operators are on the right. These operators are on the same level of precedence.same level of precedence.

Page 6: C: Operators and Keywords

6Rushdi Shams, Dept of CSE, KUET, Bangladesh

Rules of Operator Rules of Operator PrecedencePrecedence

3.3. Addition and subtraction are Addition and subtraction are evaluated last. If an expression evaluated last. If an expression contains several + or - operations, contains several + or - operations, evaluations proceed from left to evaluations proceed from left to right.right.

Page 7: C: Operators and Keywords

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?

Page 8: C: Operators and Keywords

8Rushdi Shams, Dept of CSE, KUET, Bangladesh

Brain Storm IIBrain Storm II

y=mx+cy=mx+c How do you write this expression in How do you write this expression in

C? Any parenthesis required?C? Any parenthesis required?

Page 9: C: Operators and Keywords

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 It is in algebraic format. How will it

be written in C? What will be the be written in C? What will be the order of precedence given by the C order of precedence given by the C compiler while it is written in C compiler while it is written in C format?format?

Page 10: C: Operators and Keywords

10Rushdi Shams, Dept of CSE, KUET, Bangladesh

Decision Making: Decision Making: Relational OperatorsRelational Operators

Executable C expressions either do Executable C expressions either do actionsactions (calculations or input or output) or make a (calculations or input or output) or make a decisiondecision

For example, in a program where the user will For example, in a program where the user will put his mark and want to know if he passed or put his mark and want to know if he passed or failed, you will have to do three things-failed, you will 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 2. Compare the mark with a predefined pass mark range. mark range. DecisionDecision

3. Provide that decision to user. 3. Provide that decision to user. ActionAction

Page 11: C: Operators and Keywords

11Rushdi Shams, Dept of CSE, KUET, Bangladesh

Decision Making: Decision Making: Relational OperatorsRelational Operators

The decision in C is done by a control The decision in C is done by a control structure called structure called ifif

It allows a program to make a decision It allows a program to make a decision based on the truth or falsity of some based on the truth or falsity of some statement of fact called statement of fact called conditioncondition

If the condition is true, If the condition is true, the body the body statementstatement will be executed otherwise will be executed otherwise not- it will execute the next statement not- it will execute the next statement after the after the ifif structure structure

Page 12: C: Operators and Keywords

12Rushdi Shams, Dept of CSE, KUET, Bangladesh

Decision Making: Decision Making: Relational OperatorsRelational Operators

Conditions in Conditions in ifif structures are structures are formed by using formed by using relational operatorsrelational operators

The relational operators have the The relational operators have the same level of precedence and they same level of precedence and they associate left to right associate left to right

Only Only equality operatorsequality operators have a lower have a lower level of precedence than others and level of precedence than others and they also associate left to right they also associate left to right

Page 13: C: Operators and Keywords

13Rushdi Shams, Dept of CSE, KUET, Bangladesh

Decision Making: Decision Making: Relational OperatorsRelational Operators

Page 14: C: Operators and Keywords

14Rushdi Shams, Dept of CSE, KUET, Bangladesh

Decision Making: Decision Making: Relational OperatorsRelational Operators

There is a subtle difference between There is a subtle difference between == and =. In case of =, it is == and =. In case of =, it is associated from right to left. a=b associated from right to left. a=b means the value of b is assigned to a means the value of b is assigned to a

Page 15: C: Operators and Keywords

15Rushdi Shams, Dept of CSE, KUET, Bangladesh

Decision Making: Decision Making: Relational OperatorsRelational Operators

#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();

}}

Page 16: C: Operators and Keywords

16Rushdi Shams, Dept of CSE, KUET, Bangladesh

KeywordsKeywords

int int and and ifif are are keywordskeywords or or reserved reserved wordswords in C in C

You cannot use them as variable You cannot use them as variable names as C compiler processes names as C compiler processes keywords in a different way keywords in a different way

Page 17: C: Operators and Keywords

17Rushdi Shams, Dept of CSE, KUET, Bangladesh

KeywordsKeywords

Page 18: C: Operators and Keywords

18Rushdi Shams, Dept of CSE, KUET, Bangladesh

QsQs

1.1. Every C program must have a function. What is its Every C program must have a function. What is its name?name?

2.2. What symbols are used to contain the body of the What symbols are used to contain the body of the function?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? Which function displays information on the screen?

Which header file is required for that function?Which 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?