Assign an expression to a variable

Asked

Viewed 915 times

6

In c++ is it possible to execute expressions within a variable? Ex(in Lua):

n = math.random(9, 10)
a = n .. (#tostring(n) > 9 and "-" or "--")
print(a)

If n were equal to 9 it would return:

9--

If not, I would return

10-

In c++ I tried:

#include <iostream>

int main(){
    int a = (1>2 && 5 || 10);
        std::cout << a;
return 0;
}

But returned:

prog.cpp: In function ‘int main()’:
prog.cpp:4:20: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
  int a = (1>2 && 5 || 10)
                    ^
prog.cpp:5:3: error: expected ‘,’ or ‘;’ before ‘std’
   std::cout << a;
   ^
prog.cpp:4:6: warning: unused variable ‘a’ [-Wunused-variable]
  int a = (1>2 && 5 || 10)
      ^

1 answer

6


Since you don’t know the basic concepts well, you use the wrong terms. The phrase "Expressions within a variable" makes no sense. What exists is to assign an expression to a variable. Any high-level programming language can do this, it is something basic.

Conditional expression

What you’re actually wondering is whether it’s possible to have conditional expressions to generate the value to be assigned to the variable. And yes, this is possible. I don’t know any language that can’t do this. From the simplest expression to what you seem to want to do.

Many programmers do not understand the operation of some language commands because they do not understand the basic concepts. When you try to learn a lot in practice without understanding the theory, you learn everything by half and it is always more difficult to understand the whole. Commands like if, while and for expect a boolean value (true or false) always to make a decision. This value can be obtained directly through the literal or through a conditional expression, which is more common. Examples:

while (true)

Is a value as valid as while (variavel > 10). Well, then we can do this:

bool condicao = variavel > 10;
while (condicao)

What strikes me is that it’s common for people to over-create variables in programs. And in this case a variable can be created with the conditional expression and then use in the conditional command "nobody" thinks it is possible to do. I’m not saying that this form should be used often, but at some point it can be useful. The important thing here is to understand what a conditional expression is.

There’s no point in it:

bool condicao = variavel > 10;
while (condicao == true)

And I often see people do this. They think in a if or while if you don’t have a relational operator you’re wrong. And it’s just the opposite. It needs to have a boolean value. Whether it is generated by an expression or a simple value does not matter. Another example:

if (arquivo.eof() == false)

It makes no sense, it would be better to do:

if(!arquivo.eof())

Ternary operator

But what you really want to know is whether it is possible to create an expression where you test a condition and decide whether to use one of the following two results. Each language has a strategy to do this. C++ uses a ternary operator (because it has three operands) conditional.

Would look like this:

#include <iostream>

int main() {
    int a = 1 > 2 ? 5 : 10;
    std::cout << a;
    return 0;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Questions that talk about this operator:

  • Perfect. As for the conditional expression, I am aware of this, rs. But thank you for the explanation and for the clarity.

  • 3

    The complete answer is to help everyone. I take the opportunity to try to instill the right conceptualization whenever I can.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.