#Weird Syntax Question

54 messages · Page 1 of 1 (latest)

old quartzBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hasty shale
#

You don't need a return statement for a void function unless you want to preemptively exit out of the function

mellow frost
#

Also it does not make sense to have a comma after a. You’d want a semicolon

magic mirage
#

Yeah the simple answer is just use braces and semicolons

mellow frost
#
if(a) cout << a, return;
magic mirage
#

You can't abuse the comma operator for this

#

Not much else to say

#

The continue one is definitely not possible

hasty shale
#

You could do:

T f(int a) {
    if (a) { return std::cout << a, <some_value>; }
    return <some_other_value>;
}
magic mirage
#

Return you can "cheese" yes

mellow frost
#

Just do if(a) { cout << a; }

#

Much better than the cursed thing you are doing with the comma

hasty shale
#

And for a void function you can just do:

void f(int a) {
    if (a) std::cout << a;
}
magic mirage
#

No fun allowed var

mellow frost
#

Sure, but if I saw that pushed in a PR in my code base I’d get me mallet. Cat_Bonk

hasty shale
#

The continue code of yours is just an endless loop

mellow frost
#

#cursed-code moment

hasty shale
#

That doesn't work.
The comma operator expects expressions as each sequence point.
contine is not an expression, nor is return

magic mirage
#

Continue has no form that takes an expression

#

So no

hasty shale
#

You can restructure your code, smth like:

void f(int a ){
  while(a == 1){
    std::cout << "A became 1!";
  }
  a = 1;
}
#

my god, then just remove them

magic mirage
#

#define NOT_A_CURLY_BRACE {

full needle
#

yes, but I want a continue

hasty shale
#
void f(int a ){
  while(a == 1)
    std::cout << "A became 1!";
  
  a = 1;
}
full needle
#

dude

#

the code was dummy

#

the main thing I want is to be able to use continue

#

here I'll write a better one

hasty shale
#

that'd be great

full needle
#
void f(int a){
  int cnt=0;
  while(1){
    if(cnt!=5) continue;
    doSomeImportantWorkOmg();
    break;
  }
}
#

but now

#

say it has to be like

#
void f(int a){
  int cnt=0, f=0;
  while(1){
    if(cnt!=5) f^=cnt, continue;
    doSomeImportantWorkOmg(f);
    break;
  }
}
#

anyway I just want no curly braces and a continue 😭 too much to ask?

hasty shale
full needle
#

ugh

hasty shale
#

If you want to forcefully use a continue, there's almost always a better way.
Not to mention that the continue doesn't work with the , operator

#

Because - as I said before - continue is not an expression (but a statement)

full needle
#
void solve(){
    int n; cin>>n;
    vector<int> a(n);
    int f=-1, odd=-1;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=0;i<n;i++){
        if(a[i]==1) f=i, continue;
        // DON'T make this an else if and don't add a a[i]!=1
        if(a[i]&1) odd=i;
    }
}
hasty shale
# full needle ```cpp void solve(){ int n; cin>>n; vector<int> a(n); int f=-1, odd=...

I wouldn't know how to do that in a way that'd satisfy you, besides maybe this:

void solve(){
    int n; cin>>n;
    vector<int> a(n);
    int f=-1, odd=-1;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=0;i<n;i++){
        cin>>a[i];
        if (a[i] & 1)
            if (a[i] == 1) odd = i;
            else f = i;
    }
}
```or some dark `goto` magic, but I would really refrain from that.

But let me tell you, curly braces do not look ugly. Actually many people recommend to put them **even if you only have a oneliner**.
full needle
#

😔

hasty shale
#

If you want to impress them, then do some ternary magic:

for(int i=0;i<n;i++){
    cin>>a[i];
    f = !(a[i] - 1) ? i : f;
    odd = (a[i] - 1) && (a[i] & 1) ? i : odd;
}
full needle
#

awesome, that looks cool and cryptic as hell

#

I like it!

#

thanks!

old quartzBOT
#

@full needle Has your question been resolved? If so, type !solved :)

full needle
#

yea it has been

#

thanks to all involved @here

old quartzBOT
full needle
#

no just the people in this help channel man

#

thanks @hasty shale @magic mirage and @mellow frost for your help, I will close this now

#

!solved

old quartzBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity