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

Programming Concept

C operators are used to perform operations on variables and values. The main types of operators in C are arithmetic, assignment, relational, logical, and bitwise operators. Increment and decrement operators are also used to increase or decrease the value of a variable by 1. Operators are evaluated based on their precedence and associativity rules. Control statements like if/else, switch, loops are used to control the flow of a program based on conditions.

Uploaded by

daudimtui1889
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 views

Programming Concept

C operators are used to perform operations on variables and values. The main types of operators in C are arithmetic, assignment, relational, logical, and bitwise operators. Increment and decrement operators are also used to increase or decrease the value of a variable by 1. Operators are evaluated based on their precedence and associativity rules. Control statements like if/else, switch, loops are used to control the flow of a program based on conditions.

Uploaded by

daudimtui1889
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/ 35

C OPERATORS

Operators are used to perform operations on variables and values.

C divides the operators into the following groups:


i. Arithmetic operators
ii. Assignment operators
iii. Relational operators
iv. Logical operators
v. Bitwise operators
vi. Increment and Decrement Operators
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.

Operator Name Meaning Example


+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y


% Modulus Returns the division remainder x%y
++ Increment Increases the value of a variable by 1 ++x

-- Decrement Decreases the value of a variable by 1 --x


Example:
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;

c = a+b;
printf("a+b = %d \n",c);

c = a-b;
printf("a-b = %d \n",c);

c = a*b;
printf("a*b = %d \n",c);

c = a/b;
printf("a/b = %d \n",c);

c = a%b;
printf("Remainder when a divided by b = %d \n",c);

return 0;
}

OUTPUT
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
Assignment Operators
Assignment operators are used to assign values to variables.
Operator Example Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Example:
#include <stdio.h>
int main()
{
int a = 5, c;

c = a; // c is 5
printf("c = %d\n", c);

c += a; // c is 10 Output
printf("c = %d\n", c);

c -= a; // c is 5 c=5
printf("c = %d\n", c); c = 10
c=5
c *= a; // c is 25
printf("c = %d\n", c);
c = 25
c=5
c /= a; // c is 5 c=0
printf("c = %d\n", c);

c %= a; // c = 0
printf("c = %d\n", c);

return 0;
}
Relational Operators
Comparison operators are used to compare two values (or
variables). This is important in programming, because it helps us to
find answers and make decisions.

The return value of a relational operator is either 1 or 0, which


means true (1) or false (0).
Operator Meaning Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


Example:
#include <stdio.h>
int main()
{ Output
int a = 5, b = 5, c = 10;
5 == 5 is 1
printf("%d == %d is %d \n", a, b, a == b); 5 == 10 is 0
printf("%d == %d is %d \n", a, c, a == c); 5 > 5 is 0
printf("%d > %d is %d \n", a, b, a > b); 5 > 10 is 0
printf("%d > %d is %d \n", a, c, a > c); 5 < 5 is 0
printf("%d < %d is %d \n", a, b, a < b); 5 < 10 is 1
printf("%d < %d is %d \n", a, c, a < c); 5 != 5 is 0
printf("%d != %d is %d \n", a, b, a != b); 5 != 10 is 1
printf("%d != %d is %d \n", a, c, a != c); 5 >= 5 is 1
printf("%d >= %d is %d \n", a, b, a >= b); 5 >= 10 is 0
printf("%d >= %d is %d \n", a, c, a >= c); 5 <= 5 is 1
printf("%d <= %d is %d \n", a, b, a <= b); 5 <= 10 is 1
printf("%d <= %d is %d \n", a, c, a <= c);

return 0;
}
Logical Operators

Logical operators are used to determine the logic between variables or values:

Operator Name Description Example


&& Logical Returns true if both x < 5 && x < 10
and statements are true

|| Logical or Returns true if one of the x < 5 || x < 4


statements is true

! Logical not Reverse the result, returns !(x < 5 && x < 10)
false if the result is true
Example:

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n", result);
Output
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
(a == b) && (c > b) is 1
result = (a == b) || (c < b); (a == b) && (c < b) is 0
printf("(a == b) || (c < b) is %d \n", result); (a == b) || (c < b) is 1
(a != b) || (c < b) is 0
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result); !(a != b) is 1
!(a == b) is 0
result = !(a != b);
printf("!(a != b) is %d \n", result);

result = !(a == b);


printf("!(a == b) is %d \n", result);

return 0;
}
Bitwise Operators
In the arithmetic-logic unit (which is within the CPU), mathematical operations
like: addition, subtraction, multiplication and division are done in bit-level. To
perform bit-level operations in C programming, bitwise operators are used.

Operators Meaning of operators


& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise compliment
<< Shift left
>> Shift right
Increment and Decrement Operators
C programming has two operators increment ++ and decrement -- to change
the value of an operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement -- decreases the


value by 1. These two operators are unary operators, meaning they only
operate on a single operand.
#include <stdio.h>
int main()
{ Output
int a = 10, b = 100;
float c = 10.5, d = 100.5;
++a = 11
printf("++a = %d \n", ++a); --b = 99
printf("--b = %d \n", --b); ++c = 11.500000
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d); --d = 99.500000

return 0;
}
Operator Description Precedence Associativity

() Function call Square 1 L-R (left to right)


[] brackets.
+ Unary plus 2 R-L (right to left)
- Unary minus
++ Increment
-- ! Decrement
~ Not operator
* Complement
& Pointer operator
sizeof Address operator
Sizeof operator
* Multiplication 3 L-R (left to right)
/ Division
% Modulo division
+ Addition Subtraction 4 L-R (left to right)
-
<< Left shift Right 5 L-R (left to right)
>> shift
< <= > >= Relational Operator 6 L-R (left to right)

= = != Equality Inequality 7 L-R (left to right)

& Bitwise AND 8 L-R (left to right)


^ Bitwise XOR 9 L-R (left to right)
| Bitwise OR 10 L-R (left to right)
&& Logical AND 11 L-R (left to right)
|| Logical OR 12 L-R (left to right)
?: Conditional 13 R-L (right to left)
= *= /= %= += Assignment operator 14 R-L (right to left)
-= &= ^= <<=
>>=
, Comma operator 15 L-R (left to right)
CONTROL STATEMENTS
In C programming, a control statement is a statement that alters
the flow of execution of a program. We use control statements to
determine the order in which the instructions within a program are
executed. They allow us to make decisions, repeat actions, and
control the flow of our program based on certain conditions.

Types of Control Statements in C:


i. Selection Statements (Conditional Statements)
ii. Iteration Statements (Loops)
Selection Statements (Conditional Statements)
Selection statements, also known as conditional statements,
are fundamental part of C programming language. They allow you
to make decisions in your code by executing certain blocks of code
based on the evaluation of conditions.

We have primarily four types of selection statements:


1. if
2. if-else
3. Nested else-if
4. Switch
If statement
we use the if statement for conditional branching. It allows a
program to evaluate if a given condition is true or false and execute
a block of code accordingly.

Syntax:
if (condition)
{
// statements to execute if condition is true
}
Example:

#include <stdio.h>
int main() {
int number;

printf("Enter an integer: ");


scanf("%d", &number);

if (number < 0)
{
printf("You entered %d.\n", number);
}

printf("The if statement is easy.");

return 0;
}
if-else statement
The if-else statement is a fundamental control flow structure that
allows for conditional execution of code. It tests a condition: if the
condition is true, one block of code is executed, and if the condition
is false, another block (or none at all) is executed.
Syntax :

if (condition)
{
// statements to execute if condition is true
}
else
{
// statements to execute if condition is false
}
Example:
// Check whether an integer is odd or even

#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);

// True if the remainder is 0


if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}

return 0;
}
if...else Ladder
The if...else statement executes two different codes depending upon
whether the test expression is true or false. Sometimes, a choice ha
to be made from more than 2 possibilities.

The if...else ladder allows you to check between multiple test


