What is a condition…
 
Notifications
Clear all

What is a conditional statement?

7 Posts
6 Users
4 Likes
530 Views
0
Topic starter

Explain what are conditional statements

demo.learner demo.learner 04/03/2023 5:17 pm

@natasha1305 Please post answers as well on other’s questions.

Rivek.t Rivek.t 05/03/2023 7:43 am
This post was modified 2 years ago by Rivek.t

@natasha1305 Conditional statements are programming constructs that allow for conditional execution of code based on a specified condition.

 

Here’s an example of a conditional statement in JavaScript that checks if a number is even or odd:

 

var number = 5;

if (number % 2 == 0) { // Check if the number is even
  console.log("The number is even.");
} else { // If the number is not even, it must be odd
  console.log("The number is odd.");
}
Topic Tags
4 Answers
1
A type of coding instruction used to compare values and express and make decisions.
 
A conditional statement tells a program to execute an action depending on whether a condition is true or false. It is often represented as an if-then or if-then-else statement.
1

Conditional statements help you to make a decision based on certain conditions. These conditions are specified by a set of conditional statements having boolean expressions which are evaluated to a boolean value of true or false. 

1

@natasha1305 A conditional statement is a computer programming tool used to handle decisions. The most common way of handling with decisions are the if and else statements.

For example:

Q: Write a code in Python to check if a person is eligible for voting or not (Age for voting is 18+)

A:

if (age >= 18):
   print("the person is eligible")
else:
   print("the person is not eligible")

For a deeper knowledge into conditionals in python, you can click here

demo.learner demo.learner 05/03/2023 5:13 pm

@akshajsrivastava Great Akshaj. The hyperlink feature which you used is also working.

1

A conditional statement is a programming construct that allows a program to make decisions based on the value of a particular condition. It is a control structure that allows the program to execute different blocks of code based on whether a specified condition is true or false.

The most common type of conditional statement is the “if-else” statement, which has the following basic structure:

if (condition) {

// code to execute if condition is true

}

else {

// code to execute if condition is false

}

It is also possible to chain multiple “if” statements together to create more complex conditions. For example:

if (condition1) {
// code to execute if condition1 is true
} else if (condition2) {
// code to execute if condition1 is false and condition2 is true
} else {
// code to execute if both condition1 and condition2 are false
}
In this example, the program will first check if “condition1” is true. If it is, the code within the first block of braces will be executed. If it is false, the program will check if “condition2” is true. If it is, the code within the second block of braces will be executed. If both conditions are false, the code within the final block of braces will be executed.
Share: