Operators
Operators are tools for performing operations on variables and values within code.
Arithmetic operators facilitate tasks, like addition subtraction, multiplication and division.
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
sum = a + b
difference = a - b
product = a * b
quotient = a / b
remainder = a % b
let a, b;
let sum = a + b;
let difference = a - b;
let product = a * b;
let quotient = a / b;
let remainder = a % b;
Relational Operators:
Relational operators compare values and return a boolean result.
#include <iostream>
int main() {
int a = 10;
int b = 20;
if (a == b) {
// equal
std::cout << "a is equal to b" << std::endl;
}
if (a != b) {
// not equal
std::cout << "a is not equal to b" << std::endl;
}
if (a < b) {
// less than
std::cout << "a is less than b" << std::endl;
}
if (a > b) {
// greater than
std::cout << "a is greater than b" << std::endl;
}
if (a <= b) {
// less than or equal
std::cout << "a is less than or equal to b" << std::endl;
}
if (a >= b) {
// greater than or equal
std::cout << "a is greater than or equal to b" << std::endl;
}
return 0;
}
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
if (a == b) {
// equal
printf("a is equal to b\n");
}
if (a != b) {
// not equal
printf("a is not equal to b\n");
}
if (a < b) {
// less than
printf("a is less than b\n");
}
if (a > b) {
// greater than
printf("a is greater than b\n");
}
if (a <= b) {
// less than or equal
printf("a is less than or equal to b\n");
}
if (a >= b) {
// greater than or equal
printf("a is greater than or equal to b\n");
}
return 0;
}
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
if (a == b) {
// equal
System.out.println("a is equal to b");
}
if (a != b) {
// not equal
System.out.println("a is not equal to b");
}
if (a < b) {
// less than
System.out.println("a is less than b");
}
if (a > b) {
// greater than
System.out.println("a is greater than b");
}
if (a <= b) {
// less than or equal
System.out.println("a is less than or equal to b");
}
if (a >= b) {
// greater than or equal
System.out.println("a is greater than or equal to b");
}
}
}
a = 10
b = 20
if a == b:
# equal
print('a is equal to b')
if a != b:
# not equal
print('a is not equal to b')
if a < b:
# less than
print('a is less than b')
if a > b:
# greater than
print('a is greater than b')
if a <= b:
# less than or equal
print('a is less than or equal to b')
if a >= b:
# greater than or equal
print('a is greater than or equal to b')
let a = 10;
let b = 20;
if (a == b) {
// equal
console.log('a is equal to b');
}
if (a != b) {
// not equal
console.log('a is not equal to b');
}
if (a < b) {
// less than
console.log('a is less than b');
}
if (a > b) {
// greater than
console.log('a is greater than b');
}
if (a <= b) {
// less than or equal
console.log('a is less than or equal to b');
}
if (a >= b) {
// greater than or equal
console.log('a is greater than or equal to b');
}
Output
a is not equal to b a is less than b a is less than or equal to b
Logical Operators:
Logical operators perform logical operations on boolean values.
if (condition1 && condition2) {
// logical AND
}
if (condition1 || condition2) {
// logical OR
}
if (!condition) {
// logical NOT
}
if (condition1 && condition2) {
// logical AND
}
if (condition1 || condition2) {
// logical OR
}
if (!condition) {
// logical NOT
}
if (condition1 && condition2) {
// logical AND
}
if (condition1 || condition2) {
// logical OR
}
if (!condition) {
// logical NOT
}
if condition1 and condition2:
# logical AND
if condition1 or condition2:
# logical OR
if not condition:
# logical NOT
if (condition1 && condition2) {
// logical AND: Executes if both condition1 and condition2 are true
}
if (condition1 || condition2) {
// logical OR: Executes if either condition1 or condition2 (or both) are true
}
if (!condition) {
// logical NOT: Executes if condition is false
}
Unary, Binary, and Ternary Operators:
Unary operators operate on a single operand, binary operators on two, and ternary operators on three.
int unary = -a; // unary minus
int binary = a + b; // binary plus
int ternary = (a > b) ? a : b; // ternary conditional
int unary = -a; // unary minus
int binary = a + b; // binary plus
int ternary = (a > b) ? a : b; // ternary conditional
int unary = -a; // unary minus
int binary = a + b; // binary plus
int ternary = (a > b) ? a : b; // ternary conditional
unary = -a # unary minus
binary = a + b # binary plus
ternary = a if a > b else b # ternary conditional
How to Start Coding
In this ever-evolving digital landscape, the ability to code is not just a valuable asset but a gateway to creative expression and innovation. This brief guide will outline practical steps and essential principles to guide aspiring learners on their path to mastering the art of coding.
Table of Content
- Introduction, to Basic Syntax. Hello World
- Understanding Data Types
- Variables and Constants
- Keywords
- Operators
- Decision Making statements
- Loops
- Numbers
- Characters
- Arrays
- Strings- Basic String Concepts
- Functions
Contact Us