Unable to check the value of a variable in Objective-C - objective-c

I need to check a variable vi_theIndex for its value. At the given moment it has a value of 65.
I want to check if vi_theIndex is bigger or equal to zero AND smaller than 32.
Right now I do it like this:
long long vi_theIndex = 65;
if ((vi_theIndex >= 0) && (vi_theIndex < 32) )
{
//Case true
}
else
{
//Case false
}
I realized that the results are wrong for 65. The second case should come up but the first case becomes true. Why is this?
I tried this:
long long vi_theIndex = 65;
bool limitFlag1, limitFlag2;
limitFlag1 = (vi_theIndex <= 0);
limitFlag2 = (vi_theIndex = 65);
limitFlag2 becomes true and limitFlag1 becomes undefined, the debugger doesn´t even stop there on my breakpoint. It looks like C doesn´t understand the '<', '<=' or '>' signs. This also happens when I use the '<' or '>' sign alone like here:
limitFlag1 = (vi_theIndex < 0);
limitFlag1 is not defined.
Can somebody please shed some light on this?

You must not be showing your real code for your first example - as you say, "case false" should be executed.
Your second example has a problem - you have vi_theIndex = 65, rather than vi_theIndex == 65, which you probably meant. The statement as you have it is always true. limitFlag1 will be 0 - I'm not sure what you mean by it "becomes undefined" - are you not showing your real code here, too?

Related

How to move to another part of the code using an if statement

in my code I have the following if statement:
if (a.count >= 2) {
t2 = array[b % a.count];
array[0] = t2;
}
I have another if statement that goes like the first. What I want it to do is if a <= 0 then goto a certain line, or skip over certain parts of code. How would I do this? I was thinking something along the lines of
if (a.count <= 0) {
goto line 96
}
This wouldn't work, the syntax is wrong, but how would I do this?
Goto statements are generally considered bad programming and excessive utilization of them can lead to code that is hard to maintain and debug.
That said, if/else/else if provide all the functionality you need.
I recommend putting the code you need to run inside that if statement in a separate method and then calling it from the if statement.
if (a.count <= 0) {
nameOfNewMethod();
}
//somewhere else
- (void) nameOfNewMethod {
//code here
}
Put the lines of code you want to "goto" in a function (or if appropriate, a block) and call the function (or block). If there are lines of code you want to skip, you can always return early out of a function, or use an else block?
There is in fact a goto command in Objective-C. To utilize it, you have to create a label, ex:
marker:
and jump to it like so within the same method:
goto marker;
But you can't declare any variables between those two commands. All the variables have to be created before the jump so that they still exist after.
Here's an example of how to use goto:
int x = 0;
if (a.count <= 0) {
goto marker;
}
x = 5;
marker:; // <-- semi-colon indicates the label is followed by an empty statement, thus allowing for immediate variable declaration
int y = x + 7;
In that case, if a.count <= 0, y == 7, else y == 12.

How to shorten code when I do a variable assignment in a condition in Dart?

Here's the ugly long code:
var i;
if(true)
i = 1;
else
i = 0;
When I try this:
var i = (true ? 0 : 1);
it doesn't work resulting in an error on the following line. I guess I was a bit inattentive reading Dart's syntax specs, so can anybody show me the right way?
This looks perfectly fine from a syntax point of view. You can omit the parentheses.
I get a warning 'Dead code' at '1' with your example because of 'true'.
The Darteditor shows you a hint that you wrote code that may contain a bug because he knows your expression can never evaluate to 1 because of the hardcoded 'true'.
void main(List<String> args) {
var b = true;
var i = b ? 0 : 1;
}
doesn't produce a warning.

If statements not working correctly

