3-Branching Statements - if, if-else, if-else if- else, nested conditional statements Looping Statements - while, do-while, for Switch statement - break and continue-19-12-2024
3-Branching Statements - if, if-else, if-else if- else, nested conditional statements Looping Statements - while, do-while, for Switch statement - break and continue-19-12-2024
oriented programming
Dr. P.SURESH
Associate Professor
School of Computer Science &
Engineering(SCOPE)
VIT,Vellore.
BCSE102L- Structured and Object-
Oriented Programming
Module-1:C Program Fundamentals -
Introduction
◦ Variables
◦ Reserved Words
◦ Data types
◦ Expressions
◦ Type Conversions
◦ I/O Statements
.
BCSE102L- Structured and Object-
Oriented Programming
Module-1: C Program Fundamentals –
Branching and Looping
◦ if, if-else, nested if, else-if ladder
◦ Switch Statement
◦ Goto Statement
◦ Looping
For
◦ Break Statement
◦ Continue Statement
.
OPERATORS AND EXPRESSIONS
C supports a rich set of built-in operators
◦ For Example: Basically we used =,+,-,*,/, & ……. etc
◦ Operator is a symbol that tells the computer
to perform certain mathematical or logical
manipulations.
◦ Arithmetic
◦ Relational Binary Operators
◦ Logical
◦ Assignment
◦ Bitwise
◦ Increment and Decrement Unary Operator
◦ Conditional Ternary Operator
◦ Special
Arithmetic Operators
• Perform basic arithmetic operations
++ increment by 1, -- Decrement by 1
True in C means
Truth Table for ! (not) Operator non-zero & False
means zero
Logical Operators
Truth Table for AND
a=a+1 a+=1
a=a-1 a-=1
a=a*(n+1) a*=n+1
a=a/(n+1) a/=n+1
a=a%b a%=b
Bitwise Operators
• Used for manipulation of data’s at bit level.
• It is mainly used in numerical computations for a faster
calculation because it consists of two digits – 1 or 0.
• Used for testing the bits, or shifting them right or left.
– Note: Bitwise operators not applied to float or double
Bitwise Operators(OR)
• Bitwise Inclusive OR (|)
– Bit by bit OR ing is done using the truth table of OR
Bitwise Operators(AND)
• Bitwise AND(&)
– Bit by bit AND ing is done using the truth table of
AND
Bitwise Operators(XOR)
• Bitwise XOR(^)
– Bit by bit XOR ing is done using the truth table of
XOR
Bitwise One’s Complement
Operator(~)
• Bitwise One’s Complement Operator(~)
– Bit by bit converts all the zero(0) bits to One(1) and
All One(1) bits to Zero(0).
Bitwise Right Shift Operator(>>)
• Example: If(a>b)
x=a;
a=10; or else
b=15; x=b;
x=(a>b)? a:b
#include <stdio.h>
int main()
{
float a,b,c,x;
a=9; b=12; c=3;
x= a - b / 3 + c * 2 - 1;
printf("x=%f", x);
return 0;
} Output ????
Arithmetic Expressions
• It is a meaningful combination of variables,
constants and operators arranged as per the
syntax of the language.
Example: Sum = a + b;
• SYNTAX :
<exp> = <exp> op <exp>/<var> op <var>/
<var> op <const>/<const> op <var>/
<const> op <const> /<const>/<var>
where <op> may be arithmetic / logical / relational operator
For E.g.
i. Assignment = 10;
ii. Calc = Number + 10;
iii. Choice = ‘y’
iv. Interest = (Principal * NoOfYears * RateOfInterest )/100;
Type Conversions in Expressions
• “C” Permits mixing of constants and variables of
different types.
• “C Converts a variable from one data type to another
data type.
int number=100;
float conversionValue;
conversionValue=number; Automatic Conversion with Data
Loss
conversionValue = 10.45; Value of number is 10
number = conversionValue;
Explicit Type Conversion
• Type conversion performed by the programmer.
(data_type)expression;
• For example:
x=(int)a+b*d;
Explicit Type Conversion
No Type Casting!
The result is 2.0
int numerator=5;
int denominator=2;
float result;
result = numerator/denominator; Wrong Type Casting!
The result is 2.0
result = numerator/(float)denominator;
Correct Type Casting!
The result is 2.5