Questions tagged [conditional-operator]

The conditional operator is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages. It is also commonly referred to as the ternary operator or inline if. Different languages have different syntax for the same construct, but all select between one of two options based on a condition.

Filter by
Sorted by
Tagged with
0 votes
1 answer
39 views

Understanding Javascript ternary operator syntax and reduce syntax [closed]

I'm using javascript and trying to combine reduce and the ternary if/else statement. I am getting an extremely unexpected result and I cannot figure out why. It almost seems like a bug in branch ...
jand's user avatar
  • 13
-4 votes
0 answers
33 views

how to understand python loops? [closed]

Hello I'm learning python, it is also my first language. I'm trying to figure out how classes work. I have this small code and I can't get it to work after a week of searching. Thank you for helping ...
Aarav Singh's user avatar
-4 votes
0 answers
28 views

what is different types of javascpit and how we can implement this? [closed]

I didn't understand about div tag and span tag Anyone can understand to me what is the use of javascript and where we can implement the javascript in html and how i can use ? if anyone can tell me ...
Banshi prasad12311895's user avatar
1 vote
1 answer
67 views

Why does a ternary operator make a character variable print an integer rather than a character?

char g = 'G'; int i = 0; System.out.print(1 == 1 ? g : 0); System.out.print(" "); System.out.print(1 == 0 ? i : g); In theory, this should print out "G G" as it prints the value ...
Felipe Russo Henriques's user avatar
-5 votes
0 answers
91 views

Why does my C++ code produce different output on my computer compared to Codeforces?

The code below for solving this problem on codeforces is giving different output for the same set of inputs on my machine and on codeforces and I haven't been able to figure out the reason for it. I'...
pranav's user avatar
  • 1
0 votes
2 answers
64 views

Condition operator, doesn't react on condition changes, I guess

I have a React component representing a form with inputs (Text, Number, Date). The date input has a default value - today. I use this component for two functions: creating an object and editing an ...
Caplex's user avatar
  • 3
0 votes
0 answers
34 views

Why is the conditional operator in java behaving diffrently with conversions? I want to print float as integer form if the decimal part is 0 [duplicate]

I wanted to make a type of formatted printing (without using the printf() method) so that I can print a float varible without decimal point if the number does not contain a decimal part. I chose to ...
Kristi Teneqexhi's user avatar
0 votes
2 answers
46 views

Typescript is complaining about ternary operator

