If/Else

If-else statements in computer programming are fundamental constructs for implementing conditional logic, enabling programmers to execute different blocks of code based on specified conditions. They provide flexibility and control flow within programs, allowing for the execution of alternative paths when certain conditions are met. If-else statements are versatile tools, capable of handling complex decision-making scenarios with multiple branches, nested conditions, and logical operators. They enhance code readability and maintainability by organizing code logic in a clear and structured manner, making it easier for programmers to understand and modify. Additionally, if-else statements play a critical role in error handling, input validation, and implementing various algorithms and business logic within software applications. Overall, if-else statements are indispensable for creating dynamic, responsive, and robust computer programs across a wide range of domains.

Select Languages

Examples

C

int carCount = 6;
  
if (carCount > 3) {
  printf("More than three cars");
} else if (carCount == 3) {
  printf("Equal to three cars");
} else {
  printf("Less than three cars");
}

C#

int carCount = 6;
    
if (carCount > 3) {
  Console.WriteLine("More than three cars");
} else if (carCount == 3) {
  Console.WriteLine("Equal to three cars");
} else {
  Console.WriteLine("Less than three cars");
}

C++

int carCount = 6;

if (carCount > 3) {
  cout << "More than three cars";
} else if (carCount == 3) {
  cout << "Equal to three cars";
} else {
  cout << "Less than three cars";
}

Go

import "fmt"

var carCount = 6;

if carCount > 3 {
  fmt.Println("More than three cars")
} else if (carCount == 3) {
  fmt.Println("Equal to three cars")
} else {
  fmt.Println("Less than three cars")
}

Java

int carCount = 6;

if (carCount > 3) {
  System.out.println("More than three cars");
} else if (carCount == 3) { 
  System.out.println("Equal to three cars");
} else {
  System.out.println("Less than three cars");
}

JavaScript

const carCount = 6;

if (carCount > 3) {
  console.log('More than three cars');
} else if (carCount === 3) {
  console.log('Equal to three cars');
} else {
  console.log('Less than three cars');
}

Kotlin

val carCount = 6;

if (carCount > 3) {
  println("More than three cars");
} else if (carCount == 3) {
  println("Equal to three cars");
} else {
  println("Less than three cars");
}

MatLab

carCount = 6;

if carCount > 3
  display('More than three cars');
elseif carCount == 3
  display('Equal to three cars');
else
  display('Less than three cars');
end

PHP

$carCount = 6;

if ($carCount > 3) {
  echo "More than three cars";
} elseif ($carCount == 3) {
  echo "Equal to three cars";
} else {
  echo "Less than three cars";
}

Python

carCount = 6

if carCount > 3:
  print("More than three cars")
elif:
  print("Equal to three cars")
else:
  print("Less than or equal to three cars")

R

carCount <- 6;

if (carCount > 3) {
  print("More than three cars");
} else if(carCount == 3) {
  print("Equal to three cars");
} else {
  print("Less than three cars");
}

Ruby

carCount = 6;
if carCount > 3
  puts "More than three cars"
elsif carCount == 3
  puts "Equal to three cars"
else
  puts "Less than three cars"

Rust

let carCount = 6;

if carCount > 3 {
  println!("More than three cars");
} else if(carCount == 3) {
  println!("Equal to three cars");
} else {
  println!("Less than three cars");
}

Scala

var carCount:Int = 6

if (carCount > 3) {
  printf("More than three cars");
} else if (carCount == 3) {
  printf("Equal to three cars");
} else {
  printf("Less than three cars");
}

Swift

let carCount = 6; 

if carCount > 3 {
  print("More than three cars");
} else if carCount == 3) {
  print("Equal to three cars");
} else {
  print("Less than three cars");
}

TypeScript

const carCount:number = 6;

if (carCount > 3) {
  console.log('More than three cars');
} else if (carCount === 3) {
  console.log('Equal to three cars');
} else {
  console.log('Less than three cars');
}

Copyright 2025. All Rights Reserved. IronCodeMan.