0% found this document useful (0 votes)
14 views

Lec 7 Programming Fundamentals (2)

Uploaded by

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

Lec 7 Programming Fundamentals (2)

Uploaded by

rabeyaasif1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CSC113-Programming Fundaments

Lecture 07
Week 05
Ms. Noor-ul-Huda
Senior Lecturer-I
Department of Computer Science
College of Computer Science and Information Systems
[email protected]
Lecture outcomes:
◦ What are Logical operators?
◦ Examples of AND, OR and NOT
◦ Real World Examples
An Operator in Computer
Programming
◦ In computer programing, an operator is a character, symbol, keyword, or
combination of those.
◦ It determines what action gets performed on one or more operands.

◦ An operand is an individual data item that gets manipulated by the operator.


What Are The Different Types of
Operators in C Programming?
In C programming, operators fall into one of three categories:
1. Unary operators
2. Binary operators
3. Ternary operators
Unary operators
oUnary operators operate on a single operand. Some of the unary operators in C are:

oArithmetic operators such as the increment operator(++), which increments the value of the
operand by 1. And the decrement operator(--), which decrements the value of the operand by 1.
oLogical operators like the NOT(!) operator. This operator reverses the logical value of the
operand – it changes true to false and false to true.
oBitwise operators like the NOT(~) operator, which changes each 0 bit to 1 and each 1 bit to 0.
Binary operators
oBinary operators operate on two operands.
oSome of the binary operators in C are:
oArithmetic operators (+, -, *, /, %). These operators perform mathematical calculations on
numerical data such as addition, subtraction, multiplication, division, and finding the remainder.
oEquality/Relational operators (==, !=, >, <, >=, <=). These operators compare two values and
determine if one operand is greater than, less than, equal to, or not equal to the other operand.
oLogical/Conditional operators such as the AND(&&) and OR(||) operators.
oBitwise operators ((&, |, ^, <<, >>), which treat data items as a sequence of bits (that is, 0s and
1s).
oAssignment operators (=, +=, -=, *=, /=, %=), which assign a specific value to a variable.
The Ternary operator
oThe Ternary operator (?:) operates on three operands. The general syntax looks something
similar to the following:

(condition) ? expression1 : expression2;


oThe ternary operator is a conditional operator you can use as a shorthand for an if..else
statement. It performs comparisons and returns a result.
Logical operators
oThe logical operators evaluate the logical expression and return a result.

oThe result is always a Boolean value.


oA Boolean value determines whether the expression is true or false.
oThere are three logical operators in C programming:
1. Logical AND(&&)
2. Logical OR(||)
3. Logical NOT (!)
Logical AND (&&)
#include <stdio.h>
int main() {
int x = 5;
int y = 10;
if (x > 0 && y > 0) {
printf("Both x and y are greater than 0\n");
} else {
printf("At least one of x or y is not greater than 0\n");
}
return 0;
}
Logical OR (||) Operator:
#include <stdio.h>
int main() {
int age = 25;
int hasLicense = 1; // 1 represents true
if (age >= 18 || hasLicense) {
printf("You can drive!\n");
} else {
printf("You cannot drive.\n");
}
return 0;
}
Logical NOT (!) Operator:
#include <stdio.h>
int main() {
int isRaining = 0; // 0 represents false
if (!isRaining) {
printf("It's not raining. You can go outside.\n");
} else {
printf("It's raining. Stay indoors.\n");
}
return 0;
}
#include <stdio.h>
int main() {
int temperature = 28;
int isSunny = 1; // 1 represents true

if (temperature > 30 && isSunny) {


printf("It's a hot and sunny day.\n");
} else if (temperature > 30 || isSunny) {
printf("It's either hot or sunny (or both).\n");
} else {
printf("It's neither hot nor sunny.\n");
}
return 0;
}
Real world examples of logical
operators
oUser Authentication:
Logical AND operator (&&) is often used to check multiple conditions during user authentication.

o Online Shopping Cart:


When managing an online shopping cart, the logical OR operator (||) can be used.

oTemperature Control System:


In a climate control system, logical operators can be used to decide whether to turn on heating or cooling.

oAccess Control in Buildings:


Logical operators can be used to control access to secure areas. To open a high-security door, both a valid key card
AND biometric recognition (fingerprint or retina scan) may need to be true.

You might also like