Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in...

17
Lecture-2 Operators and Conditionals

Transcript of Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in...

Page 1: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

Lecture-2

Operators and Conditionals

Page 2: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

Variables(again???) Type: Representation of “bits” in

memory Variables: Name for a memory

object. Starts with letters, and may contain letters, numerals and underscores

Declaration: Start of program block. Combination of type and variable

Assignment: Value for the variable

Page 3: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

Example

int a, b; /* Declaration */

int a = 9; /* Decl. with Initial Value */

b = 2; /* Assignment/Definition */

type name

Page 4: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

scanf() Function Input to the program can be done

through scanf() function Format is same as printf(), except

for variable used is attached an “&”

For e.g. scanf(“%d”, &i);

Page 5: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

Arithmetic Operators

+ Addition

- Subtraction

* Multiplication

/ Division

% Remainder

unary +

unary - Negate

Page 6: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

test1.cFollow this link to test1.c on the class web page

Page 7: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

Math Library(math.h)cos(x) cos x

sin(x) sin x

tan(x) tan x

pow(x, y) xy

sqrt(x) Square root of x

exp(x) x

log(x) ln x

Note: For Trigonometric functions, angles are in radians

Page 8: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

hypot.c

Follow this link to hypot.c on the class web page

Page 9: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

In class Exercise -1

Write a program to calculate the Areaof a triangle.• Prompt the user to enter base and height• Use “float” variables

Page 10: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

Conditional Statements(if-else) if (expression) statement: Execute

the statement if the expression is true

if (expression) statement-1else statement-2Execute statement-1 if expression is true, else execute statement-2

Page 11: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

Relational OperatorsRelational operators return 1 if the condition is true, and 0 if the condition is false. They are used in expressions for conditionals

== Equals

!= Not equal to

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

Page 12: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

Logical Operators ! operator: !(expression) inverts

the value of the expression. For example: !0=1, !2.0=0

&& operator: expression1 && expression2 is true if and only if both expressions are true

|| operator: expression1 || expression2 is true if any one expression is true

Page 13: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

grader.c

Follow this link to grader.c on the class

web page

Page 14: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

Switch-case statementSwitch (expression) { case value1: statements1; break; case value2: statements2; break; default: default statements; break;}

Expression is an integer expression, and are matched againstCase values which also must be integers!

Page 15: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

calculator.c

Follow this link to calculator.c on the class webpage

Page 16: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

In class Exercise - 2

Modify the calculator.c program for floating pointvariables. You should support all the operationsas the original program.

Your program should print out an error messageincase of “divide by zero” error, and exit gracefully.

Page 17: Lecture-2 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,

Homework-2

Follow this link to Homework-2 on the class webpage