Relational and Logical operators
Unary Operator
A unary operator in C is an operator that operates on a single operand, either transforming its
value or determining a characteristic of its state. From increasing or decreasing values to
retrieving the memory address of variables, unary operators facilitate a broad range of
functionalities that are indispensable in programming tasks.
Binary operators
Binary operator follow a specific syntax. They are placed between the LHS and the RHS,
defining the operation that needs to be performed on the operands. The binary operator acts
on the two operands to produce a result.
Ternary operator
The conditional operator in C is kind of similar to the if-else statement as it follows the
same algorithm as of if-else statement but the conditional operator takes less space and helps
to write the if-else statements in the shortest way possible. It is also known as the ternary
operator in C as it operates on three operands.
Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
Operator Meaning of Operator Example
5 == 3 is
== Equal to
evaluated to 0
5 > 3 is evaluated
> Greater than
to 1
5 < 3 is evaluated
< Less than
to 0
5 != 3 is
!= Not equal to
evaluated to 1
5 >= 3 is
>= Greater than or equal to
evaluated to 1
Operator Meaning of Operator Example
5 <= 3 is
<= Less than or equal to
evaluated to 0
Example 4: Relational Operators
// Working of relational operators
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}
Run Code
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in C
programming.
Operato
Meaning Example
r
If c = 5 and d = 2 then,
Logical AND. True only if all
&& expression ((c==5) && (d>5))
operands are true
equals to 0.
If c = 5 and d = 2 then,
Logical OR. True only if either one
|| expression ((c==5) || (d>5))
operand is true
equals to 1.
Logical NOT. True only if the If c = 5 then, expression !(c==5)
!
operand is 0 equals to 0.
Example 5: Logical Operators
// Working of logical operators
#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);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
Run Code
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Explanation of logical operator program
(a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1
(true).
(a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
(a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
(a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
!(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1
(true).
!(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).
Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Example 3: Assignment Operators
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Run Code
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
Conditional operator
The conditional operator in C programming offers a concise and efficient alternative to if-else
statements in situations where there is only one statement corresponding to each condition. It
evaluates an expression and executes the appropriate statement based on the result, providing
a streamlined solution.
The conditional operator in C has the syntax:
(condition) ? expression1 : expression2
The conditions are treated as logical, where non-zero values are considered true, and 0 is
considered false. expression1 and expression2 can be statements, expressions, variables, or
constants. Based on the result, the operator evaluates the expression and executes either
expression1 or expression2
Working as Conditional Operator in C
In C, the conditional operator evaluates the condition and implicitly converts the result to a
boolean value. If the condition is true, expression1 is executed; otherwise, expression2 is
executed. The result of the operator is the result of either expression1 or expression2. Only
one of the two expressions is evaluated, while the other is ignored.
Count-controlled loops
There are two ways in which programs can iterate or ‘loop’:
count-controlled loops
condition-controlled loops
Each type of loop works in a slightly different way and produces different results.
Count-controlled loops
Sometimes it is necessary for steps to iterate a specific number of times.
Consider this simple algorithm for adding up five inputted numbers:
1. set the total to 0
2. repeat this section five times
o input a number
o add the number to the total
3. go back to step 2
4. say what the total is
This algorithm would allow five numbers to be inputted and would work out the total.
Because it is known in advance how many times the algorithm needs to loop, a count-
controlled loop is used.
while loop in C
Last Updated : 07 May, 2023
The while Loop is an entry-controlled loop in C programming language. This loop can be
used to iterate a part of code while the given condition remains true.
Syntax
The while loop syntax is as follows:
while (test expression)
{
// body consisting of multiple statements
}
while Loop Structure
The while loop works by following a very structured top-down approach that can be divided
into the following parts:
1. Initialization: In this step, we initialize the loop variable to some initial
value. Initialization is not part of while loop syntax but it is essential when we are
using some variable in the test expression
2. Conditional Statement: This is one of the most crucial steps as it decides whether
the block in the while loop code will execute. The while loop body will be executed if
and only the test condition defined in the conditional statement is true.
3. Body: It is the actual set of statements that will be executed till the specified
condition is true. It is generally enclosed inside { } braces.
4. Updation: It is an expression that updates the value of the loop variable in each
iteration. It is also not part of the syntax but we have to define it explicitly in the body
of the loop.
Working of while Loop
We can understand the working of the while loop by looking at the above flowchart:
1. STEP 1: When the program first comes to the loop, the test condition will be
evaluated.
2. STEP 2A: If the test condition is false, the body of the loop will be skipped program
will continue.
3. STEP 2B: If the expression evaluates to true, the body of the loop will be executed.
4. STEP 3: After executing the body, the program control will go to STEP 1. This
process will continue till the test expression is true.
n C programming, loops are responsible for performing repetitive tasks using a short code
block that executes until the condition holds true. In this article, we will learn about for loop
in C.
for Loop in C
The for loop in C Language provides a functionality/feature to repeat a set of statements a
defined number of times. The for loop is in itself a form of an entry-controlled loop.
Unlike the while loop and do…while loop, the for loop contains the initialization, condition,
and updating statements as part of its syntax. It is mainly used to traverse arrays, vectors, and
other data structures.
Syntax of for Loop
for(initialization; check/test expression; updation)
{
// body consisting of multiple statements
}
Structure of for Loop
The for loop follows a very structured approach where it begins with initializing a condition
then checks the condition and in the end executes conditional statements followed by an
updation of values.
1. Initialization: This step initializes a loop control variable with an initial value that
helps to progress the loop or helps in checking the condition. It acts as the index value
when iterating an array or string.
2. Check/Test Condition: This step of the for loop defines the condition that
determines whether the loop should continue executing or not. The condition is
checked before each iteration and if it is true then the iteration of the loop continues
otherwise the loop is terminated.
3. Body: It is the set of statements i.e. variables, functions, etc that is executed
repeatedly till the condition is true. It is enclosed within curly braces { }.
4. Updation: This specifies how the loop control variable should be updated after each
iteration of the loop. Generally, it is the incrementation (variable++) or
decrementation (variable–) of the loop control variable.
How for Loop Works?
The working of for loop is mentioned below:
Step 1: Initialization is the basic step of for loop this step occurs only once during the
start of the loop. During Initialization, variables are declared, or already existing
variables are assigned some value.
Step 2: During the Second Step condition statements are checked and only if the
condition is the satisfied loop we can further process otherwise loop is broken.
Step 3: All the statements inside the loop are executed.
Step 4: Updating the values of variables has been done as defined in the loop.
Continue to Step 2 till the loop breaks.
Nested Loop :
A nested loop means a loop statement inside another loop statement. That is why
nested loops are also called “loop inside loops“. We can define any number of loops
inside another loop.
1. Nested for Loop
Nested for loop refers to any type of loop that is defined inside a ‘for’ loop. Below
is the equivalent flow diagram for nested ‘for’ loops:
Syntax:
for ( initialization; condition; increment ) {
for ( initialization; condition; increment ) {
// statement of inside loop
}
// statement of outer loop
}
o First, the 'i' variable is initialized to 1 and then program control passes to the i<=n.
o The program control checks whether the condition 'i<=n' is true or not.
o If the condition is true, then the program control passes to the inner loop.
o The inner loop will get executed until the condition is true.
o After the execution of the inner loop, the control moves back to the update of the
outer loop, i.e., i++.
o After incrementing the value of the loop counter, the condition is checked again, i.e.,
i<=n.
o If the condition is true, then the inner loop will be executed again.
o This process will continue until the condition of the outer loop is true.