How to go through sequential steps inside a while loop? - ios

I need to go through a series of steps one by one. In all, I have three steps to go through which are inside of a while loop. Once the three tests are completed, then and only then should the user be exited from the while loop. The catch is that these steps need to be done sequentially, and require the user to do each test in order, if they pass, then move on to the next step.
Here is the relevant code:
int passCount = 0;
BOOL flatPass = FALSE;
BOOL landscapePass = FALSE;
BOOL portraitPass = FALSE;
while (passCount < 3) {
if (flatPass == FALSE) {
if (device.orientation == UIDeviceOrientationFaceUp || device.orientation == UIDeviceOrientationFaceDown) {
[self pushSound];
}
}
else if (landscapePass == FALSE) {
if (device.orientation == UIDeviceOrientationLandscapeLeft || device.orientation == UIDeviceOrientationLandscapeRight) {
[self pushSound];
}
}
else if (portraitPass == FALSE) {
if (device.orientation == UIDeviceOrientationPortrait || device.orientation == UIDeviceOrientationPortraitUpsideDown) {
[self pushSound];
}
}
}
I need the user to position the iOS device in each position, and a beep sound is played to indicate a successful test. Once ALL of the three tests have been completed in order, I want the user to be exited from the loop. I figure each time a test has been cleared, I would increment the passCount counter by 1, until we reach 3 which would exit me from the loop. My issue though is how to go through each test, and in order.

Assuming this isn't running on the main UI thread, remove the while loop, replace each if and else if with a while condition, set the appropriate boolean flags to true when a test passes and you're done.

You could implement that in
deviceDidRotate()
you need a object variable for saving the current progress, or you use an enum like you have done:
int step;
then in deviceDidRotate you check:
if (step == 0 && device.orientation == UIDeviceOrientationFaceUp ) {
step = 1;
} else if (step == 1 && device.orientation == UIDeviceOrientationLandscapeLeft) {
step = 2;
} else if (step == 2 && device.orientation == UIDeviceOrientationPortrait ) {
// successful!
// now do action, reset step? call method
}

Related

How to stop the if statement from executing further

