KEMBAR78
LDP Unit-3 Material | PDF | Boolean Data Type | Software Development
0% found this document useful (0 votes)
23 views21 pages

LDP Unit-3 Material

Operators and expressions are fundamental concepts in C programming. [1] Operators are symbols that represent actions to be performed on operands, like addition (+). [2] Expressions combine values and operators to produce a single value. [3] There are several types of operators in C, including arithmetic, relational, logical, assignment, bitwise, conditional, and increment/decrement operators.

Uploaded by

Nidhi Sindal
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)
23 views21 pages

LDP Unit-3 Material

Operators and expressions are fundamental concepts in C programming. [1] Operators are symbols that represent actions to be performed on operands, like addition (+). [2] Expressions combine values and operators to produce a single value. [3] There are several types of operators in C, including arithmetic, relational, logical, assignment, bitwise, conditional, and increment/decrement operators.

Uploaded by

Nidhi Sindal
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/ 21

Silver Oak Group of Institutes

Silver Oak College of Computer Application

Created By :- Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

Operators & Expression: -


Operators:-An operator is a symbol or keyword that represents a specific action to be
performed on one or more operands (variables, constants, or expressions) to produce a result.

For example, in the expression "5 + 3", the "+" symbol is an operator that performs the
addition operation on the operands 5 and 3 to produce the result 8.

Expression: - An expression is a combination of one or more values, variables, operators,


and function calls that are evaluated to produce a single value.

For example, in the expression "2 + 3", the values 2 and 3 are combined with the "+" operator
to produce the result 5.

Expressions can be as simple as a single value or variable, or they can be more complex and
involve multiple operators and function calls.

Here are some examples of expressions in the C programming language:

 5 + 3 (evaluates to 8)
 x * y (evaluates to the product of variables x and y)
 (a > b) && (c < d) (evaluates to true or false depending on the values of variables a, b, c, and
d)
Types of Operators and Expression:-

Types of Operators

1. Arithmetic operators: These operators perform basic mathematical operations on


numeric operands. The arithmetic operators in C are + (addition), - (subtraction), *
(multiplication), / (division), and % (modulo or remainder).

a) Addition: int sum = 2 + 3; (This will set the value of sum to 5.)

b) Subtraction: int difference = 5 - 2; (This will set the value of difference to 3.)

c) Multiplication: int product = 4 * 6; (This will set the value of product to 24.)

d) Division: float quotient = 10.0 / 3.0; (This will set the value of quotient to approximately
3.333333.)

Created By :- Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

e) Modulus: int remainder = 10 % 3; (This will set the value of remainder to 1, which is the
remainder when 10 is divided by 3.)

Example:-
#include <stdio.h>

int main() {
int a = 10;
int b = 5;

int sum = a + b;
int difference = a - b;
int product = a * b;
float quotient = (float) a / (float) b;
int remainder = a % b;

printf("Sum: %d\n", sum);


printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %.2f\n", quotient);
printf("Remainder: %d\n", remainder);

return 0;
}
Output:-
Sum: 15
Difference: 5
Product: 50
Quotient: 2.00
Remainder: 0

2. Relational operators: These operators compare two operands and return a Boolean
value (true or false) depending on whether the relation is true or false. The relational
operators in C are == (equal to), != (not equal to), > (greater than), < (less than), >=
(greater than or equal to), and <= (less than or equal to).

Created By :- Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

Example:-
#include <stdio.h>

int main() {
int a = 5;
int b = 10;

// Greater than operator


if (a > b) {
printf("a is greater than b\n");
} else {
printf("b is greater than or equal to a\n");
}

// Less than or equal to operator


if (a <= b) {
printf("a is less than or equal to b\n");
} else {
Created By :- Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

printf("a is greater than b\n");


}

// Equal to operator
if (a == b) {
printf("a is equal to b\n");
} else {
printf("a is not equal to b\n");
}

return 0;
}
Output:-

b is greater than or equal to a


a is less than or equal to b
a is not equal to b

3.Logical operators: These operators combine two or more Boolean expressions and return
a Boolean value depending on the result. The logical operators in C are && (logical AND),
|| (logical OR), and ! (logical NOT).

Example:-
#include <stdio.h>

Created By :- Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