expressions and execute different statements.
Example:
Syntax:
#include <stdio.h>
if (test expression1) {
// statement(s) int main() {
} int age;
else if(test expression2) {
// statement(s) printf("Enter your age: ");
} scanf("%d", &age);
else if (test expression3) {
// statement(s) if (age < 18) {
} printf("Ticket Price: $7 (Child)\n");
. } else if (age >= 18 && age < 60) {
. printf("Ticket Price: $12 (Adult)\n");
else { } else {
// statement(s) printf("Ticket Price: $9 (Senior)\n");
} }

return 0;
}
Nested if...else
It is possible to include an if...else statement inside the body of
another if...else statement.
Syntax :

if (outer condition) {
if (inner condition1) {
// Code for inner condition1
} else if (inner condition2) {
// Code for inner condition2
} else {
// Code for other cases within outer condition
}
} else if (another outer condition) {
// Code for another outer condition
} else {
// Code for cases not covered by any condition
}
Example:

#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

if (number1 >= number2) {


if (number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
else {
printf("Result: %d > %d", number1, number2);
}
}
else {
printf("Result: %d < %d",number1, number2);
}

return 0;
}
switch statement in C
The switch statement is a control flow statement in C (and many other
programming languages) that allows you to choose one of several
possible code blocks to execute based on the value of an expression.
It’s often used as a more concise alternative to a series of if-else
statements when you need to compare a single value against multiple
possible values.
Syntax :

switch (expression) {
case value1:
// code will be executed if expression equals value1
break;
case value2:
// code will be executed if expression equals value2
break;
// additional cases...
default:
// code will be executed if none of the above cases
match
}
Example:
#include <stdio.h>

int main() {
char rideType = 'P'; // U for UberX, P for UberPool, L for UberLux

switch (rideType) {
case 'U':
printf("You've selected UberX. A comfortable ride for up to 4.\n");
break;
case 'P':
printf("You've selected UberPool. Share your ride and save!\n");
break;
case 'L':
printf("You've selected UberLux. Ride in style!\n");
break;
default:
printf("Invalid selection. Please choose a valid ride type.\n");
}
return 0;
}
Iteration Statements (Loops)
We use loops to execute a block of code repeatedly based on a
condition or a set of conditions.

We have primarily three types of iteration statements:


1. for loop
2. while loop
3. do-while loop
for loop in C
The for loop provides a concise way to iterate over a block of code
using an initializer, a condition, and an iterator. We commonly use it
when the number of iterations is known beforehand.

It has three main parts:


Initialization: Executed once at the beginning of the loop. This step
allows you to declare and initialize any loop control variables.
Condition: Evaluated before each iteration. If the condition
evaluates to true (non-zero), the loop body is executed. If it
evaluates to false (zero), the loop is terminated.
Update: Executed after each iteration. It’s typically used to update
loop control variables.
Syntax : Example:

for (initialization; condition; update) #include <stdio.h>


{
// code to be executed in each iteration of int main() {
the loop; for(int i = 1; i <= 7; i++) {
} printf("%d\n", i);
}
return 0;
}
while loop in C
A while loop is a control flow structure that allows for a piece of
code to be executed repeatedly as long as a particular condition is
true.

Here, the condition is evaluated before the execution of the loop’s


body. If the condition is true, the code inside the loop will run. After
the loop body has run, the condition is evaluated again, and if it’s
still true, the body runs again. This process continues until the
condition becomes false. If the condition starts off as false, the loop
body will never execute.
Example:
Syntax :

while (condition) // Print numbers from 1 to 5


{ #include <stdio.h>
// code to be executed as long as condition is int main() {
true; int i = 1;
} while (i <= 5) {
printf("%d\n", i);
++i;
}

return 0;
}
do-while loop
The do-while loop checks the condition after executing the loop
body, which ensures that the loop body is executed at least once,
even if the condition is initially false.

Syntax :

do
{
// code to be executed;
} while (condition);
Example:

// Program to add numbers until the user enters zero

#include <stdio.h>
int main() {
double number, sum = 0;

// the body of the loop is executed at least once


do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);

printf("Sum = %lf",sum);

return 0;
}

You might also like