I have a simple function in component: const Button = ({text}: Props) => { const {t, ready} = useTranslation(); const [title, setTitle] = useState(''); useEffect(() => { if (ready) { ...
Alyona's user avatar
  • 1,748
0 votes
2 answers
31 views

Filter a sheet to keep rows only when they have a value in one column OR another column

I've got an array of 5 rows, 5 columns (as an example, my spreadsheet has 500 rows). I would like // to set a filter on that sheet to keep only rows which column 4 or clumn 5 equal to a value. I don't ...
Sébastien Chevin's user avatar
0 votes
1 answer
28 views

Multiple conditions

I'm trying to make multiple conditions: adae <- adae %>% mutate(AREPIOD = if_else((AESTDTC >= AP01SDT) & (AESTDTC <= AP01EDT), 1, if_else((AESTDTC >= ...
Olga Serebryakova's user avatar
-1 votes
2 answers
93 views

Problem using PHP null coalescing together with ternary operator to reset a Session [duplicate]

I am trying to reset the Session with the below code: <?= $_SESSION['pettySuccess']??''; (($_SESSION['pettySuccess']??'')=='') ?'': unset($_SESSION['pettySuccess']); ?> the idea behind my ...
achur00's user avatar
-1 votes
0 answers
30 views

Creating a column based on logical condition using r language

I am learning r programming. I have a table of 4 date columns ("consent_date", "LAR2_consent_date", "lab_date", "dxa_date"). I want to add another column (...
siddharthaUD's user avatar
-1 votes
1 answer
82 views

Why aren't these two ternary expressions equal? [duplicate]

When I run the below code, I get two different values for i. Why? The only difference is the first uses i = i + 1 whereas the second uses i += 1 which are equivalent in all other use cases I've seen. ...
Vux's user avatar
  • 17
0 votes
2 answers
88 views

Ternary operators gives compilation errors

I'm trying to print the result based on the input of arithmetic operator double n1= 45; double n2= 9; double result = (mathOperator == '/') ? n1/n2 : (mathOperator == '+') ? n1+n2 : ...
ImMe's user avatar
  • 9
0 votes
0 answers
9 views

Trying to Conditionally Render SVG's in a Custom React Select Component

Good Morning, I am currently trying to render a custom dropdown indicator in a React Select Component - either down or up, based on whether or not the menu is open. The custom component: const ...
Jake Matthews's user avatar
1 vote
2 answers
30 views

R mutate across defined variables and change values based on another column

I would like to replace values with "NA" in a data frame but only if a value in a specified column is out of a defined range. Here's an example: Assuming I have 5 columns in a data frame (...
Juli's user avatar
  • 49
0 votes
1 answer
56 views

otree: activate dynamic choice for a question depending on the answer to a previous question (on the same page)

I am currently trying to implement a piece of code in which first I ask a question and after this question is answered I want another question to appear. The problem is that the second question that ...
Martina's user avatar
  • 11
0 votes
2 answers
48 views

How to use .assign with with .where() for "condition, true, false" like in numpy.where()?

I have a dataframe df: # %% import pandas as pd # %% values = [("a", 1), ("b", 2), ("c", 3), ("d", 4), ("a", 4), ("b", 6), ("c", 7)...
Vega's user avatar
  • 2,699
0 votes
0 answers
16 views

Shopify Metafield Variant Archive = True

My main issue is, I got lots of products here on shopify, and some of products has Variants as well. My concern is some of variants aren't available on this season, so I assign them into metafields ...
Bernie Sotto's user avatar
1 vote
2 answers
113 views

Should the expression x && (~x) return 1 or 0? And does it depend on the compiler?

In C, given "short x = 0xFFF0" what is the output of "x && (~x)". This question was proposed to me on a quiz and the answer for it was 0. Although when compiled 1(true) is ...
Zayed Aldhaheri's user avatar
0 votes
1 answer
57 views

optimize two ternary operation in dart

I still haven't found anyway for optimizing this code, I think there should be something missing from the code, please help me to make it shorter. firstWay: fromAddress Get....
The Code Whisperer's user avatar
0 votes
0 answers
47 views

HTTP Error 500 when using ternary operator in PHP

I recently updated the php version on my family tree website from 7.4 to 8.1 and also enabled SSL Certificate to get rid of the "this website is not secure" message. Now my page generates an ...
Lee Worbey's user avatar
0 votes
1 answer
33 views

ternary-operator `'messege' in order` returns true but it still does not show anything in react [duplicate]

I am trying to print order.messege if it exists in order object here are the console.logs <div> { typeof(orderData) !== 'undefined' && orderData !== null ? ...
Akshat Sharma's user avatar
1 vote
2 answers
104 views

Getting unexpected results when using macros with arguments [duplicate]

#include <stdio.h> #define big(a, b) a > b ? a : b #define swap(a, b) temp = a; a = b; b = temp; int main() { int a = 3, b = 5, temp = 0; if ((3 + big(a, b)) > b) swap(a, ...
Kanishk's user avatar
  • 83
0 votes
0 answers
58 views

Is it possible to have a ternary operator, if,if/else statement and switch statement on 1 src code in Java?

I tried all 4 on 1 src code and my program stops half way through. here's the code, what did I do wrong?: /* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change ...
Tshepo's user avatar
  • 1
1 vote
1 answer
101 views

Can unexecuted branches of a conditional operator cause undefined behavior at compile-time?

As far as I can tell, the ternary ?: operator can be evaluated at compile time if at least the condition and the executed branch are compile-time constants. However, what if one of the unexecuted ...
user16217248's user avatar
  • 3,729
1 vote
1 answer
107 views

Why can GCC not handle compile-time evaluation of compound literal in a ternary with sizeof as a condition inside a struct initializer?

The following code: struct Int { int i; }; const struct Int i = {sizeof(int) ? (int){1} : 0}; Results in: initializer element is not constant (Live demo GCC) Even though those statements are at ...
user16217248's user avatar
  • 3,729
-4 votes
1 answer
71 views

Execution of Assigning value and Logical AND and Ternary operator [duplicate]

I am learning JavaScript, and I do not understand what is the execution sequence of 2nd line. Please advise! let envArr; const envName = envArr && envArr[0] ? envArr[0] : "env1";
user3055964's user avatar
0 votes
0 answers
51 views

Role of parenthesis in a ternary operator expression [duplicate]

I want to know what parenthesis exactly doing in following code (or conditional operator.) : int main(){ int score = 91; cout<<((score>90)? "A grade!" : "Good"); } ...
GGY's user avatar
  • 1
6 votes
0 answers
147 views

Is it OK to explicitly cast prvalue to const reference to avoid extra copy in ternary operator?

Let's suppose we have some non-copyable and non-movable type Foo, and a function int f(const Foo& foo) { ... // somehow compute the result depending on the value of foo. } Now let's suppose we ...
Alexander Belous's user avatar
0 votes
3 answers
64 views

Operators for set and not empty

Often I'm using ?? to check variables, but that will pass the left variable even if empty. I want to express (!empty($b)) ? $b : $c Close is: $b ?: $c Except that will throw a warning if $b is not ...
Thomas's user avatar
  • 103
0 votes
2 answers
37 views

Wrongs in close of trades in MQL4

I created this code to Open trade if conditions true and close them if conditions true But he only Open trade and not close them. This is my code extern double lotSize = 0.02; extern double stopLoss ...
Hilal Frikol's user avatar
0 votes
2 answers
75 views

C - ternary operator in initialization statement of a for-loop

I am quite new to C and preparing for a test by working through some sample questions. Given the code below, I don't understand the initialization of int j by using the ternary operator. for (int j = ...
paranoid_android's user avatar
-1 votes
1 answer
37 views

react jsx and ternary operator issues [closed]

pleaaase why i have always this error when i work with the ternary operator in a handle function were i want to set state of the class i don't have this problem with if() but in the exercice they ...
zineb's user avatar
  • 1
1 vote
1 answer
64 views

Assignment of ternary operator to different variables in C#

If there are these variables: bool _v; int c = 5; int a = 0; int b = 0; Is it possible to express if (_v) { a+=c; } else { b+=c; } using a ternary operator?
Alex's user avatar
  • 972
0 votes
1 answer
75 views

Hide Shipping Method conditionally based on Shipping Class, Cart Quantity and Cart Total in WooCommerce [duplicate]

I am encountering some issues with my condition to check for cart total, amongst cart quantity and shipping classes. What I am trying to achieve I am trying to hide a specific shipping method ...
Richard's user avatar
  • 61
-1 votes
3 answers
97 views

Why does my c program not print my error message after a condition is true in my if statement?

In my program, I want to check if the values input by the user will produce a valid table, if not I want to print an error message. I used an if statement followed by printf. This works for all the ...
Bon's user avatar
  • 25
0 votes
0 answers
27 views

Pandas: Conditional left-join using date-intervals [duplicate]

I have two dataframes, df1 and df_weight: import pandas as pd df1 = pd.DataFrame({'date': [pd.to_datetime("2023-01-01"), pd.to_datetime("2023-01-01"), ...
TylerD's user avatar
  • 391
0 votes
1 answer
35 views

How to Dynamically Evaluate Variable Java Expressions from JSON Data at Runtime?

I have a requirement to dynamically evaluate Java expressions provided as strings within my Java application. This expression is a ternary operator and it can change in the future as it's coming from ...
Brian Figueroa's user avatar
0 votes
0 answers
82 views

PATCH request don't work in ODATA with DataServiceContext and If-Match

The ODATA Version is 4 and the ODATA Client Version is 7.13.00. I try to send a conditional PATCH request using If-Match header using the OData Client Context and a DataServiceCollection and ...
Alexandre Mirabel's user avatar
-1 votes
4 answers
139 views

How to use ? question mark in for loop in C?

If i is even, for(int j = 0; j < m; j++) Else if iis odd, for(int j = m-1; j > 0; j--) I would like to combine the two conditions as follows. for( (i%2==0) ? (int j = 0; j < m; j++) : (int j =...
ident's user avatar
  • 68
9 votes
0 answers
213 views

What is the return type of ternary operator applied to 'const char* const&' and 'char* &'?

Consider the following useless code snippet, which gives inconsistent results with different compilers (demo) #include <type_traits> char* dummy; const char* const& a = dummy; char* ...
康桓瑋's user avatar
  • 36.4k
1 vote
1 answer
85 views

ANTLR accepting special characters like . (dot) and , (comma) in identifier or expression

I'm trying to execute dynamic expressions for the input data (Map) using ANTLR and using the answer of @Bart Kiers from the stackoverflow's post I was able to do it. I tried to add IN, STARTSWITH, ...
User1512's user avatar
0 votes
1 answer
66 views

JAVA: Compiler error using ternary operator in method overload

String a = "bla"; byte[] b = new byte [10]; method (a != null ? a : b, b); Why can't I use the above method call using ternary operator? Using if else statement works fine: if (a != null) {...
NotInCheck's user avatar
2 votes
2 answers
79 views

C++20 return a tuple in ternary statement [duplicate]

I have a small function that returns three values. I use a std::tuple to facilitate returning of multiple values. What the function returns is determined by one variable, if the variable is not zero ...
Ξένη Γήινος's user avatar
2 votes
2 answers
246 views

How to use await in a ternary operator in C#?

With this code the call to SoftUpdateAsync and UpdateAsync seems not awaited and I got an error of deadlock transaction. _ = await _tabRepository.IsReferencedFromAnotherTableAsync(entity.Id) ? ...
rinomau's user avatar
  • 1,278
1 vote
2 answers
140 views

Casting inside a ternary operator causes underflow

I have the following snippet of code #include <iostream> int main() { int64_t res; int some_val = 5; if(false) { res = static_cast<uint32_t>(some_val); } ...
someCoder's user avatar
  • 185
1 vote
2 answers
88 views

Why is Ternary Operator Considered an Expression and not a Statement?

I know that an expression produces a value while a statement is an action. However, I don't understand why the ternary operator falls under the category of an expression. From what I understand, the ...
Unknown User's user avatar
0 votes
2 answers
34 views

Excel VBA Change Cell Value From Empty Lines Only To Blank Value

I am new to VBA. I have searched my challenge online but still cannot figure it out. Below please find my challenge: I have a table with 3 columns. Column A, B and C. I have 3 conditions to locate the ...
vivianna's user avatar
0 votes
0 answers
84 views

Correctly using union types and comparisons

I'm pretty new to TypeScript, and I'm having trouble wrapping my head around defining a variable to be a union type and using a ternary operator, which is throwing a compile error. I have a variable ...
lkalliance's user avatar

1
2 3 4 5
40