I have the following code and trying to implement a bad word filtering system but the break is not executing. I thought that break means to stop executing further if statements but it is still progressing through to adding the information to the database..
for (int i = 0; i < badWords.size(); i++) {
String badWord = badWords.get(i);
if (txt_name.toLowerCase().contains(badWord) || txt_address.toLowerCase().contains(badWord) || txt_suburb.toLowerCase().contains(badWord) || txt_state.toLowerCase().contains(badWord) || postcode2.toLowerCase().contains(badWord) || txt_doctor.toLowerCase().contains(badWord)) {
// txt_review = txt_review.replace(badWord, "*****");
Toast.makeText(MainActivity.this, "Make sure that it does not contain any rude words", Toast.LENGTH_SHORT).show();
break;
} else if (clinic_name.equals(txt_name) || clinic_name2.equals(txt_name)) {
Toast.makeText(MainActivity.this, "Medical Clinic already exists in the database!", Toast.LENGTH_SHORT).show();
break;
} else if (txt_name.isEmpty() || txt_address.isEmpty() || phone.isEmpty() || txt_suburb.isEmpty() || txt_state.isEmpty() || postcode2.isEmpty() || txt_doctor.isEmpty()) {
Toast.makeText(MainActivity.this, "Make sure you have completed all fields and also make sure they are bulk billing clinics!!", Toast.LENGTH_SHORT).show();
break;
} else {
You can try to run this code in a function and use return instead of break.

If && not working

I have looked at every similar question i can find but i am scratching to work out why my if && stament isn't working.
if ((cEventStatus) && (ddayDif = 0)) {
NSLog(#"upDateCountersWorking");
[self upDateCountersWorking];
}else if ((cEventStatus) && (ddayDif > 0 )){
NSLog(#"currentlyWorkingAddDays");
[self currentlyWorkingAddDays:&ddayDif];
}else if ((!cEventStatus) && (ddayDif = 0)){
NSLog(#"upDateCountersResting");
[self upDateCountersResting];
}else if ((!cEventStatus) && (ddayDif > 0)){
NSLog(#"currentlyRestingAddDays");
[self currentlyRestingAddDays:&ddayDif];
}else{
NSLog(#"final else");
}
I originally didn't have enough () around each statement and only & instead of &&, from the other questions i found i made the changes to come to the above code. But still no success.
In desperation i have added the following code to confirm the variables and am getting the expected results.
if (cEventStatus) {
NSLog(#"YES status");
}else{
NSLog(#"NO status");
}
if (ddayDif > 0) {
NSLog(#"greater than 0");
}else{
NSLog(#"less than 0");
}
But the if else never gives the expected result, always falls to the final else regardless of variable values.
I am sure this will be a simple issue but any help is appreciated.
This line:
if ((cEventStatus) && (ddayDif = 0)) {
isn't checking the value of ddayDif, it's setting it (to zero) - which isn't what you want.
In each of your checks you should be using == to test the value (instead of = to set it).

Tic tac toe programming iOS

For some reason the function the checkForWin is returning always a NO.
Because of that I am not able to retrieve the winner.
Else if suggest a different logic to judge winner
I am using this function every time user puts up a symbol
-(BOOL) checkForWin{
NSLog(#"yes");
// HORIZONTAL WINS
if((s1.image == s2.image) & (s2.image == s3.image) & (s1.image != NULL))
{
return YES;
}
else if((s4.image == s5.image) & (s5.image == s6.image) & (s4.image != NULL))
{
return YES;
}
else if((s7.image == s8.image) & (s8.image == s9.image) & (s7.image != NULL))
{
return YES;
}
// VERTICAL WINS
else if((s1.image == s4.image) & (s4.image == s7.image) & (s1.image != NULL))
{
return YES;
}
else if((s2.image == s5.image) & (s5.image == s8.image) & (s2.image != NULL))
{
return YES;
}
else if((s3.image == s6.image) & (s6.image == s9.image) & (s3.image != NULL))
{
return YES;
}
// DIAGONAL WINS
else if((s1.image == s5.image) & (s5.image == s9.image) & (s1.image != NULL))
{
return YES;
}
else if((s3.image == s5.image) & (s5.image == s7.image) & (s3.image != NULL))
{
return YES;
}
//right now return 1 becuase we havn't implemented this yet
else{
return NO;
}
}
-(void)displayWinner{
if([self checkForWin] == YES){
if(playerToken==1){
lbl2.text =#"X is the WINNER";
} else {
lbl2.text =#"O is the WINNER";
}
}
}
Maybe you can write
if([s1.image isEqual:s2.image2]){
}
this code instead of this control statement
if((s1.image == s2.image) & (s2.image == s3.image) & (s1.image != NULL))
{
return YES;
}
The answer by Yılmaz Gürsoy is probably correct.
The reason, is that you are comparing the references to the images, and not the image data. The references are probably not the same, while the data probably is.
He is suggesting that you compare the data instead of the references.
In my oppinion though, you should actually add a member to the s# object, containing a tristate value. could be an integer.
/**
s.x is avalue indicating which image is shown (x, O, or empty)
x > 0 means 'X'
x == 0 means 'O'
x < 0 means empty
**/
and then set it when you assign the image. and check against that.
otherwise you will be wasting time comparing data, to get the exact same end result.

If statement not calling all methods returning BOOL

I have code like this:
-(IBAction)send {
if ([self isCorrect1] && [self isCorrect2] && ...) {
[self sendRequest];
}
}
-(BOOL)isCorrect1 {
...
}
-(BOOL)isCorrect2 {
...
}
Every isCorrect method is checking some condition showing some message in the view and returning result of the checking. I noticed that if first condition is false it will only show error message for the first method (and I need all of them to be checked) and no breakpoint is triggered inside these methods. I thought it was some kind of LLVM optimization so I created code like this:
-(IBAction)send {
BOOL correct = [self isCorrect1];
correct = correct && [self isCorrect2];
...
if (correct) {
[self sendRequest];
}
}
And is still not working correctly. Do I have to create new BOOL variable to store result for the check or is there some other way?
Since the first condition is evaluated to false, it won't check for the rest of the conditions and will go to the else part straightaway.
Try this.
BOOL finalResult = [self isCorrect1];
finalResult = [self isCorrect2] && finalResult;
finalResult = [self isCorrect3] && finalResult;
finalResult = [self isCorrect4] && finalResult;
...
if (finalResult) {
}
This will go through all of the isCorrect tests and will let you know if it passed all of them in the end or not.
The behaviour you see is the expected behaviour of the &&, namely, it "short-circuits" the evaluation, if it can determine the result in advance, before having evaluated all conditions:
expression-yielding-false && something-else
The result of the above is completely determined by the first part; regardless of what the second operand yields, the final result is false. This allows you to write something like:
if (obj != null && obj->count == 3)
{
...
}
If the && did not have the short-circuit behaviour, you'd have to write
if (obj != null)
{
if (obj->count == 3)
{
...
}
}
The || has a similar behaviour. In case of
something-yielding-true || anything
the right-hand side cannot affect the result value, as the left-hand side already returned true.
One possible work-around would be:
int succeeses = 0;
succeesses += [self isCorrect1]? 1 : 0;
succeesses += [self isCorrect2]? 1 : 0;
succeesses += [self isCorrect3]? 1 : 0;
if (successes == 3)
{
// All tests did succeed
}
else
{
// At least one failed.
}
If you need to know, which tests passed, and which failed, you can try:
BOOL passed1 = [self isCorrect1];
BOOL passed2 = [self isCorrect2];
BOOL passed3 = [self isCorrect3];
if (passed1 && passed2 && passed3)
{
// All tests did succeed
}
else
{
// At least one failed.
}
A more dense version of the above would be
int passed = 0;
passed |= [self isCorrect1]? (1 << 0) : 0;
passed |= [self isCorrect2]? (1 << 1) : 0;
passed |= [self isCorrect3]? (1 << 2) : 0;
if (passed == 7)
{
// All tests did succeed
}
else
{
if (passed & (1 << 0))
{
// First test passed
}
else
{
// First test failed
}
if (passed & (1 << 1))
{
// Second test passed
}
else
{
// Second test failed
}
if (passed & (1 << 2))
{
// Third test passed
}
else
{
// Third test failed
}
}
which is simply a more occult formulation of the version with a boolean variable per test tried.

Cannot capture TouchEvent.UP in Blackberry

I am working on a Scrollable Image field.I am handling TouchEvent.DOWN mTouchEvent.MOVE,TouchEvent.UP.
Somehow control never goes to TouchEvent.UP section.How to capture the UP event.
I have to findout the start and end points of the drag.
My Code looks like this..
if (event == TouchEvent.DOWN && touchEvent.isValid())
{
_xTouch = touchEvent.getX(1);
_yTouch = touchEvent.getY(1);
}
else if(event == TouchEvent.UP && touchEvent.isValid())
{
int x = touchEvent.getX(1);
int y = touchEvent.getY(1);
}
else if (event == TouchEvent.MOVE && touchEvent.isValid())
{
boolean result = scrollImage((touchEvent.getX(1) - _xTouch), (touchEvent.getY(1) - _yTouch));
_xTouch = touchEvent.getX(1);
_yTouch = touchEvent.getY(1);
//If scrolling occurred, consume the touch event.
if (result)
{
return true;
}
else
{
return false;
}
}
Thanks in advance.
:)
it was a misunderstanding.I was handling the touch event in multiple layers..like Field level,layout manager level and screen level.
So in particular case..it was being cosumed by manager.And i need the event to be cosumed by field.
It was a mistake in return value.

Resources