[go: up one dir, main page]

0% found this document useful (0 votes)
12 views38 pages

PPS Unit-2

Uploaded by

Rashmi Bisht
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)
12 views38 pages

PPS Unit-2

Uploaded by

Rashmi Bisht
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
You are on page 1/ 38

Programming for Problem Solving

(BCS 101 / 201)

Unit - 2
FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Operators
• An operator is a symbol that tells the compiler to perform certain mathematical or logical
manipulations. They form expressions.
C operators can be classified as
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment or Decrement operators
6. Conditional operator
7. Bit wise operators
8. Special operators

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Arithmetic Operators
• All basic arithmetic operators are present in C.
operator meaning
+ add
- subtract
* multiplication
/ division
% modulo division(remainder)

• An arithmetic operation involving only real operands(or integer operands) is called real
arithmetic(or integer arithmetic). If a combination of arithmetic and real is called mixed
mode arithmetic.

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Relational Operators
• We often compare two quantities and depending on their relation take certain
decisions for that comparison we use relational operators.
operator meaning
< is less than
> is greater than
<= is less than or equal to
>= is greater than or equal to
== is equal to
!= is not equal to
• It is the form of:

ae-1 relational operator ae-2

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Logical Operators
• Logical operators are used to evaluate the truth value of an expression or a condition.
They allow programmers to combine multiple conditions and test them simultaneously.
Logical operators are essential in programming, as they help to control program flow
and determine the outcome of different operations.
• There are three logical operators in C: "&&" (logical AND), "||" (logical OR), and "!"
(logical NOT).
• The && operator returns true if both operands are true, otherwise false.
• The || operator returns true if at least one of the operands is true, otherwise false.
• The ! operator returns true if the operand is false, and false if the operand is true.

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Assignment Operators
• Assignment operators are used to assigning value to a variable. The left side operand of
the assignment operator is a variable and right side operand of the assignment operator
is a value. The value on the right side must be of the same data-type of the variable on
the left side otherwise the compiler will raise an error.
• The various types of the assignment operators are :
= Assigns values from right side operands to left side operand
+= It adds the right operand to the left operand and assign the result to the left operand.
-= It subtracts the right operand from the left operand and assigns the result to the left
operand.
/= It multiplies the right operand with the left operand and assigns the result to the left
operand.
*= It divides the left operand with the right operand and assigns the result to the left
operand.
%= It takes modulus using two operands and assigns the result to the left operand.

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Increment or Decrement operators
• The increment(++) and decrement operators(--) are important unary operators in C.
Unary operators are those which are applied on a single operand. The increment
operator increases the value of the variable by one and the decrement operator
decreases the value of the variable by one.

• Pre-increment Operator : x = ++a; and x = --a;


• Post increment Operator : x = a++; and x = a--;

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Conditional Operator
• The conditional statements are the decision-making statements which depends upon the
output of the expression. It is represented by two symbols, i.e., '?' and ':'.
• As conditional operator works on three operands, so it is also known as the ternary
operator.
• Syntax : expression1? expression2: expression3;

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Bitwise Operators
• C supports special operators known as bit wise operators for manipulation of data at bit
level. They are not applied to float or double.
1. & (bitwise AND): takes two numbers as operands and does AND on every bit of two
numbers. The result of AND is 1 only if both bits are 1.
2. | (bitwise OR): takes two numbers as operands and does OR on every bit of two
numbers. The result of OR is 1 if any of the two bits is 1.
3. ^ (bitwise XOR): takes two numbers as operands and does XOR on every bit of two
numbers. The result of XOR is 1 if the two bits are different.
4. << (left shift): takes two numbers, the left shifts the bits of the first operand, and
the second operand decides the number of places to shift.
5. >> (right shift): takes two numbers, right shifts the bits of the first operand, and the
second operand decides the number of places to shift.
6. ~ (bitwise NOT): takes one number and inverts all bits of it.

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Special Operators
• Special operators are used to perform specific operations that cannot be done with
normal arithmetic or logical operators. These operators are special because they have
their own unique syntax and functionality.
Operators Description
& This is used to get the address of the variable.
Example : &a will give address of a.

* This is used as pointer to a variable.