int main() {
int a = 5;
int b = 10;
int c = 15;

// Logical AND operator


if (a < b && b < c) {
printf("a is less than b and b is less than c\n");
} else {
printf("either a is greater than or equal to b, or b is greater than or equal to c\n");
}

// Logical OR operator
if (a < b || b > c) {
printf("either a is less than b, or b is greater than c\n");
} else {
printf("a is greater than or equal to b, and b is less than or equal to c\n");
}

// Logical NOT operator


if (!(a < b)) {
printf("a is greater than or equal to b\n");
} else {
printf("a is less than b\n");
}

return 0;
}
Output:-

a is less than b and b is less than c


either a is less than b, or b is greater than c
a is less than b

4.Assignment operators: These operators assign a value to a variable. The assignment


operators in C are = (simple assignment), += (add and assign), -= (subtract and assign), *=
(multiply and assign), /= (divide and assign), and %= (modulo and assign).

Created By :- Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

Example:-
#include <stdio.h>

int main() {
int a = 5;
int b = 10;

// Simple assignment
int c = a + b;
printf("c = %d\n", c);

// Compound assignment
a += b;
printf("a = %d\n", a);

return 0;
}
Output:-
c = 15
a = 15

5.Bitwise operators: These operators perform operations on the individual bits of a binary
number.
The bitwise operators in C are & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), << (left
shift), and >> (right shift).

Created By :- Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

Example:-

#include <stdio.h>

int main() {
int a = 5; // binary representation: 0000 0101
int b = 3; // binary representation: 0000 0011

int c = a & b; // bitwise AND


printf("a & b = %d\n", c); // expected output: 1

int d = a | b; // bitwise OR
printf("a | b = %d\n", d); // expected output: 7

int e = a ^ b; // bitwise XOR


printf("a ^ b = %d\n", e); // expected output: 6

int f = ~a; // bitwise NOT


printf("~a = %d\n", f); // expected output: -6

return 0;
}

Created By :- Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

6.Conditional operator: This operator is also known as the ternary operator. It is used to
evaluate a Boolean expression and return one of two values depending on whether the
expression is true or false. The conditional operator in C is ?:.

Syntax\:- The conditional operator can be in the form


variable = Expression1 ? Expression2 : Expression3;
Example
#include <stdio.h>

int main() {
int a = 5;
int b = 3;
int max = (a > b) ? a : b;
printf("The maximum of %d and %d is %d\n", a, b, max);
return 0;
}
Outpout:- The maximum of 5 and 3 is 5

7.Increment and decrement operators: These operators are used to increment or


decrement the value of a variable by one.
The increment operator is ++, and the decrement operator is --.

Example:-
#include <stdio.h>

int main() {
int a = 5;
int b = 3;

a++; // increment a by 1
printf("a after increment: %d\n", a); // expected output: 6

b--; // decrement b by 1
printf("b after decrement: %d\n", b); // expected output: 2

int c = ++a; // increment a and assign to c


printf("a after pre-increment: %d\n", a); // expected output: 7
printf("c after pre-increment: %d\n", c); // expected output: 7

int d = b--; // assign b to d and then decrement b


printf("b after post-decrement: %d\n", b); // expected output: 1
printf("d after post-decrement: %d\n", d); // expected output: 2

Created By :- Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

return 0;
}
8. Comma operator: This operator is used to separate two or more expressions in a
statement. The comma operator in C is, (comma).

Types of Expression

Expressions can be classified into several types based on their behavior and the type of
values they produce. Some of the commonly recognized types of expressions are:

1. Numeric expressions: These expressions involve numeric values and arithmetic


operators to perform mathematical operations such as addition, subtraction,
multiplication, and division.
For example, 2 + 3, 5 * 6, and 12 / 4 are all numeric expressions.

2. Boolean expressions: These expressions involve logical operators to compare values


and produce a Boolean result of either true or false. For example, 5 > 3 and 2 == 2 are
Boolean expressions.

3. String expressions: These expressions involve string values and operators that can
concatenate or compare strings. For example, "Hello" + "World" concatenates the two
strings to produce "HelloWorld".

4. Conditional expressions: These expressions, also known as ternary expressions,


involve a condition followed by two expressions that are evaluated based on the result of
the condition. For example, (x > y)? x: y evaluates to x if x is greater than y,
otherwise, it evaluates to y.

5. Function call expressions: These expressions involve calling a function and passing
one or more arguments to it. The function can return a value that is used in the
expression. For example, sqrt(25) returns the square root of 25.

6. Array expressions: These expressions involve accessing elements of an array using an


index. For example, myArray[3] accesses the fourth element of the array myArray.

Precedence & Associativity:-

