Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Lecture-2 Operators and Conditionals

2 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

3 Example int a, b; /* Declaration */ int a = 9; /* Decl. with Initial Value */ b = 2; /* Assignment/Definition */ type name

4 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);

5 Arithmetic Operators +Addition -Subtraction *Multiplication /Division %Remainder unary + unary -Negate

6 test1.c Follow this link to test1.c on the classtest1.c web page

7 Math Library(math.h) cos(x)cos x sin(x)sin x tan(x)tan x pow(x, y)xyxy sqrt(x)Square root of x exp(x) xx log(x)ln x Note: For Trigonometric functions, angles are in radians

8 hypot.c Follow this link to hypot.c on the classhypot.c web page

9 In class Exercise -1 Write a program to calculate the Area of a triangle. Prompt the user to enter base and height Use “float” variables

10 Conditional Statements(if-else) if (expression) statement: Execute the statement if the expression is true if (expression) statement-1 else statement-2 Execute statement-1 if expression is true, else execute statement-2

11 Relational Operators Relational 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

12 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

13 grader.c Follow this link to grader.c on the classgrader.c web page

14 Switch-case statement Switch (expression) { case value1: statements1; break; case value2: statements2; break; default: default statements; break; } Expression is an integer expression, and are matched against Case values which also must be integers!

15 calculator.c Follow this link to calculator.c on the classcalculator.c webpage

16 In class Exercise - 2 Modify the calculator.c program for floating point variables. You should support all the operations as the original program. Your program should print out an error message incase of “divide by zero” error, and exit gracefully.

17 Homework-2 Follow this link to Homework-2 on the class webpageHomework-2


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

Similar presentations


Ads by Google