Opposite of 'is' in if statement? - if-statement

I want to see if this statement is false:
if twData is Array {
}
isnt and isnot don't seem to exist.
This doesn't work:
if (twData is Array) == false {
}
So I'm not sure exactly how to do this, other than the less clean:
if twData is Array {
} else {
//Code goes here
}

If you know the generic type stored in the array, then you should make it explicit:
if !(twData is Array<Int>) {
// Do something
}
If instead you just want to know if it's an array regardless of the generic type, then you have to use NSArray:
if !(twData is NSArray) {
}

Documentation says:
let isarray = twData is Array
if !isarray {
do something
}

Related

C# - Simpler/Shorter way to make an 'if-or' statement

Is there any way to shorten this statement:
if(string.Equals("Hello") || string.Equals("Hi") || string.Equals("Hey")) { }
To something like:
if(string.Equals("Hello" || "Hi" || "Hey")) { }
It's not necessary, but can be handy.
Thanks to #thelaws who suggested using an array of the possible values and flipping the statement, which I got to work with:
if(new string[]{"Hello", "Hi", "Hey"}.Contains(value)) { }
if ((new List<string> { "Hello", "Hi", "Hey" }).Contains(yourValue))
{
//your code here
}
Here I created a list of strings with values Hello, Hi and Hey. Then I am just searching whether the value of variable yourValue is present in the created list.

Functioning if statement, improving it

if (dog.equalsIgnoreCase("yes")) {
drink.don.setCost(8.75);
drink.don.getType();
drin.l.add(drink.don.getType());
drink.c.add((double) coke.don.getCost());
cokeprice = coke + fanta.don.getCost();
else if (dog.equalsIgnoreCase("no"))
else catch(IllegalArgumentException iae) {
System.out.println("requires yes or no");
}
}
Ignore the stupid naming conventions had to change them, incase any class mates decided to steal anything ;p
I'm trying to get my if statement to allow the user input yes and do a condition, then if "no" has been entered then nothing happens just moves onto the next statement, then anything else is illegal and the program crashes.
I don't like throwing exceptions, especially if I expect that the user might type in something that I don't want. I'd rather do something like
if (userInput.equalsIgnoreCase("Yes")) {
// do yes
}
else if (userInput.equalsIgnoreCase("No")) {
// do no
}
else {
// Sorry, invalid input
}
I don't know what language you are using, nor do I know what any of the methods you are using do, but here is an example of a similar statement in C#.
First, use a method to convert the user input to a true or false (boolean) value:
public static bool IsYes (string userInput)
{
if (userInput == "yes')
{
return true;
}
else if (userInput == "no")
{
return false;
}
else
{
throw new CustomException();
}
}
Next, you can take the result of IsYes() and use it for the if else statement:
if (IsYes(userInput))
{
// code you want to execute if "yes"
}
else
{
// code you want to execute if "no"
}
Hopefully this code will give you an idea of how to use if-else statements, but in the future please explain your question more clearely. Remember, this is C#, so although if statements are similar in almost all languages some of the other code will differ. Also, this is just an example, it won't do anything on its own.

c++ function check true condition

I make a function call and when it returns false, I continue to check indefinitely until it returns true. Is the following code is fine?
while(true)
{
bool result = func();
if(result == false)
continue;
else
break;
}
How about getting rid of the break and continue. It is not considered very nice to use them (especially when not required):
bool result;
do
{
result = func();
if(result == false) {
// Supposedly you want to do something here...?
}
} while(result == false);
Of course you can use ! instead of false to save some bytes in your source code. But I suppose that does not really matter at this point.
You could do that, but why not just:
while(!func())
{
// do what you want to do...
}
Why not use
while (!func());
instead? Some folk don't like to see an empty while and may therefore prefer #dwxw's solution.
You can make it shorter.
do
{
} while (!func());

How can I tell that I'm at the last object in a foreach, in Qt

I was wondering if anyone knows of a library method or function within Qt that will tell you when you've hit the last object in a foreach.
Below I'm rolling on a list of strings and I've made up a fictional method below called "isLast()":
foreach( QString a_string, string_list )
{
if ( a_string.isLast() ) // does something like this exist?
{
...
}
}
Does anyone know if anything like "isLast()" exists?
Thanks,
Wes
I've not seen an isLast()-style function around QT. Your best bet is probably to mix in a little old-school counter logic:
int str_count = 0;
int str_list_last_elem = string_list.size()-1;
foreach(QString a_string, string_list) {
str_count++;
if (str_count == str_list_last_elem) {
...
}
}
If the strings in string_list all have unique values you could do:
foreach(QString a_string, string_list) {
if(a_string == string_list.last()){
// it's the last string
}
...
}
Otherwise you would have to use some sort of counter as #ascentury suggested.

boost lexical cast <int> check

This should be an easy one. I have a function that traverses a csv and tokenizes based on commas and does things with the tokens. One of these things is convert it into an int. Unfortunately, the first token may not always be an int, so when it is not, I'd like to set it to "5".
Currently:
t_tokenizer::iterator beg = tok.begin();
if(*beg! ) // something to check if it is an int...
{
number =5;
}
else
{
number = boost::lexical_cast<int>( *beg );
}
Seeing as lexical_cast throws on failure...
try {
number = boost::lexical_cast<int>(*beg);
}
catch(boost::bad_lexical_cast&) {
number = 5;
}
I don't normally like to use exceptions this way, but this has worked for me:
try {
number = boost::lexical_cast<int>(*beg);
} catch (boost::bad_lexical_cast) {
number = 5;
}