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 glues two conditions into one test, and that test is TRUE only when both parts are TRUE.
Have a goMia's mum says: 'You can go out if your homework is done AND your room is tidy.' Homework: done. Room: a disaster zone. Does Mia go out?
No. Mia stays in.
It's tempting to think one finished job earns half a yes, but AND needs both parts TRUE, and the room part is FALSE.
- Each side of AND is a complete condition: age >= 12 AND age <= 16, never age >= 12 AND <= 16.
- 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 goes round again only while both parts are TRUE.
- That means the loop stops the first time either part is FALSE. It never waits for both.
Have a goFinish the sentence with one word: WHILE lives > 0 AND time < 60 keeps going until lives reaches 0 ___ time reaches 60.
OR
Either one turning FALSE is enough to stop an AND loop. Writing 'and' here would wrongly mean the loop waits for both.
- Watch everyday English: 'under 13 and over 65' needs OR in code, because no age is both.
- NOT flips a condition: if paused is TRUE, NOT paused is FALSE. So AND NOT paused means 'and not paused'.
Have a goA drone should keep flying while battery > 20 and it has not landed. Use the Boolean variable landed to finish: WHILE battery > 20 AND ___
NOT landed
'Has not landed' is TRUE when landed is FALSE, so NOT flips it. Plain 'landed' would only let the drone fly once it was already on the ground.
- Brackets show which parts go together. NOT (a AND b) flips the whole bracket, not just a.
- Mixing OR into an AND? Bracket the OR, as in lives > 0 AND (hasKey OR hasCoin), so it counts as one part.
The big picture
One small word, two jobs. In an IF, AND makes a single test check two things. In a WHILE loop, it keeps the loop going only while both parts hold, so the first FALSE part ends it. Mix AND with NOT and OR, and use brackets to show what belongs 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 feels right because both parts are 'in charge', but 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 28 October 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.