Something like this, I'd like to see the full syntax.
Pseudo Code:
var = user_input
if var > 5:
output = 'var > 5'
else:
output = 'var < 5'
Something like this, I'd like to see the full syntax.
Pseudo Code:
var = user_input
if var > 5:
output = 'var > 5'
else:
output = 'var < 5'
How about something along the lines of:
with a couple of test runs:
In addition to the other two answers, there's always the ternary operator
?:
which can be used like this:It's pretty much what you've got there. Your example:
The only difference is that you need semicolons after the statements and parentheses around the conditional expression, and the colons are not required.
You can also use curly braces to denote a block of commands to execute given a certain condition. When there's only one line being executed, however, the braces are not necessary. But this is equivalent to:
You can have braces just after the
if
or just after theelse
, or both, or neither. Remember, though, that with multiple statements the braces are required.It's also worth noting that the line breaks are optional. This could be written
Or even
But this code is far less readable. The first and second forms are better practice.
Hope this will help you get started .
This seems to correspond to what you'd like:
With caveats about using
scanf()
- I generally don't like it for resilient code, but it gives a minimal answer swiftly.You'd have to wrap it in a
main()
and#include <stdio.h>
to make it executable: