Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

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

The document outlines the curriculum for BCSE102L, focusing on Structured and Object-Oriented Programming, specifically covering C programming fundamentals. It includes topics such as variables, data types, operators, control statements, and type conversions. The content is structured into modules that detail various programming concepts and their applications in C.

Uploaded by

golaniaman07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

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

The document outlines the curriculum for BCSE102L, focusing on Structured and Object-Oriented Programming, specifically covering C programming fundamentals. It includes topics such as variables, data types, operators, control statements, and type conversions. The content is structured into modules that detail various programming concepts and their applications in C.

Uploaded by

golaniaman07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

BCSE102L- Structured and object-

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

◦ Operators- Operator Precedence

◦ 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

 While and do while

◦ 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

• Short hand Operators (++, --)

++  increment by 1, --  Decrement by 1

e.g. a++ is equivalent to a = a + 1


b-- is equivalent to b = b – 1

Note: % operator cannot be used with real operands


Relational Operators
• Compare two quantities and depending on their
relation, taking certain decisions.

• It is used in decision making statements to


decide the course of action of a running
program.
Logical Operators
• Join multiple conditions or negate a condition i.e.,
Expression of this kind which combines two or
more relational expressions.

True in C means
Truth Table for ! (not) Operator non-zero & False
means zero
Logical Operators
Truth Table for AND

Truth Table for OR


Logical Operators
• Short Circuit operations:
– <condition-1> && <condition-2>
• If condition 1 is false, then condition 2 will not
be checked at all
– <condition 1> || <condition 2>
• If condition 1 is true, then condition 2 will not
be checked at all
For Example:
i. To check For The Largest among Three Numbers
(First_no > Second_no) && (First_no > Third_no)

ii. To Check for the value of choice is either ‘y’ or ‘Y’

( Choice == ‘y’) || (Choice == ‘Y’)


Assignment Operators
• “=“ used to assign the result of an expression to a variable
For Example:
Number = 100;
Temp = Number;
• Shorthand Operators (op=)
– Op may be any arithmetic operator. For Example:
– Assignment += 10; This statement is equivalent to
Assignment = Assignment + 10;

Statement with simple assignment Statement with short hand Operator


Operator

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

• Move(shift) the specific number of bits in


binary sequence in the right direction.
• Syntax: number >> number_of_positions_to_shift
Bitwise Left Shift Operator(<<)

• Move(shift) the specific number of bits in


binary sequence in the left direction.
• Syntax: number << number_of_positions_to_shift
Increment and Decrement Operator(++ & --)
• Increment Operator(++)  ++x , x++
• Increments the Value of the variable by 1.
• Two Categories
– pre-increment operator (or) prefix increment operator.
– Operator is before operand – increments value of the variable first,
incremented values is taken for evaluation
X=10;
Y=++X;
– post-increment operator (or) postfix increment operator.
– Operator is after operand – first expression is evaluated
and then the value of variable is incremented.
X=10;
Y=X++; ( Incremented value not used in the
expression)
Increment and Decrement Operator(++ & --)
• Decrement Operator(--)  --x , x--
• Decrements the Value of the variable by 1.
• Two Categories
– pre-decrement operator (or) prefix decrement operator.
– Operator is before operand – decrements value of the variable first,
decremented values is taken for evaluation
X=10;
Y=--X;
– post-decrement operator (or) postfix decrement operator.
– Operator is after operand – first expression is evaluated
and then the value of variable is decremented.
X=10;
Y=X--; (Decremented value not used in the expression)
Conditional or Ternary Operator
• It works on three operands which is used
for decision making.
• Syntax: Condition ? TrueExpression : FalseExpression

• Example: If(a>b)
x=a;
a=10; or else
b=15; x=b;
x=(a>b)? a:b

• Try Minimum of three numbers.


Special Operators
• Comma
• Sizeof
• Pointer Operators(& and *) - will discuss in module 3
• Member Selection Operators(. and ->) - will discuss in
module 4

Comma operator: Used to link the related expressions


together.
For example: value = (x=10, y=5, x+5);
Sizeof operator: returns the number of bytes the operand
occupies
For Example: m = sizeof(n) or sizeof(float) or
sizeof(char)
Operator Precedence
• “C” has precedence associated with it. This precedence is
used to determine how an expression involving more
than one operator is evaluated.
• Operators at the highest level are evaluated first.
• Operators at the same precedence are evaluated either
from left to right or from right to left, depending on the
level. This is known as associativity property of an
operator.
• Example:
if (x == 10 + 15 && y < 10)
if (x == 25 && y < 10)
• // Precedence rule: Addition operator has highest
priority than logical and relational operators.
Precedence of Arithmetic Operators
Precedence of Arithmetic Operators
Example :

#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.

Type Conversion Methods

Automatic Type Conversion Explicit Type Conversion


(Coercion) (Type Casting)
Implicit Type Conversion/Automatic Type
• When the type conversion is performed automatically
by the compiler without programmers intervention.
• The compiler converts all operands into the data type of
the largest operand.
RULES:
• char -> short int -> int ->
• unsigned int -> long -> unsigned ->
• long long -> float -> double -> long double
• If either of the operand is of type long double, then
others will be converted to long double and result will
be long double.
• Else, if either of the operand is double, then others are
converted to double.
• Else, if either of the operand is float, then others are
converted to float.
Implicit Type Conversions - Example
#include<stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character y
// y implicitly converted to int. ASCII value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
}
Output x=??, y=??
Implicit Type Conversions - Example
No Data Loss
as small to large conversion 100
Example: will be converted to float as
100.00

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.

• Explicit type conversion is also known as type casting.

• Type casting in c is done in the following form:

(data_type)expression;

• where, data_type is any valid c data type,


and expression may be constant, variable or an
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 = (float) (numerator/denominator);

result = numerator/(float)denominator;
Correct Type Casting!
The result is 2.5

Note: Every data type can’t be converted into other type


Try Yourself
1. To find the area(Formula= a2) and perimeter (4 a) of
a square.
2. To find the area of a circle(𝐴 = 𝜋𝑟 2 ). Use radius as a
variable and pi as a constant.
3. To convert Celsius to Fahrenheit and vice-versa.
C to F= ( * 1.8)+32
F to C= ( - 32)* 0.5556
1. To convert gram to kilogram and vice-versa.
2. To convert kilometers to miles and vice-versa.
3. To check whether a number is negative, positive or
zero.
4. To check input number for odd or even.
5. To check whether a number is divisible by 6 and 8 or
not.
6. To find the greatest among three numbers.
7. To check whether the given year is leap year or not.
Thank You

You might also like