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 ← 7
2 tries ← 1
3 guess ← USERINPUT
4 WHILE guess ≠ secret AND tries < 3
5 tries ← tries + 1
6 guess ← USERINPUT
7 ENDWHILE
8 IF guess = secret THEN
9 OUTPUT "Got it!"
10 ELSE
11 OUTPUT "Out of tries"
12 ENDIF
gametriesguessguess ≠ secrettries < 3whole AND
114TRUETRUETRUE

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.

1 / 7

Use Next to move one check at a time. Cover the last three columns and work them out yourself before you look.

Step 1 of 7: 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..

Exam line: Asked why a loop like this stopped? Name the part that was FALSE at the last check, and give the values that made it FALSE.
Watch out: The loop does not wait for both parts to be FALSE. In game 1 it stops while tries < 3 is still TRUE. In game 2 it stops while the guess is still wrong.

Selection · IF with AND

The login test: both parts or nothing

A login program runs: IF username = "sam" AND password = "fish42" THEN OUTPUT "Welcome". For each attempt, tick every column that is TRUE. Leave a box empty for FALSE. Fill the first two columns before you touch the last one.

Types sam and fish42
Types sam and fish24
Types tom and fish42
Types tom and cat99

Exam line: When you evaluate an AND condition, work out each part on its own first, then combine them.

Combining AND, OR and NOT

Why does this check never ask again?

This program should keep asking until the user types a mark from 0 to 100. But type 150 and it doesn't ask again: it just says the mark is saved. Which line is wrong?

A program with one bug — which line goes wrong?

Exam line: Test a combined condition with one value that should make it TRUE. If no value ever can, the parts are joined the wrong way.

Your turn

Write the conditions for a game

A game uses four variables: lives (a whole number), quit (TRUE or FALSE), score (a whole number) and day (a string such as "Sat"). (a) Write the first line of a WHILE loop that keeps the game running while the player has at least one life left and has not chosen to quit. (b) Write the first line of an IF statement that gives a bonus when score is 100 or more and it is the weekend (day is "Sat" or "Sun"). [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

Exam line: Write every comparison out in full, naming the variable each time. Each side of AND or OR must be a complete condition on its own.

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

1A AND B is TRUE only if A is TRUE and B is TRUE.
2One FALSE part makes the whole AND condition FALSE, whichever side it is on.
3WHILE X AND Y stops the first time X or Y is FALSE. It does not wait for both.
4A test that can never be TRUE, like mark < 0 AND mark > 100, needs OR instead of AND.
5NOT in front of a bracketed condition flips the whole bracket, e.g. NOT (mark >= 0 AND mark <= 100).

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?
Only when A is TRUE and B is TRUE. If either part is FALSE, the whole condition is FALSE.
WHILE X AND Y: when does the loop stop?
The first time X or Y is FALSE. It does not wait for both to be FALSE.
A loop controlled by AND has stopped. How do you explain why?
Find the part that was FALSE at the last check, and say which values made it FALSE.
Why is mark < 0 AND mark > 100 a bug?
No number is below 0 and above 100 at once, so it can never be TRUE. Use OR, or NOT (mark >= 0 AND mark <= 100).
What do brackets do in NOT (a AND b)?
They make NOT flip the result of the whole AND, not just a.
A shop is open 'Saturday and Sunday'. Which operator joins the two day tests?
OR: day = "Sat" OR day = "Sun". A single day can't be both, so AND would never be TRUE.

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 posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

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.