Recent

Author Topic: How to use two conditions on while  (Read 1308 times)

jart

  • Newbie
  • Posts: 1
How to use two conditions on while
« on: November 10, 2021, 04:46:29 pm »
Hi guys, I need help.
I want to put two conditions on the while below, basically I want the finalanwser to be different then 'n' or 'N', I've tried While ((finalanwser <> 'N') or (finalanwser <>'n')) but it didnt work when I ran the program.

While (finalawnser <> 'n') do

Imants

  • Full Member
  • ***
  • Posts: 196
Re: How to use two conditions on while
« Reply #1 on: November 10, 2021, 04:54:51 pm »
I thin you do not need to have two checks you can just change string to lower case then 'N' or 'n' would give same result

Code: Pascal  [Select][+][-]
  1.   While (finalawnser.ToLower <> 'n') do

And you need to use "and" for both conditions to work

Code: Pascal  [Select][+][-]
  1. While ((finalawnser <> 'n') and (finalawnser <> 'N')) do  

because if you use "or" one of conditions always will be true and while will continue

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: How to use two conditions on while
« Reply #2 on: November 10, 2021, 05:04:18 pm »
Also:

Code: Pascal  [Select][+][-]
  1. while not((FinalAnswer = 'n') or (FinalAnswer = 'N')) do
  2. // or
  3. while UpCase(FinalAnswer) <> 'N' do
  4. // or
  5. while UpperCase(FinalAnswer) <> 'N' do
  6. // or
  7. while LowerCase(FinalAnswer) <> 'n' do

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to use two conditions on while
« Reply #3 on: November 10, 2021, 05:08:50 pm »
Hi!

And sets are always forgotten:

Code: Pascal  [Select][+][-]
  1. while not (FinalAnswer in ['N','n'] ) do ...

Winni


Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: How to use two conditions on while
« Reply #4 on: November 10, 2021, 05:16:25 pm »
+1 winni

Relatively short and easiest to understand (at least for me). I should include more set data types in my code.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: How to use two conditions on while
« Reply #5 on: November 10, 2021, 05:27:25 pm »
Hi guys, I need help.
I want to put two conditions on the while below, basically I want the finalanwser to be different then 'n' or 'N', I've tried While ((finalanwser <> 'N') or (finalanwser <>'n')) but it didnt work when I ran the program.

While (finalawnser <> 'n') do
U need to use the"and" not "or" in your attempt
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018