Or statements in RobotC

I was trying to write some code for a school project and I was wondering if it was possible to write an or statement with Until Dark statements. I’m trying to write it so that it says “Until Dark (a) or (b) or ©”. A, B and C are all line follower sensor values for different line followers. Is it possible to write something like this in RobotC?

https://www.tutorialspoint.com/cprogramming/c_operators.htm

Read the section on logical operators. You won’t be able to do it with the untilDark functions sadly.

Thanks. Is there a way to get the same type of outcome where once the parameter is met it’ll leave that else statement (it’s in an else statement and I want it to go back and check the other if statement again)?

while(sensorA > 50 || sensorB > 50 || sensorC > 50){ }

So that code says to continue executing code as long as 1 sensor sees a value greater than 50.

while(sensorA > 50 && sensorB > 50 && sensorC > 50) { }

And that code says to continue executing code as long as all 3 sensors sees a value greater than 50.

So then if that was in an else statement, what would I need to put in the brackets of that while in order for it to complete the else statement and go back and check the if statement again?

The else block will complete itself if nothing inside of it blocks the program from continuing. You don’t have to put anything specific in to make this happen; a block is “complete” even if it is empty.

As for repeating, flow control for repeating a piece of code happens outside that piece of code, not inside of it. There is nothing* you can put inside an if block to make the if block be checked again. You would have to shove the if block into something that repeats itself, like a while, do...while, or for loop.

(*Technically, there is a command that can make this happen, but I’m not going to tell you what it is because it’s considered bad practice to use it unless you have no other option.)

I already have the if/else statement in a while 1 == 1 statement so it’s it’ll run infinitely. But that helped a lot and I think I know how to fix the problem so thank you both so much! I really appreciate it.

I tried the while statement and it didn’t work so I switched if for an if statement and if still didn’t work. My teacher said that an the code “break” will bump the code out of the if statement but I’m not sure where if should go. This is the code and I’m having issues with the bottom section. I put the stop motor thing in to see if that section was even triggering and it was but it still wasn’t doing what I wanted it to.image

Would it be possible tk post your entire code? You can do this by dragging and dropping the robotc file into the browser when going to post.

And wrapping it in [code] tags!

image

This is the first half of the code. It wouldn’t let me upload the file because of the file type.

Can you cut and paste the code in? that screen is pretty much useless…

Only partially correct. break will break out of a loop (for, while, do...while) or switch statement, but does not interact with if directly. So it will jump execution out of the if block, but it will also jump it out of the enclosing loop. If you want a similar statement that jumps back to the start of the loop and starts a new loop cycle, continue can do that. It’s like break in that regard, but instead of terminating the enclosing loop, it just acts as if the program had reached the end of the loop normally.

As for the last case in your if statement, I don’t think the problem is between choice of if and while. I’m not entirely sure because I don’t know quite what you’re trying to do, but I think I’m seeing other errors in the program logic here.

1 Like