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.

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 when the user types 150, 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 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

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 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 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.