Page 1 of 1

Using if while multiple conditions need to be met

Posted: 19 Nov 2017, 03:29
by autopuff
My code works fine but it's super redundant. I need to meet two conditions (A = Window AND B = Image to File) to take a screenshot, or it will do several clicks to meet the 2 conditions. The following is a simplified version of my code:

Code: Select all

if A = Window
	{
		if B= Image to File
		{
			Take the screenshot
		}
		else
		{
			Several clicks to make B= Image to File
			Sleep, 100
			Take the screenshot
		}
	}
	else
	{
		Several clicks to make A = Window
		if B= Image to File
		{
			Take the screenshot
		}
		else
		{
			Several clicks to make B= Image to File
			Sleep, 100
			Take the screenshot
		}
	}
Is there a way to simplify this? Two conditions are already pretty time-consuming for me to write the code in that fashion, but I might run into situations requiring more than that. I really appreciate anyone who can help.

Re: Using if while multiple conditions need to be met  Topic is solved

Posted: 19 Nov 2017, 08:29
by Exaskryz
The only thing I'm seeing is "factor out" the Take the screenshot step. It's common to both second-level if's.

Code: Select all

If A = Window
    {
        If B!=Image To File ; this is != or not-equals
        {
        Several clicks to make B the image to file
        Sleep, 100
        }
    Take the screenshot ; saves you a line here, and an else
    }
    else
    {
        Several clicks to make A = Window
        If B!=Image To File ; this is != or not-equals
        {
        Several clicks to make B the image to file
        Sleep, 100
        }
    Take the screenshot ; saves you a line here, and an else
    }
But isn't that curious? We have two very similar blocks. All of this is common to each other:

Code: Select all

    {
        If B!=Image To File ; this is != or not-equals
        {
        Several clicks to make B the image to file
        Sleep, 100
        }
    Take the screenshot ; saves you a line here, and an else
    }
The only difference is making A = Window, so, what if we just do this?

Code: Select all

If A != Window
    Several clicks to make A = Window
If B!=Image To File ; this is != or not-equals
        {
        Several clicks to make B the image to file
        Sleep, 100
        }
Take the screenshot ; saves you a line here, and an else
If the Window and Image to File are correct, then it'll just take the screenshot. If either or both condition(s) is/are not met, it'll complete actions to meet that/those respective condition.

Re: Using if while multiple conditions need to be met

Posted: 20 Nov 2017, 15:57
by autopuff
Exaskryz wrote:The only thing I'm seeing is "factor out" the Take the screenshot step. It's common to both second-level if's.

Code: Select all

If A = Window
    {
        If B!=Image To File ; this is != or not-equals
        {
        Several clicks to make B the image to file
        Sleep, 100
        }
    Take the screenshot ; saves you a line here, and an else
    }
    else
    {
        Several clicks to make A = Window
        If B!=Image To File ; this is != or not-equals
        {
        Several clicks to make B the image to file
        Sleep, 100
        }
    Take the screenshot ; saves you a line here, and an else
    }
But isn't that curious? We have two very similar blocks. All of this is common to each other:

Code: Select all

    {
        If B!=Image To File ; this is != or not-equals
        {
        Several clicks to make B the image to file
        Sleep, 100
        }
    Take the screenshot ; saves you a line here, and an else
    }
The only difference is making A = Window, so, what if we just do this?

Code: Select all

If A != Window
    Several clicks to make A = Window
If B!=Image To File ; this is != or not-equals
        {
        Several clicks to make B the image to file
        Sleep, 100
        }
Take the screenshot ; saves you a line here, and an else
If the Window and Image to File are correct, then it'll just take the screenshot. If either or both condition(s) is/are not met, it'll complete actions to meet that/those respective condition.
Thank you for your response! It's very easy to follow. When I run the code, I can see the conditions are changed if they are not met, but it doesn't take the screenshot. I use the ControlClick to do that and it works fine in the original code. Is there a reason for that?

Re: Using if while multiple conditions need to be met

Posted: 20 Nov 2017, 16:35
by Exaskryz
We may need to see the final code to make sure nothing odd is going on. Do you use return in either of the If blocks? Do you use use an else statement? The final code that I simplified down to didn't use any elses.

Re: Using if while multiple conditions need to be met

Posted: 20 Nov 2017, 16:49
by Osprey
The simplest way to simplify is simply (heh) to move the redundant parts out into subroutines, like so...

Code: Select all

if A = Window
{
	Goto, BImage
}
else
{
	Several clicks to make A = Window
	Goto, BImage
}

ExitApp

BImage:
	if B= Image to File
	{
		Goto, TakeScreenshot
	}
	else
	{
		Several clicks to make B= Image to File
		Sleep, 100
		Goto, TakeScreenshot
	}
return

TakeScreenshot:
	Take the screenshot
return
That should do exactly what your old code did, minus the ExitApp that I added so that it doesn't continue on and process BImage unless called. You can replace it with Return or other code that you have.

Re: Using if while multiple conditions need to be met

Posted: 20 Nov 2017, 16:50
by autopuff
Exaskryz wrote:We may need to see the final code to make sure nothing odd is going on. Do you use return in either of the If blocks? Do you use use an else statement? The final code that I simplified down to didn't use any elses.
Nevermind. It works just fine now. This thing happened last time when I used ControlClick as well. Thank you very much!