0% found this document useful (0 votes)
8 views14 pages

Chapter 2 Notes

The document provides an overview of programming instructions, focusing on type declarations, arithmetic instructions, and control instructions. It emphasizes the importance of declaring variables before use, the validity of arithmetic operations, and operator precedence. Additionally, it covers various operators, including arithmetic, relational, logical, and assignment operators, along with their respective priorities and usage in programming.

Uploaded by

vosipex610
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

Chapter 2 Notes

The document provides an overview of programming instructions, focusing on type declarations, arithmetic instructions, and control instructions. It emphasizes the importance of declaring variables before use, the validity of arithmetic operations, and operator precedence. Additionally, it covers various operators, including arithmetic, relational, logical, and assignment operators, along with their respective priorities and usage in programming.

Uploaded by

vosipex610
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Instructions

These are statements in a Program

Types

Type Declaration Control


Instructions Arithmetic Instructions
Instructions
Instructions
Variable
Type Declaration Instructions Declare var before using it

VALID we declared
INVALID
If we write

it before int a=b=c=1;


with out declaring the a,b,c
we used it then its incorrect we have to

int a = 22; int a = 22; write

int b = a; int b = a;
int a,b,c;
int a=b=c=1;

int c = b + 1; int c = b + 2; Its because 1ts the compiler


will understand 1 will be on
c then b=c and then a-b so
int d = 1, e; int d = 2, e; where the b came from so it
messed up so we need to
declare it first

int a,b,c;
int a,b,c = 1;
a = b = c = 1; Didnt mentioned in the class
why it is invalid
Arithmetic Instructions LHS RHS

Variable(single) = a+b*c/d
a+b if
int x, y = a+b;
is written then the value will
Operand 1 Operand 2 count on y not x

Operator

NOTE - single variable on the LHS


Arithmetic Instructions
+, -,*, /, %
VALID INVALID
a=b+c b+c=a
a=b*c a = bc
a=b/c a = b^c

Like 5^3 (in c we cant write this we have to write


pow(5,3) then it will count as 3 to the power 5,
like
int b,c;
int a= pow(b,c);

NOTE - pow(x,y) for x to the power y


N.B. To use "pow" we need to add a header file after include <stdio.h>
and that header file is #include <math.h>
#
Arithmetic Instructions It doesnt work in "float" that means
dont work in decimal value.

Modular Operator %
Returns remainder for int

3%2=1
-3 % 2 = -1
It Gives reminder after division like
2| 5 |2
4 here if we write 5%2
------- then 1 will be print
1
Arithmetic Instructions
Type Conversion

int op int int


Op means Operation
2+2 = 2
2+3.5 = 5.5
int op float float 2.5+3.5 = 6
Int take 2 space but But float take 4 space
that's why the compiler easily understand But in print we have to write %f that means float
otherwise the error will come
that he will need big space to print float
so it easily convery to float

float op float float One thing to notate if we want value of something like 3 / 2 it's supposed to
be 1.50000 but for that we have to write 3.0 divide 2 otherwise if we don't give .0
but we write a %f in printf still will not get the value. it will only print 0.000 so to get
the correct value the we have to put a decimal in the value. {It is discussed in the
video at 1:13:00}

printf("%f",3/2);........................Ans: 0.0000
But
printf("%f",3.0/2);........................Ans: 1.500000
Arithmetic Instructions
Operator Precedence
1st piority

*, /, % x = 4 + 9 * 10

2nd piority
+, -

= x=4*3/6*2 Look next page


Arithmetic Instructions
Associativity (for same precedence)
Left to Right

x=4*3/6*2
4*3/6*2
=12/6*2
=2*2
=4
Instructions
Control Instructions
Used to determine flow of program
a. Sequence Control

b. Decision Control

c. Loop Control

d. Case Control
Operators
a. Arithmetic Operators +, -, *, /, %

b. Relational Operators ==, <=, >=,!=

c. Logical Operators And &&, Or ||, Not !=

d. Bitwise Operators

e. Assignment Operators

f. Ternary Operator
If its true the answer will be 1
Operators and if false the answer will be 0

Relational Operators
== Equal

>, >= Equal or greater

<, <= equal or less

!= Not equal
Operators
Logical Operators
&& AND

|| OR

! NOT
Operator Precendence
Priority Operator
1 !
2 *, /, %
3 +, -
4 <, <=, >, >=
5 ==, !=
6 &&
7 ||
8 =
Operators
Assignment Operators
=
It express a=a*b
+= instead of repeating "a" we can
directly write this
same for all *+-/
-=

*=

/=

%=

You might also like