I am developing an app where the user receives an overall score and are judged from that score and given a title. However, with the code I am using, the end result is always the same, no matter what score the subject gets. I dont know if this a math problem or a code problem, as it always comes up with the first option: You have no SWAG whatsoever...
if (totalScore<24) {
describe.text = #"You have no SWAG whatsoever...";
}
else if (25<totalScore<49) {
describe.text = #"You seem to be new to SWAG.";
}
else if (50<totalScore<74) {
describe.text = #"You have a bit of SWAG, not enough though.";
}
else if (75<totalScore<99) {
describe.text = #"You definately have SWAG!";
}
else if (totalScore == 100) {
describe.text = #"You are a GOD of SWAG.";
}
else if (25<totalScore<49) {
should be:
else if (25<totalScore && totalScore<49) {
The way you wrote it is parsed as if you'd written:
else if ((25<totalScore) < 49) {
25<totalScore will be either 1 or 0 depending on whether it's true or false. Either way, it's less than 49.
Also, all your comparisons should be <= rather than <. Otherwise, you're excluding all the boundary values.
building if in this way
if (25<totalScore<49) {...}
is risky.In reality you do something like
25<totalScore -> YES/NO (values will be casted from BOOL to int as 1/0)
and then you will do
0/1 < 49 which will be always true.
so in total your if is wrong.
Your first line of code looks right from what you have displayed so far? You need to output what total score is. You are maybe not setting it before running your code?
Failing that, are you sure its compiling properly? You need to use && in your subsequent if statements.
Also, you need to use <=, because at the moment, if the score is 24 it wont work.

How do you check if an NSInteger is greater than another NSinteger?

I'm trying to write code that detects if an integer is greater than another integer. Is this possible?
Here is what i've done so far.
if (NumCorrect >> NumWrong) {
btnCool.title = #"Awww";
}
else {
btnCool.title = #"Cool!";
}
All its doing is going to the else
EDIT:
NSString *numCorrect = [NSString stringWithFormat:#"%d",NumCorrect];
NSString *numWrong = [NSString stringWithFormat:#"%d", NumWrong];
lblWrong.text = numWrong;
lblCorrect.text = numCorrect;
if (NumCorrect > NumWrong) {
btnCool.title = #"Awww";
} else {
btnCool.title = #"Cool!";
}
Use single >
if (NumCorrect > NumWrong) {
btnCool.title = #"Awww";
} else {
btnCool.title = #"Cool!";
}
Double >> is a bit shift operation. You shift every bit in the binary representation of your variable NumCorrect NumWrong amount of bytes to the right. In almost all cases this will return in a number other then 0, which will then treated as a false value and thus the else block is executed.
Almost perfect - just take off one of those >'s. >> and << are for "bit-shifting", a weird hold-over from the earliest days of programming. You're not gonna use them much. What you really want is > and <, which is for testing if numbers are greater than each other or less than each other.
In addition, you may remember from math class that ≥ and ≤ (greater-than-or-equal-to and less-than-or-equal-to) are useful operations as well. Because there's no symbols for those on most keyboards, however, C and Xcode use >= and <= instead.
Finally, you may already know this, but to check if two numbers are exactly equal to each other you can use == (because = is used for setting the contents of variables).
Hope that's helpful!

ios int to float comparison always evaluates to true

I am trying to compare a CGFloat to an integer value. Based on this value, execute a conditional... pretty standard. However, this will always be true for some reason. I even print out the values and they are clearly less than 800.... I have tried a bunch of different combinations, the most recent is shown below, I thought maybe it was comparing the size of float and the size of the int based purely on its binary values, so I tried this risky little cast operation... Any ideas?
CGPoint textViewPoint = [scroller convertPoint:[textView center] toView:(UIView *)self.view];
NSLog(#"the y coord is %f", textViewPoint.y);
int size = (int)textViewPoint.y;
NSLog(#"the yint %d", size);
//move the main view, so that the keyboard does not hide it.
//if (self.scroller.frame.origin.y >= 0 && textViewPoint.y > 800.0);
if(size > 800);
{
NSLog(#"moving up");
The problem is the ; at the end of the if(size > 800); line, not the int vs. float comparison. Remove it and all should be OK.
This is because this semicolon is interpreted as the body of your if statement, and that's this NO-OP statement that is executed when the condition is true. Then, the rest of your code next to this if empty body is outside of the if body so is executed whatever the condition value. That's exactly as if you had written:
if(size > 800)
{
}
{
NSLog(#"moving up");
}
Compiler Warning Tip
The compiler generally warns you about this mistake. Be sure that you have the "Empty Loop Bodies" warning activated in your project Build Settings (compiler flag -Wempty-body): this way the next time you do this mistake, you will have a warning about it and will know what is wrong and how to fix it.