GCSE · Computer Science · AQA · Spec 8525
AND (Boolean operator in code)
A guessing game stops when you guess right. It also stops when you run out of tries. Two reasons to stop, one line of code. How?
Programming · Loops
Two games, one condition, two different endings
The secret number is 7 and you get three tries. The loop keeps asking while your guess is wrong AND you still have tries left. Step through two games and watch both parts of the condition at every check.
Before each step, predict: is each part TRUE or FALSE, and will the loop go round again?
1 secret ← 72 tries ← 13 guess ← USERINPUT4 WHILE guess ≠ secret AND tries < 35 tries ← tries + 16 guess ← USERINPUT7 ENDWHILE8 IF guess = secret THEN9 OUTPUT "Got it!"10 ELSE11 OUTPUT "Out of tries"12 ENDIF
| game | tries | guess | guess ≠ secret | tries < 3 | whole AND |
|---|---|---|---|---|---|
| 1 | 1 | 4 | TRUE | TRUE | TRUE |
Output
Step 1: Game 1. The first guess is 4 and tries is 1. Is 4 ≠ 7? TRUE. Is 1 < 3? TRUE. Both parts are TRUE, so the whole condition is TRUE and the loop body runs: tries goes up to 2 and the player guesses again.
Use Next to move one check at a time. Cover the last three columns and work them out yourself before you look.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
How one small word decides whether an IF runs and when a loop stops.
What you need to know
- AND joins two conditions. The whole condition is TRUE only when both parts are TRUE.
- In an IF statement, AND lets one test check two things at once, such as a username and a password.
- A WHILE loop with AND keeps going only while both parts are TRUE, so it stops as soon as either part is FALSE.
- AND can be combined with NOT and OR in the same condition. Brackets show which parts go together.
The big picture
AND joins two conditions into one. The whole condition is TRUE only when both parts are TRUE. In a WHILE loop, that means the loop stops as soon as either part becomes FALSE. You can combine AND with NOT and OR, and brackets show which parts belong together.
Key points
Worked example
Problem
A cinema program runs: IF age >= 12 AND (hasTicket OR isMember) THEN OUTPUT "Come in". Customer 1 is 14, has no ticket and is a member. Customer 2 is 10, has a ticket and is a member. Who gets let in?
⚠ Watch out
Thinking that WHILE X AND Y keeps looping until both parts are FALSE. It stops as soon as one part is FALSE. Waiting for both is how OR behaves, not AND.
Memory hook
AND is a strict bouncer: both names must be on the list. One FALSE, and you're not getting in — and a loop isn't going round again.
Check yourself
A loop starts WHILE fuel > 0 AND distance < 100. At the next check, fuel is 5 and distance is 100. Does the loop go round again? Say which part decides it, and why.
Flashcards
(6)When is A AND B TRUE?
WHILE X AND Y: when does the loop stop?
A loop controlled by AND has stopped. How do you explain why?
Why is mark < 0 AND mark > 100 a bug?
What do brackets do in NOT (a AND b)?
A shop is open 'Saturday and Sunday'. Which operator joins the two day tests?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore AQA GCSE Computer Science topics
How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 30 September 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.