MT Module
MT Module
● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
● Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x + y);
return 0;
}
Subtraction Example
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x - y);
return 0;
}
Multiplication Example
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x * y);
return 0;
}
Division Example
#include <stdio.h>
int main() {
int x = 12;
int y = 3;
printf("%d", x / y);
return 0;
}
School of Information and Technology
Modulus Example
#include <stdio.h>
int main() {
int x = 5;
int y = 2;
printf("%d", x % y);
return 0;
}
Increment Example
#include <stdio.h>
int main() {
int x = 5;
printf("%d", ++x);
return 0;
}
Decrement Example
#include <stdio.h>
int main() {
int x = 5;
printf("%d", --x);
return 0;
}
School of Information and Technology
Assignment Operators
Assignment operators are used to assign values to variables.
int x = 10;
Ex.
#include <stdio.h>
int main() {
int x = 10;
printf("%d", x);
return 0;
#include <stdio.h>
int main() {
int x = 10;
x += 5;
printf("%d", x);
return 0;
}
School of Information and Technology
= 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
Example : =
#include <stdio.h>
int main() {
int x = 5;
printf("%d", x);
return 0;
}
Output :5
Example : +=
#include <stdio.h>
int main() {
int x = 5;
x += 3;
printf("%d", x);
return 0;
}
Output:8
Example : - =
#include <stdio.h>
int main() {
int x = 5;
x -= 3;
printf("%d", x);
return 0;
}
Output:2
Example: *=
#include <stdio.h>
int main() {
int x = 5;
x *= 3;
printf("%d", x);
return 0;
School of Information and Technology
}
Output:15
Example: /=
#include <stdio.h>
int main() {
float x = 5;
x /= 3;
printf("%f", x);
return 0;
}
Output:1.66667
Example: %=
#include <stdio.h>
int main() {
int x = 5;
x %= 3;
printf("%d", x);
return 0;
}
Output:2
Example : &=
#include <stdio.h>
int main() {
int x = 5;
x &= 3;
printf("%d", x);
return 0;
}
Output:1
Example: |=
#include <stdio.h>
int main() {
int x = 5;
x |= 3;
printf("%d", x);
return 0;
School of Information and Technology
}
Output: 7
Example: ^=
#include <stdio.h>
int main() {
int x = 5;
x ^= 3;
printf("%d", x);
return 0;
}
Output:6
Example: >>=
#include <stdio.h>
int main() {
int x = 5;
x >>= 3;
printf("%d", x);
return 0;
}
0utput: 0
Example: <<=
#include <stdio.h>
int main() {
int x = 5;
x <<= 3;
printf("%d", x);
return 0;
}
Output: 40
School of Information and Technology
Comparison 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 comparison is either 1 or 0, which means true (1) or false
(0). These values are known as Boolean values, and you will learn more about
them in the Booleans and If..Else chapter.
In the following example, we use the greater than operator (>) to find out if 5 is
greater than 3:
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
return 0;
Output: 1
School of Information and Technology
== Equal to x == y
!= Not equal x != y
Example: ==
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
return 0;
Output: 0
School of Information and Technology
Example: !=
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
return 0;
Output: 1
Example: >
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
return 0;
Output:1
School of Information and Technology
Example: <
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
return 0;
Output:0
Example:>=
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
return 0;
Output:1
School of Information and Technology
Example:<=
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
return 0;
Output: 1
School of Information and Technology
Logical Operators
You can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables or values:
Example : &&
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
return 0;
Output: 1
School of Information and Technology
Example : ||
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
// Returns 1 (true) because one of the conditions are true (5 is greater than 3, but 5 is not
less than 4)
return 0;
Output:1
Example : !
#include <stdio.h>
int main() {
int x = 5;
int y = 3;
return 0;
}
School of Information and Technology
Output:0
Sizeof Operator
The memory size (in bytes) of a data type or a variable can be found with the
sizeof operator:
#include <stdio.h>
int main() {
int myInt;
float myFloat;
double myDouble;
char myChar;
printf("%lu\n", sizeof(myInt));
printf("%lu\n", sizeof(myFloat));
printf("%lu\n", sizeof(myDouble));
printf("%lu\n", sizeof(myChar));
return 0;
Output:
Note that we use the %lu format specifier to print the result, instead of %d. It is
because the compiler expects the sizeof operator to return a long unsigned int
School of Information and Technology
(%lu), instead of int (%d). On some computers it might work with %d, but it is safer to
use %lu.
Booleans
Very often, in programming, you will need a data type that can only have one of
two values, like:
● YES / NO
● ON / OFF
● TRUE / FALSE
Boolean Variables
In C, the bool type is not a built-in data type, like int or char.
It was introduced in C99, and you must import the following header file to use
it:
#include <stdbool.h>
A boolean variable is declared with the bool keyword and can only take the
values true or false:
Before trying to print the boolean variables, you should know that boolean
values are returned as integers:
Therefore, you must use the %d format specifier to print a boolean value:
#include <stdio.h>
int main() {
return 0;
Output:
For example, you can use a comparison operator, such as the greater than (>)
operator, to compare two values:
School of Information and Technology
#include <stdio.h>
int main() {
return 0;
Output:
You are not limited to only comparing numbers. You can also compare
boolean variables, or even special structures, like arrays (which you
will learn more about in a later chapter):
#include <stdio.h>
int main() {
return 0;
Output:
1
School of Information and Technology
Remember to include the <stdbool.h> header file when working with
bool variables.
In the example below, we use the >= comparison operator to find out if the age
(25) is greater than OR equal to the voting age limit, which is set to 18:
#include <stdio.h>
int main() {
return 0;
Output:
1
School of Information and Technology
Ex. 2
Output "Old enough to vote!" if myAge is greater than or equal to 18. Otherwise
output "Not old enough to vote.":
#include <stdio.h>
int main() {
} else {
return 0;
Output:
C If ... Else
Conditions and If Statements
You have already learned that C supports the usual logical conditions from
mathematics:
● Equal to a == b
You can use these conditions to perform different actions for different decisions.
true
is false
false
The if Statement
true.
Syntax
if (condition) {
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
In the example below, we test two values to find out if 20 is greater than 18. If the
#include <stdio.h>
int main() {
return 0;
#include <stdio.h>
int main() {
int x = 20;
int y = 18;
if (x > y) {
return 0;
In the example above we use two variables, x and y, to test whether x is greater than y
(using the > operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we
print to the screen that "x is greater than y".
School of Information and Technology
Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example
#include <stdio.h>
int main() {
In the example above, time (20) is greater than 18, so the condition is false.
Because of this, we move on to the else condition and print to the screen
"Good evening". If the time was less than 18, the program would print "Good
day".
School of Information and Technology
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}
Example
#include <stdio.h>
int main() {
int time = 22;
if (time < 10) {
printf("Good morning.");
} else if (time < 20) {
printf("Good day.");
} else {
printf("Good evening.");
}
return 0;
}
In the example above, time (22) is greater than 10, so the first condition is
false. The next condition, in the else if statement, is also false, so we
move on to the else condition since condition1 and condition2 are both false -
and print to the screen "Good evening".
However, if the time was 14, our program would print "Good day."
School of Information and Technology
Another Example:
This example shows how you can use if..else to find out if a number is
positive or negative:
#include <stdio.h>
int main() {
if (myNum > 0) {
} else {
return 0;
Switch Statement
Instead of writing many if..else statements, you can use the switch
statement.
Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
The example below uses the weekday number to calculate the weekday name:
#include <stdio.h>
int main() {
int day = 4;
switch (day) {
case 1:
printf("Monday");
break;
School of Information and Technology
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
return 0;
Output: Thursday
School of Information and Technology
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no
need for more testing.
A break can save a lot of execution time because it "ignores" the execution of all
the rest of the code in the switch block.
Note: The default keyword must be used as the last statement in the switch,
and it does not need a break.
#include <stdio.h>
int main()
{
int day = 4;
switch (day) {
case 6:
printf("Today is Saturday");
break;
case 7:
printf("Today is Sunday");
break;
default:
printf("Looking forward to the Weekend");
}
return 0;
}
Loops
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more
readable.
While Loop
The while loop loops through a block of code as long as a specified condition is true:
Syntax
while (condition) {
In the example below, the code in the loop will run, over and over again, as
long as a variable (i) is less than 5:
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
return 0;
}
School of Information and Technology
Output:
Note: Do not forget to increase the variable used in the condition (i++),
otherwise the loop will never end!
Syntax
do {
while (condition);
School of Information and Technology
The example below uses a do/while loop. The loop will always be executed at
least once, even if the condition is false, because the code block is executed
before the condition is tested:
#include <stdio.h>
int main() {
int i = 0;
do {
printf("%d\n", i);
i++;
return 0;
Output:
4
School of Information and Technology
For Loop
When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop:
Syntax
Statement 3 is executed (every time) after the code block has been
executed.
School of Information and Technology
The example below will print the numbers 0 to 4:
#include <stdio.h>
int main() {
int i;
printf("%d\n", i);
return 0;
Output:
4
School of Information and Technology
This example will only print even values between 0 and 10:
#include <stdio.h>
int main() {
int i;
printf("%d\n", i);
return 0;
Output:
10
School of Information and Technology
Nested Loops
It is also possible to place a loop inside another loop. This is called a nested
loop.
The "inner loop" will be executed one time for each iteration of the "outer loop":
#include <stdio.h>
int main() {
int i, j;
// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times
// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}
return 0;
}
Output:
Outer: 1
Inner: 1
Inner: 2
Inner: 3
Outer: 2
Inner: 1
Inner: 2
Inner: 3
School of Information and Technology
#include <stdio.h>
int main() {
int i;
if (i == 4) {
break;
printf("%d\n", i);
return 0;
Output:
3
School of Information and Technology
Continue
The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.
#include <stdio.h>
int main() {
int i;
if (i == 4) {
continue;
printf("%d\n", i);
return 0;
Output:
0
1
2
3
4
5
6
7
8
9
School of Information and Technology
Break and Continue in While Loop
You can also use break and continue in while loops:
Break Ex.
#include <stdio.h>
int main() {
int i = 0;
return 0;
}
Output:
0
1
2
3
School of Information and Technology
Continue Ex.
#include <stdio.h>
int main() {
int i = 0;
if (i == 4) {
break;
printf("%d\n", i);
i++;
return 0;
Output:
References:
https://www.w3schools.com/c/c_operators.php
https://www.w3schools.com/c/c_booleans.php
https://www.w3schools.com/c/c_conditions.php
https://www.w3schools.com/c/c_switch.php
https://www.w3schools.com/c/c_while_loop.php
https://www.w3schools.com/c/c_for_loop.php
https://www.w3schools.com/c/c_break_continue.php