Example : * a where, * is pointer to the variable a.

Sizeof () This gives the size of the variable.


Example : size of (char) will give us 1.

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Expressions in C
• Expressions are the combination of variables, operands, and operators. The result would
be stored in the variable once the expressions are processed based on the operator's
precedence.
• An expression is a formula in which operands are linked to each other by the use of
operators to compute a value. An operand can be a function reference, a variable, an
array element or a constant.
• For example:
1. a-b;
2. a-b+c
3. a+b-(a*c)
• x = 9/2 + a-b;
The entire above line is a statement, not an expression. The portion after the equal is an
expression.

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Types of Expressions in C
• There are four types of expressions exist in C:
1. Arithmetic expressions
2. Relational expressions
3. Logical expressions
4. Conditional expressions

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Arithmetic Expressions
• An arithmetic expression is an expression that consists of operands and arithmetic
operators. An arithmetic expression computes a value of type int, float or double.
• The expressions are evaluated by performing one operation at a time. The precedence
and associativity of operators decide the order of the evaluation of individual operations.

• For Example:
6*2/ (2+1 * 2/3 + 6) + 8 * (8/4)

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Relational Expressions
• A relational expression is an expression used to compare two operands.
• It is a condition which is used to decide whether the action should be taken or not.
• In relational expressions, a numeric value cannot be compared with the string value.
• The result of the relational expression can be either zero or non-zero value. Here, the
zero value is equivalent to a false and non-zero value is equivalent to true.

For Example:
1. a*b == a+b
2. a+b*c > a*b + c

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Logical Expressions
• Relational expressions and arithmetic expressions are connected with the logical
operators, and the result after an evaluation is stored in the variable, which is either true
or false.

• For Example:
1. (a+b)>c && a<b
2. a>b || b>a

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Conditional Expressions
• A conditional expression is an expression that returns 1 if the condition is true otherwise
0.
• The general syntax of conditional expression is:
Exp1? Exp2: Exp3
From the given above expressions, the first expression (exp1) is conditional, and if the
condition is satisfied, then expression2 will be executed; otherwise, expression3 will be
performed.

