473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why does the following produce an infinite loop?

Expand|Select|Wrap|Line Numbers
  1. string url = Request.Url.AbsoluteUri.ToLower();
  2.  
  3. //not logged in code path snippet
  4.  
  5. if (!url.Contains("default.aspx") | !url.Contains("cookietest.aspx") | !url.Contains("nondisclosure.aspx") | !url.Contains("errorpage.aspx"))
  6.             {
  7.                 //only redirect if user is not on the default, nondisclosure or cookie page..
  8.                Response.Redirect("default.aspx", false);
  9.  
  10.             }
  11.  

If I attempt to go to the default.aspx page, it causes an infinite loop. It's almost like the code is not seeing the ! in front of the url.contains..

So basically i'm saying.. If NOT URL.CONTAINS... OR... If NOT URL.CONTAINS..

So.. I've debugged and the URL DOES contain default.aspx.. yet it still redirects..
Oct 14 '10 #1
4 1431
Atli
5,058 Expert 4TB
The | symbol is an OR operator. So your IF clause will be triggered if ANY of the tests are true. - Which means that if any of the four pages are NOT in the URL, the redirect happens.

You probably meant to use the AND operator, &.
Oct 14 '10 #2
Curtis Rutland
3,256 Expert 2GB
And instead of the bitwise operators, you probably wanted to use the logical operators:

Logical And: &&
Logical Or: ||

In this case, it's not a big deal, but if you ever applied these single character operators to integers, you'd be surprised by the result.

Now, on to your logic. Read De Morgan's Laws. It basically states:

NOT (P AND Q) = (NOT P) OR (NOT Q)
NOT (P OR Q) = (NOT P) AND (NOT Q)

The logic you're going for:

If my page contains "A" or "B" or "C" or "D", don't redirect.

Which can be rewritten as
Expand|Select|Wrap|Line Numbers
  1. IF(A OR B OR C OR D) 
  2.   {do nothing} 
  3. ELSE 
  4.   {do something}
Which again, can be rewritten as:
Expand|Select|Wrap|Line Numbers
  1. IF NOT (A OR B OR C OR D) 
  2.   {do something}
Now, by De Morgan's law (the second one), we can re-write this:
Expand|Select|Wrap|Line Numbers
  1. IF((NOT A) AND (NOT B) AND (NOT C) AND (NOT D)) 
  2.   {do something}
So you see, you have to swap your ORs for ANDs.
Oct 14 '10 #3
Curtis Rutland
3,256 Expert 2GB
To explain it a bit less theoretically, walk yourself through this.
Here's your conditional:
Expand|Select|Wrap|Line Numbers
  1. if(!url.Contains("default.aspx") | !url.Contains("cookietest.aspx") | !url.Contains("nondisclosure.aspx") | !url.Contains("errorpage.aspx"))
So let's assign each to an imaginary variable:
a = url.Contains("default.aspx")
b = url.Contains("cookietest.aspx")
c = url.Contains("nondisclosure.aspx")
d = url.Contains("errorpage.aspx"))

And rewrite your conditional as:
Expand|Select|Wrap|Line Numbers
  1. if(!a || !b || !c || !d)
So, let's pretend that url = "default.aspx";
a = T
b = F
c = F
d = F

So then:
!a = F
!b = T
!c = T
!d = T

Now replace the variables in the conditional with truth values:

Expand|Select|Wrap|Line Numbers
  1. if(F || T || T || T)
Since every operator is OR, and at least one operand is true, the entire statement evaluates to True.

In case you can't tell, I like boolean logic.

If my explanation seems like overkill or insulting, it was not intended that way. I like to thoroughly explain things like this because lots of people struggle with these concepts. Hope it has helped.
Oct 14 '10 #4
Atli
5,058 Expert 4TB
And instead of the bitwise operators, you probably wanted to use the logical operators:
Good point. Totally missed that one.

My C# skills are apparently getting rusty! :)
Oct 14 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
3
by: Stewart | last post by:
Dear javascripters, Through a frustrating afternoon of debugging I appear to have discovered something: Setting location.hash to an empty string in the global namespace (not inside a...
1
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote...
0
by: Vedran Vukotic | last post by:
Hello I made the following infinite loop that check's if the bot died and call it again: ------loadloop.pl----- #!/usr/bin/perl use strict; while (1) {
11
by: jojobar | last post by:
I have a aspx file (snippet shown below): ======= <td class="light-m1" id="rwCompleteButton" runat="server"><br/> <asp:ImageButton CssClass="clear-m1" runat="server" CommandName="Complete"...
1
by: Bob | last post by:
The below regular express can sometimes cause an infinite loop. Anyone know what could cause this? string RegexSig = @"To:*\s*(?<address>\S+@\S+)*(\s|\S)*did not reach the following recipient"; ...
10
by: =?ISO-8859-1?Q?G=E9rard_Talbot?= | last post by:
www.authoring.stylesheets] Dear fellow CSS colleagues and web authors in alt.html discussion forum, I would like to ask you to help me confirm that there is a serious bug in IE 7 final release...
48
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
3
by: jimgym1989 | last post by:
import java.io.*; import java.util.*; import java.lang.*; public class ArrayNo9 { static Scanner console = new Scanner(System.in); public static void main(Stringargs) { int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.