Though this chapter is not required separately because reserved keywords are part of basic programming syntax but I kept it separate to explain it right after data types and variables to make it easy to understand.Like int, long, and float, there are many other keywords supported by C programming language which we will use for different purpose. Different programming languages provide different set of reserved keywords, but there is one important & common rule in all the programming languages that we cannot use a reserved keyword to name our variables, which means we cannot name our variable like int or float rather these keywords can only be used to specify a variable data type.
For example, if you will try to use any reserved keyword for the purpose of variable name, then you will get syntax error, as follows:
#include <stdio.h> main() { int float; float = 10; printf( "Value of float = %d\n", float); }When you compile above program, it produces the following error:
main.c: In function 'main': main.c:5:8: error: two or more data types in declaration specifiers int float; ......But now let's give proper name to our integer variable, then above program should compile and execute successfully:
#include <stdio.h> main() { int count; count = 10; printf( "Value of count = %d\n", count); }
C programming reserved keywords
Here is a table having almost all the keywords supported by C Programming language:auto | else | long | switch |
break | enum | register | typedef |
case | extern | return | union |
char | float | short | unsigned |
const | for | signed | void |
continue | goto | sizeof | volatile |
default | if | static | while |
do | int | struct | _Packed |
double |
Java programming reserved keywords
Here is a table having almost all the keywords supported by Java Programming language:abstract | assert | boolean | break |
byte | case | catch | char |
class | const | continue | default |
do | double | else | enum |
extends | final | finally | float |
for | goto | if | implements |
import | instanceof | int | interface |
long | native | new | package |
private | protected | public | return |
short | static | strictfp | super |
switch | synchronized | this | throw |
throws | transient | try | void |
volatile | while |
Python programming reserved keywords
Here is a table having almost all the keywords supported by Java Programming language:and | exec | not |
assert | finally | or |
break | for | pass |
class | from | |
continue | global | raise |
def | if | return |
del | import | try |
elif | in | while |
else | is | with |
except | lambda | yield |
An operator in a programming language is a symbol that tells the compiler or interpreter to perform specific mathematical, relational or logical operation and produce final result. This chapter will explain you what are the operators and will take you through important arithmetic and relational operators available in C, Java and Python programming languages.
Arithmetic Operators
Computer programs are widely used for mathematical calculations. We can write a computer program which can do simple calculation like adding two numbers (2 + 3) and we can also write a program, which can solve a complex equation like P(x) = x4 + 7x3 - 5x + 9. If you have been even a poor student, you must be aware that in first expression 2 and 3 are operands and + is an operator. Similar concept exists in Computer Programming.Here we took following two mathematics examples:
2 + 3 P(x) = x4 + 7x3 - 5x + 9.These two statements are called arithmetic expressions in a programming language and plus, minus used in these expressions are called arithmetic operators and values used in these expressions like 2, 3 and x, etc., are called operands. In their simplest form such expressions produce numerical results.
Similar way, a programming language provides various arithmetic operators. Following table lists down few of the important arithmetic operators available in C programming language. Assume variable A holds 10 and variable B holds 20, then:
Operator | Description | Example |
---|---|---|
+ | Adds two operands | A + B will give 30 |
- | Subtracts second operand from the first | A - B will give -10 |
* | Multiplies both operands | A * B will give 200 |
/ | Divides numerator by de-numerator | B / A will give 2 |
% | This gives remainder of an integer division | B % A will give 0 |
#include <stdio.h> main() { int a, b, c; a = 10; b = 20; c = a + b; printf( "Value of c = %d\n", c); c = a - b; printf( "Value of c = %d\n", c); c = a * b; printf( "Value of c = %d\n", c); c = b / a; printf( "Value of c = %d\n", c); c = b % a; printf( "Value of c = %d\n", c); }When above program is executed, it produces the following result:
Value of c = 30 Value of c = -10 Value of c = 200 Value of c = 2 Value of c = 0
Relational Operators
Consider a situation where we create two variables and assign them some values as follows:A = 20 B = 10Here, it is obvious that variable A is greater than B in values. But how do we write this in a computer programming language? So, we need help of some symbols to write this kind of expressions which are called relational expressions. If we make use of C programming language, then it will be written as follows:
(A > B)Here, we used a symbol > and it is called relational operator and in their simplest form they produce boolean results which means result will be either true or false. Similar way, a programming language provides various relational operators. Following table lists down few of the important relational operators available in C programming language.Assume variable A holds 10 and variable B holds 20, then:
Operator | Description | Example |
---|---|---|
== | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
!= | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true. |
#include <stdio.h> main() { int a, b; a = 10; b = 20; /* Here we check whether a is equal to 10 or not */ if( a == 10 ) { /* if a is equal to 10 then this body will be executed */ printf( "a is equal to 10\n"); } /* Here we check whether b is equal to 10 or not */ if( b == 10 ) { /* if b is equal to 10 then this body will be executed */ printf( "b is equal to 10\n"); } /* Here we check if a is less b than or not */ if( a < b ) { /* if a is less than b then this body will be executed */ printf( "a is less than b\n"); } /* Here we check whether a and b are not equal */ if( a != b ) { /* if a is not equal to b then this body will be executed */ printf( "a is not equal to b\n"); } }When above program is executed, it produces the following result:
a is equal to 10 a is less than b a is not equal to b
Logical Operators
Logical operators are very important in any programming language and they help us in taking decision based on certain conditions. Suppose we want to combine the result of two conditions, then logical AND and OR logical operators help us in giving final result.Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then:
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non-zero, then condition becomes true. | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is true. |
#include <stdio.h> main() { int a = 1; int b = 0; if ( a && b ) { printf("This will never print because condition is false\n" ); } if ( a || b ) { printf("This will be printed print because condition is true\n" ); } if ( !(a && b) ) { printf("This will be printed print because condition is true\n" ); } }When you compile and execute the above program, it produces the following result:
This will be printed print because condition is true This will be printed print because condition is true
Operators in Java
Following is the equivalent program written in Java programming language. C programming and Java programming languages provide almost identical set of operators and conditional statements. This program will create two variables a and b and very similar to C programming, then we assign 10 and 20 in these variables and finally we will use different arithmetic and relation operators:You can try to execute the following program to see the output, which must be identical to the result generated by the above example.
public class DemoJava { public static void main(String []args) { int a, b, c; a = 10; b = 20; c = a + b; System.out.println("Value of c = " + c ); c = a - b; System.out.println("Value of c = " + c ); c = a * b; System.out.println("Value of c = " + c ); c = b / a; System.out.println("Value of c = " + c ); c = b % a; System.out.println("Value of c = " + c ); if( a == 10 ) { System.out.println("a is equal to 10" ); } } }
Operators in Python
Following is the equivalent program written in Python. This program will create two variables a and b and same time assign 10 and 20 in those variables. Fortunately, again C programming and Python programming languages provide almost identical set of operators. This program will create two variables a and b and very similar to C programming, then we assign 10 and 20 in these variables and finally we will use different arithmetic and relation operators.You can try to execute following program to see the output, which must be identical to the result generated by the above example.
a = 10 b = 20 c = a + b print "Value of c = ", c c = a - b print "Value of c = ", c c = a * b print "Value of c = ", c c = a / b print "Value of c = ", c c = a % b print "Value of c = ", c if( a == 10 ): print "a is equal to 10"