Exit early (abort) and max # of tests allowed.
Ok, I have two more questions.
Is there a way to about the current evaluation, say, if you want the program to abruptly end for whatever reason..
and
How many if's &&'s and ||'s can you do?
I mean, is this legal (code below) or is there some elegant way of asking for 3 different logical operations?
Code:
if (num > 0 && num < 100 && num != 50)
do something.....
else
do something else....
What about combining them
Quote:
Originally Posted by
matsp
Code:
How many if's &&'s and ||'s can you do?
More than you ever need. I think there is a statement in the C standard that says the compiler must allow 127 or something along those lines, but that's not to say that modern compilers won't do more than that.
I just compiled this [snipped to keep it short]
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000
#define LIMIT 60
int main()
{
int x[SIZE];
int i;
for(i = 0; i < SIZE; i++)
{
x[i] = rand();
}
if (
x[0] > LIMIT &&
x[1] > LIMIT &&
x[2] > LIMIT &&
x[3] > LIMIT &&
x[4] > LIMIT &&
x[5] > LIMIT &&
x[6] > LIMIT &&
x[7] > LIMIT &&
x[8] > LIMIT &&
x[9] > LIMIT &&
x[10] > LIMIT &&
<....>
x[48] > LIMIT &&
x[49] > LIMIT
)
{
printf("All numbers above LIMIT\n");
}
else
{
printf("Some numbers under LIMIT\n");
}
return 0;
}
So that is 50 && conditions in one expression. If your actual code reaches 50 conditionals in one statement, you probably should rethink your solution in some way.
--
Mats
What about combining the statements..
like:
if (something > somethingelse && something > anothing thing && something != somethingdifferent || something < someotherdifferentthing)
What is allowed for something like that? In Autolisp you would use the COND function, which evaluates several different situations like:
Code:
cond ((x > y) (setq a 1)) ;SETQ is equal to the A = B; It just "sets equal" 'A' to 'B'.
((x < v) (setq a 2))
((a = 1) (alert "X is Greater than Y!"))
((a = 2) (alert "X is Less than V!"))
.... and keep going (lisp is used with parenthesis and different syntax obviously)
is there a way to do something like this in C / C++? I know about the switch command, but that requires an integer (or single character) to determine a condition.. I would like to just switch a condition depending on the outcome of the previous (or just one) original condition. (Or maybe you WOULD make use of the switch command?) I don't know how it's normally done in C/C++.
Thanks.