Jump to content

I'm trying to make a condition be met with both while loops, and do while loops. Here's an example:

 

while (variable != 'A')

{

CODE

}

 

^^^ This would only be run if the char variable is not equal to capitol A. I have touppered all of my chars for this code, so the upper case is not causing issues. This code works great. However....I'm trying to run this but I want to add 3 other letters to the mix. For example:

 

while ((variable != 'A') || (variable != 'B') || (variable != 'C') || (variable != 'D'))

{

CODE

}

 

^^^So this should only be ran if "variable" is NOT equal to the char A, B, C, or D. I tested this in a while loop, even when variable was equal to A (couted to check) it STILL ran the code. Additionally, I need a similar statement at the end of a do while loop. In the do while loop, I want the code to repeat ONLY if variable != A, B, C, or D. Even when variable was equal to A it still ran the code(a second time to be clear, since it's a do while, will always run once)...........what the hell, someone please help? I've never had such an issue with while/do while loops before...EVER.

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
https://linustechtips.com/topic/593412-help-with-c-loop/
Share on other sites

Link to post
Share on other sites

Lets assume variable=A.

 

Then when you get to the second loop, you evaluate the conditions. Does Variable=A? Yes, go to the next condition. Does Variable=B? No, enter the loop and execute the code. So you need to change the logic of the while loop. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/593412-help-with-c-loop/#findComment-7717305
Share on other sites

Link to post
Share on other sites

 

	while (variable != 'A')
	{
	//CODE
	}
	 
	// variable can't be A, B, C and D at the same time, when you do logical OR on those conditions
	// you always get True,
	// because if the variable is 'A' then it's not 'B' so the condition is true
	// You have to make sure the variable is none of those values at the same time
	while ((variable != 'A') && (variable != 'B') && (variable != 'C') && (variable != 'D'))
	{
	//CODE
	}
	

Here's a great article explaining the difference between Logical AND and Logical OR operators

https://msdn.microsoft.com/en-us/library/z68fx2f1.aspx

 

CPU: Intel i7 5820K @ 4.20 GHz | MotherboardMSI X99S SLI PLUS | RAM: Corsair LPX 16GB DDR4 @ 2666MHz | GPU: Sapphire R9 Fury (x2 CrossFire)
Storage: Samsung 950Pro 512GB // OCZ Vector150 240GB // Seagate 1TB | PSU: Seasonic 1050 Snow Silent | Case: NZXT H440 | Cooling: Nepton 240M
FireStrike // Extreme // Ultra // 8K // 16K

 

Link to comment
https://linustechtips.com/topic/593412-help-with-c-loop/#findComment-7717328
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×