Conditional statements in programming are used to execute certain blocks of code based on specified conditions. They are fundamental to decision-making in programs. Here are some common types of conditional statements:
1. If Statement in Programming:
The if
statement is used to execute a block of code if a specified condition is true.
C++
#include <iostream>
using namespace std;
int main()
{
int a = 5;
if (a == 5) {
cout << "a is equal to 5" ;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
int a = 5;
if (a == 5) {
printf ( "a is equal to 5" );
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 5 ;
if (a == 5 ) {
System.out.println( "a is equal to 5" );
}
}
}
|
C#
using System;
public class GFG {
static public void Main()
{
int a = 5;
if (a == 5) {
Console.WriteLine( "a is equal to 5" );
}
}
}
|
Javascript
let a = 5;
if (a === 5) {
console.log( "a is equal to 5" );
}
|
Python3
a = 5
if a = = 5 :
print ( "a is equal to 5" )
|
2. if-else Statement in Programming:
The if-else
statement is used to execute one block of code if a specified condition is true, and another block of code if the condition is false.
C++
#include <iostream>
using namespace std;
int main()
{
int a = 10;
if (a == 5) {
cout << "a is equal to 5" ;
}
else {
cout << "a is not equal to 5" ;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
int a = 10;
if (a == 5) {
printf ( "a is equal to 5" );
}
else {
printf ( "a is not equal to 5" );
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 10 ;
if (a == 5 ) {
System.out.println( "a is equal to 5" );
}
else {
System.out.println( "a is not equal to 5" );
}
}
}
|
C#
using System;
public class GFG {
static public void Main()
{
int a = 10;
if (a == 5) {
Console.WriteLine( "a is equal to 5" );
}
else {
Console.WriteLine( "a is not equal to 5" );
}
}
}
|
Javascript
let a = 10;
if (a === 5) {
console.log( "a is equal to 5" );
} else {
console.log( "a is not equal to 5" );
}
|
Python3
a = 10
if a = = 5 :
print ( "a is equal to 5" )
else :
print ( "a is not equal to 5" )
|
Output
a is not equal to 5
3. if-else-if Statement in Programming:
The if-else-if
statement is used to execute one block of code if a specified condition is true, another block of code if another condition is true, and a default block of code if none of the conditions are true.
C++
#include <iostream>
using namespace std;
int main()
{
int a = 15;
if (a == 5) {
cout << "a is equal to 5" ;
}
else if (a == 10) {
cout << "a is equal to 10" ;
}
else {
cout << "a is not equal to 5 or 10" ;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
int a = 15;
if (a == 5) {
printf ( "a is equal to 5" );
}
else if (a == 10) {
printf ( "a is equal to 10" );
}
else {
printf ( "a is not equal to 5 or 10" );
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 15 ;
if (a == 5 ) {
System.out.println( "a is equal to 5" );
}
else if (a == 10 ) {
System.out.println( "a is equal to 10" );
}
else {
System.out.println( "a is not equal to 5 or 10" );
}
}
}
|
C#
using System;
public class GFG {
static public void Main()
{
int a = 15;
if (a == 5) {
Console.WriteLine( "a is equal to 5" );
}
else if (a == 10) {
Console.WriteLine( "a is equal to 10" );
}
else {
Console.WriteLine( "a is not equal to 5 or 10" );
}
}
}
|
Javascript
let a = 15;
if (a === 5) {
console.log( "a is equal to 5" );
} else if (a === 10) {
console.log( "a is equal to 10" );
} else {
console.log( "a is not equal to 5 or 10" );
}
|
Python3
a = 15
if a = = 5 :
print ( "a is equal to 5" )
elif a = = 10 :
print ( "a is equal to 10" )
else :
print ( "a is not equal to 5 or 10" )
|
Output
a is not equal to 5 or 10
4. Ternary Operator or Conditional Operator in Programming:
In some programming languages, a ternary operator is used to assign a value to a variable based on a condition.
C++
#include <iostream>
using namespace std;
int main()
{
int a = 10;
cout << (a == 5 ? "a is equal to 5"
: "a is not equal to 5" );
return 0;
}
|
C
#include <stdio.h>
int main()
{
int a = 10;
printf ( "%s" , (a == 5 ? "a is equal to 5"
: "a is not equal to 5" ));
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 10 ;
System.out.println(a == 5 ? "a is equal to 5"
: "a is not equal to 5" );
}
}
|
C#
using System;
public class GFG {
static public void Main()
{
int a = 10;
Console.WriteLine(a == 5 ? "a is equal to 5"
: "a is not equal to 5" );
}
}
|
Javascript
let a = 10;
console.log(a === 5 ? "a is equal to 5" : "a is not equal to 5" );
|
Python3
a = 10
print ( "a is equal to 5" if a = = 5 else "a is not equal to 5" )
|
Output
a is not equal to 5
5. Switch Statement in Programming:
In languages like C, C++, and Java, a switch
statement is used to execute one block of code from multiple options based on the value of an expression.
C++
#include <iostream>
using namespace std;
int main()
{
int a = 15;
switch (a) {
case 5:
cout << "a is equal to 5" ;
break ;
case 10:
cout << "a is equal to 10" ;
break ;
default :
cout << "a is not equal to 5 or 10" ;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
int a = 15;
switch (a) {
case 5:
printf ( "a is equal to 5" );
break ;
case 10:
printf ( "a is equal to 10" );
break ;
default :
printf ( "a is not equal to 5 or 10" );
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int a = 15 ;
switch (a) {
case 5 :
System.out.println( "a is equal to 5" );
break ;
case 10 :
System.out.println( "a is equal to 10" );
break ;
default :
System.out.println( "a is not equal to 5 or 10" );
}
}
}
|
C#
using System;
public class GFG {
static public void Main()
{
int a = 15;
switch (a) {
case 5:
Console.WriteLine( "a is equal to 5" );
break ;
case 10:
Console.WriteLine( "a is equal to 10" );
break ;
default :
Console.WriteLine( "a is not equal to 5 or 10" );
break ;
}
}
}
|
Javascript
let a = 15;
switch (a) {
case 5:
console.log( "a is equal to 5" );
break ;
case 10:
console.log( "a is equal to 10" );
break ;
default :
console.log( "a is not equal to 5 or 10" );
}
|
Output
a is not equal to 5 or 10
Each programming language may have its own syntax and specific variations of these conditional statements.
Control flow statements in Programming
Control flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow Statements.
Table of Content
- What are Control Flow Statements in Programming?
- Types of Control Flow statements in Programming
- Conditional Statements in Programming
- Looping Statements in Programming
- Jump Statements in Programming