Precedence
precedence refers to the order in which operators are evaluated in an expression. It
determines which operator is evaluated first and which operator is evaluated next,
based on their level of precedence.

Created By :- Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

For example, in the expression "3 + 4 * 5", the multiplication operator has higher
precedence than the addition operator, so it is evaluated first, resulting in the value 23.
If the addition operator had higher precedence, the expression would be evaluated as "
(3 + 4) * 5" and result in the value 35.

Associativity:-

Associativity refers to the order in which operators with the same level of precedence
are evaluated in an expression. It determines whether the operators are evaluated from
left to right (left-associative) or from right to left (right-associative).

For example, in the expression "a - b - c", both the subtraction operators have the
same level of precedence. If the operators are left-associative, the expression is
evaluated as "(a - b) - c", which subtracts "b" from "a" first, and then subtracts the
result from "c". If the operators are right-associative, the expression is evaluated as "a
- (b - c)", which subtracts "c" from "b" first, and then subtracts the result from "a".

Console based I/O and related built-in I/O function:-


The process of reading input data from the user through the console or terminal, and
displaying output data to the user on the same console or terminal. In C, console-based I/O is
typically performed using the built-in I/O functions provided by the C standard library.

The most commonly used built-in functions for console-based I/O in C are:

1.printf(): This function is used to print output data to the console. It takes a format string as its first
argument, which specifies the format of the output, and any additional arguments that correspond to
the format placeholders in the format string.
Example:-
int num = 10;
printf("The value of num is %d\n", num);
output:-
The value of num is 10

2.scanf(): This function is used to read input data from the console. It takes a format string as its first
argument, which specifies the format of the input, and pointers to the variables where the input data
should be stored.
Created By :- Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

Example:- int num;


printf("Enter a number: ");
scanf("%d", &num);

3.getchar(): This function is used to read a single character from the console. It waits for the user to
enter a character and returns the ASCII value of the character.
Example:- char ch;
printf("Enter a character: ");
ch = getchar();

4.putchar(): This function is used to print a single character to the console. It takes a character as its
argument and displays it on the console.
Example:- char ch = 'A';
putchar(ch);

Concept of Header File and #include , #define


A header file is a file containing declarations and definitions for functions, variables, and other constructs
that can be used in a program.

Header files typically have the file extension ".h".

#include :- The #include directive is used in C and C++ to include the contents of a header file in a source
file. When the pre-processor encounters an #include directive, it replaces the directive with the contents of
the specified header file.
Example:- #include "stdio.h"

#define:- The #define directive is used in C and C++ to create a macro, which is a name that represents a
value or a block of code.
When the pre-processor encounters a #define directive, it replaces any occurrences of the defined name
with the specified value or code.

Example:-
#define PI 3.14159
Created By :- Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

You can then use the "PI" macro in your code to represent the value of pi, like this:
double circumference = 2 * radius * PI;

Decision-Making Structure:

1. If statement: This is the most basic decision-making structure that checks if a particular
condition is true or false. If the condition is true, then the code inside the if block is executed,
otherwise it is skipped.
Example:
if (x > 0) {
// Do something
}

2. If-else statement: This structure is similar to the if statement, but it also includes an else
block that is executed if the condition is false.
Example:
if (x > 0) {
// Do something
}
else
{
// Do something else
}
3. Nested If-else statement: This structure is used when multiple conditions need to be
checked, and it involves nesting if-else statements inside each other.
Example:
if (x > 0) {
if (y > 0) {
// Do something
}
else
{
// Do something else
}
}
else
{
// Do something else
Created By :- Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

4. Switch statement: This structure is used when a variable need to be compared against
multiple values, and each value has a different code block to execute.

Example:
switch (day)
{
case 1:
// Code block for Monday
break;
case 2:
// Code block for Tuesday
break;
case 3:
// Code block for Wednesday
break;
// ... default:
// Code block for all other days
break;
}
Example :-
#include <stdio.h>

int main() {
int choice;
printf("Choose an option:\n");
printf("1. Print 'Hello'\n");
printf("2. Print 'World'\n");
printf("3. Print 'Goodbye'\n");
scanf("%d", &choice);

switch(choice) {
case 1:
printf("Hello\n");
break;
case 2:
printf("World\n");
break;
case 3:
printf("Goodbye\n");
break;
default:
Created By :- Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

printf("Invalid choice\n");
}

return 0;
}
Output;- Choose an option:
1. Print 'Hello'
2. Print 'World'
3. Print 'Goodbye'
1
Hello
 Control Structure& Array:,
Loop Control Structure

such as while, do-while, and for, are used in programming to repeat a set of instructions a certain
number of times or until a specific condition is met.

1. While:-The while loop executes a set of instructions repeatedly while a condition is true.
The syntax for a while loop is as follows:

while (condition) {
// code to execute while the condition is true
}
Example:- #include <stdio.h>

int main() {
int i = 1;
while (i <= 5) {

printf("%d\n", i);
i++;
}
return 0;
}
2. Do-While:-The do-while loop is similar to the while loop, except that it executes the code
block at least once, and then checks the condition. The syntax for a do-while loop is as follows:

do {
// code to execute at least once

} while (condition);
Created By :- Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

Example:-
#include <stdio.h>

int main()
{
int i = 1;
do {
printf("%d\n", i);

i++;
} while (i <= 5);
return 0;
}
3. For Loop:-The for loop is used when you know the number of times you want to repeat a set
of instructions. It has three parts: initialization, condition, and update. The syntax for a for loop is as
follows:
for (initialization; condition; update)
{
// code to execute repeatedly
}

Example:- #include <stdio.h>

int main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}

