Conditional operators are used to compare two values and return a boolean value (true or false). The most common conditional operators are:
- == (equal to)
- != (not equal to)
- > (greater than)
- < (less than)
- >= (greater than or equal to)
- <= (less than or equal to)
C++
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10;
if (a == b) {
cout << "a is equal to b" << endl;
}
if (a != b) {
cout << "a is not equal to b" << endl;
}
if (a > b) {
cout << "a is greater than b" << endl;
}
if (a < b) {
cout << "a is less than b" << endl;
}
if (a >= b) {
cout << "a is greater than or equal to b" << endl;
}
if (a <= b) {
cout << "a is less than or equal to b" << endl;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
int a = 5, b = 10;
if (a == b) {
printf ( "a is equal to b\n" );
}
if (a != b) {
printf ( "a is not equal to b\n" );
}
if (a > b) {
printf ( "a is greater than b\n" );
}
if (a < b) {
printf ( "a is less than b\n" );
}
if (a >= b) {
printf ( "a is greater than or equal to b\n" );
}
if (a <= b) {
printf ( "a is less than or equal to b\n" );
}
return 0;
}
|
Java
public class Main {
public static void main(String[] args)
{
int a = 5 , b = 10 ;
if (a == b) {
System.out.println( "a is equal to b" );
}
if (a != b) {
System.out.println( "a is not equal to b" );
}
if (a > b) {
System.out.println( "a is greater than b" );
}
if (a < b) {
System.out.println( "a is less than b" );
}
if (a >= b) {
System.out.println(
"a is greater than or equal to b" );
}
if (a <= b) {
System.out.println(
"a is less than or equal to b" );
}
}
}
|
Python
a = 5
b = 10
if a = = b:
print ( "a is equal to b" )
if a ! = b:
print ( "a is not equal to b" )
if a > b:
print ( "a is greater than b" )
if a < b:
print ( "a is less than b" )
if a > = b:
print ( "a is greater than or equal to b" )
if a < = b:
print ( "a is less than or equal to b" )
|
C#
using System;
class Program {
static void Main()
{
int a = 5, b = 10;
if (a == b) {
Console.WriteLine( "a is equal to b" );
}
if (a != b) {
Console.WriteLine( "a is not equal to b" );
}
if (a > b) {
Console.WriteLine( "a is greater than b" );
}
if (a < b) {
Console.WriteLine( "a is less than b" );
}
if (a >= b) {
Console.WriteLine(
"a is greater than or equal to b" );
}
if (a <= b) {
Console.WriteLine(
"a is less than or equal to b" );
}
}
}
|
Javascript
let a = 5, b = 10;
if (a === b) {
console.log( "a is equal to b" );
}
if (a !== b) {
console.log( "a is not equal to b" );
}
if (a > b) {
console.log( "a is greater than b" );
}
if (a < b) {
console.log( "a is less than b" );
}
if (a >= b) {
console.log( "a is greater than or equal to b" );
}
if (a <= b) {
console.log( "a is less 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
If statement in Programming
An if statement is a fundamental control structure in programming languages that allows you to execute specific code blocks based on whether a condition is true or false. It is used to make decisions and control the flow of execution in your program.
If statement in Programming
Table of Content
- What is an If Statement?
- Example of If Statement in Programming
- Conditional Operators in If Statements
- Common Mistakes to Avoid
- Best Practices for Using If Statements
Contact Us