For Example:
1. 2<3? 2 : 3
2. a*b>c ? true : false

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Type Conversion
• Type conversion in C is defined as if we assign any data type to another data type and
then call it “Type conversion”.
• Convert the value of one data type to another type. This is known as type conversion.
• There are two types of conversion in C:
1. Implicit Conversion (automatically)
2. Explicit Conversion (manually)

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Implicit Type Conversion
• In implicit type conversion, the value
of one type is automatically
converted to the value of another
type.
• All the data types of the variables are
upgraded to the data type of the
variable with the largest data type.

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Implicit Type Conversion (Example)
• #include <stdio.h>
int main(){
int x = 10; // integer x
char y = 'a'; // character c
x = x + y; // y implicitly converted to int. ASCII value of 'a' is 97
float z = x + 1.0; // x is implicitly converted to float
printf("x = %d, z = %f", x, z);
return 0;
}
Output:
x = 107, z = 108.000000

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Explicit Type Conversion
• This process is also called type casting and it is user-defined. Here the user can typecast
the result to make it of a particular data type.
• The syntax in C Programming: (type) expression
• Example:
void main(){
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
printf("sum = %d", sum);
}
Output:
sum = 2

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Operator precedence
• The precedence of operators determines which operator is executed first if there
is more than one operator in an expression.
For example:
int x = 5 - 17* 6;
• In C, the precedence of * is higher than - and =. Hence, 17 * 6 is evaluated first. Then
the expression involving - is evaluated as the precedence of - is higher than that of = .

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Associativity of Operators
• The associativity of operators determines the direction in which an expression is
evaluated.
For example:
b = a;
Here, the value of a is assigned to b, and not the other way around. It's because the
associativity of the = operator is from right to left.
• If two operators of the same precedence (priority) are present, associativity
determines the direction in which they execute.
• For example: 1 == 2 != 3
Here, operators == and != have the same precedence. And, their associativity is from
left to right. Hence, 1 == 2 is executed first. The expression above is equivalent to:
(1 == 2) != 3

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Operators Precedence & Associativity Table

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Operators Precedence & Associativity Table

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Operators Precedence & Associativity Table

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Conditional Branching
• Conditional Branching Statements in C are used to execute the specific blocks of code
on the basis of some condition (as per requirements). These type of Branching
statements in C enables the programmers to execute the code only and only certain
statements are met. The following are different types of conditional branching
statements in C.
1. If statement
2. If-else statement
3. Else-if statement
4. Nested if-else statement
5. Switch statement

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• if Statement
• This statement is used to execute the specific block of code if a certain condition is
evaluated to be true.
• Syntax of if Statement in C
if (condition)
{
//code to be executed if condition specified evaluates is true
}
• Here in the above syntax “condition” is a logical expression that is evaluated for being
true or false. If the condition is evaluated to be true, the code within curly braces will
be executed but if the condition is false, the code in braces will be skipped.

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• if Statement (Example)
#include<stdio.h>
void main()
{
int x = 10;
if (x > 5)
{
printf("x is greater than 5");
}
}
Output:
x is greater than 5

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• if – else Statement
• The else Statement in C is considered just the opposite of the if statement. This
statement is used to execute the code if the condition specified in the if statement
evaluates to false.
• Syntax of if-else Statement in C
if (condition)
{
// code executed if condition is true
}
else
{
// code executed if condition is false
}

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• if – else Statement (Example)
#include <stdio.h>
void main() {
int x = 10;
if (x < 5) {
printf("x is less than 5\n");
}
else {
printf("x is greater than or equal to 5\n");
}
}
Output:
x is greater than or equal to 5

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• else if Statement
• The else if statement in C is used when we want to check multiple conditions. It follows
an "if" statement and is executed if the previous "if" statement’s condition is false.
• Syntax of else-if Statement in C
if (condition1) {
// code to be executed if condition1 is true
}
else if (condition2) {
// code to be executed if condition2 is true and condition1 is false
}
else {
// code to be executed if condition1 and condition2 both are false
}

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• else if Statement(Example)
void main() {
int x = 10;
if (x > 15) {
printf("x is greater than 15");
}
else if (x > 5) {
printf("x is greater than 5 but less than or equal to 15");
}
else {
printf("x is less than or equal to 5");
}
}
Output:
x is greater than 5 but less than or equal to 15

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Nested if-else Statement
• Nested if means you can use one if statement inside another if statement.
• Syntax of nested if Statement in C
if( boolean_expression 1) {
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
else {
/* Executes when the boolean expression 2 is false */
}
else{
/* Executes when the boolean expression 1 is false */
}

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Nested if-else Statement(Example)
void main() {
Output:
int num;
Enter a number: 10
printf("Enter a number: ");
10 is positive.
scanf("%d", &num);
if (num > 0) {
Enter a number: -5
printf("%d is positive.\n", num);
-5 is negative.
} else {
if (num < 0) {
Enter a number: 0
printf("%d is negative.\n", num);
0 is zero.
} else {
printf("%d is zero.\n", num);
}
}
}

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Switch Statement
• The switch statement in C is used when we have multiple conditions to check. It is
often used as an alternative to multiple "if" and "else if" statements.
• Syntax of switch Statement in C
switch (expression) {
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
...
default:
// code to be executed if none of the cases match
break;
}
For PDF join our Telegram Channel from description
FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Switch Statement(Example)
void main()
default:
{
printf("x is not 1, 2, or 3");
int x = 2;
break;
switch (x)
}
{
case 1:
Output:
printf("x is 1");
x is 2
break;
case 2:
printf("x is 2");
break;
case 3:
printf("x is 3");
break;

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS ENGINEERING
BCS 101 / 201 (BEC-101/201)
• Unconditional Branching Statements
• Unconditional branching statements are used in C to change the normal flow of
program execution. These statements allow programmers to jump to a specific point in
their code regardless of any condition.
• There are the following types of unconditional branching statements in C.
1. goto Statement
2. break Statement
3. continue Statement

For PDF join our Telegram Channel from description


FUNDAMENTALS OF ELECTRONICS
Success Classes
ENGINEERING
Bijnor (BEC-101/201)

THANK YOU
For
Watching

For PDF join our Telegram Channel from description

You might also like