4 Nested loop:- the nested loop is a loop inside another loop. It is used when we want to iterate
over a collection of collections or perform a task repeatedly with multiple levels of iteration.

Example:-
#include <stdio.h>

Int main() {
Created By :- Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

int i, j;
for (i = 1; i <= 3; i++) { // outer loop
for (j = 1; j <= 4; j++){ // inner loop
printf("%d %d\n", i, j);
}
}
return 0;
}
5.Array:- An array is a data structure that stores a collection of values of the same data type in a
contiguous memory block. The values in an array are identified by an index, which is a non-negative
integer that represents the position of the value in the array.
Example:- my _array = [3, 8, 1, 0, 5,-2,32]

#include <stdio.h>

int main() {
int my_array[7]; // declaration of an array of 7 integers
int i;

// initializing the array elements


my_array[0] = 3;
Created By :- Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

my_array[1] = 8;
my_array[2] = 1;
my_array[3] = 0;
my_array[4] = 5;
my_array[5] =-2;
my_array[6] =32;

// accessing array elements using a for loop


for (i = 0; i < 7; i++) {
printf("my_array[%d] = %d\n", i, my_array[i]);
}

return 0;
}

 Other Statements
1. break:
The break statement is used to exit from a loop or a switch statement. When the break is encountered
inside a loop or a switch statement, the control is transferred to the statement following the loop or
the switch.

Syntax:
break;
Example:-
#include <stdio.h>

int main() {
int i;

for (i = 1; i <= 10; i++) {


Created By :- Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

if (i == 5) {
break;
}
printf("%d\n", i);
}

return 0;
}
In this example, we use a for loop to print the values 1 to 10. When the value of i becomes 5, we use
the break statement to exit the loop. Therefore, the loop only prints the values 1 to 4.

2. continue:
The continue statement is used to skip over the current iteration of a loop and continue with the next
iteration.

Syntax:
Continue;
Example:-

#include <stdio.h>

int main() {
int i;

for (i = 1; i <= 10; i++) {


if (i % 2 == 0) {
continue;
}
printf("%d\n", i);
Created By :- Kinjal Mehta
Silver Oak Group of Institutes
Silver Oak College of Computer Application

return 0;
}

3.goto:
The goto statement is used to transfer control to a labeled statement in the same function. It is
generally considered a bad practice to use goto, as it can make the code hard to read and debug.
Syntax:

goto label;
...
label: statement;
Example:-
#include <stdio.h>

int main() {
int i;

for (i = 1; i <= 10; i++) {


if (i == 5) {
goto end;
}
printf("%d\n", i);
}

end:
printf("Loop ended.\n");

Created By :- Kinjal Mehta


Silver Oak Group of Institutes
Silver Oak College of Computer Application

return 0;
}
4.exit:
The exit function is used to terminate the program and return a value to the operating system. It can
be called from any function in the program.

Syntax:
exit(status);
Example:-
#include <stdio.h>
#include <stdlib.h>

int main() {
int num;

printf("Enter a positive number: ");


scanf("%d", &num);

if (num < 0) {
printf("Error: Negative number entered.\n");
exit(1);
}

printf("Square of %d is %d.\n", num, num * num);

return 0;
}

Created By :- Kinjal Mehta

You might also like