Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
Delaying the else¶
We don’t recommend that you require students to use
else right away. It actually makes code much harder for students to read. Using an
else hides away the condition when the
else block would execute. Beginning students are having enough trouble just reading the code and making sense of it. Using an
else hides away clues to how the program is working. Studies show that making the test explicit makes it ten times easier for beginning students to read programs with
To do a something like an
else using two
if statements, use a not.
Sneak up on and and or¶
Students have a difficult time with logical expressions. It’s probably just a matter of less experience with them. They do a lot with arithmetic expressions, but expressions whose value is true or false are much less common in mathematics.
In particular, expressions with multiple logical expressions, combined with
or, are challenging for students. There is some research that shows that the more logical expressions in the program, the more difficult (in terms of time to understand it, ability to read or modify the program) it is for students.
Don’t ask students to do much with
or to start. Give them a lot of experience with simple logical expressions, before requiring them to use
Teacher Note: Confusing while and if¶
while loop and an
if block look almost the same. They each have a logical expression, and a block of instructions underneath them.
Students often get them confused. Some key distinctions to make to students:
ifblock executes only once. It does the test, then executes the instructions indented underneath once.
whileblock executes repeatedly as long as the expression is true. The body of the loop can be executed many times.
When you show students examples of
while loops, make sure that you choose examples where the body of the loop gets executed several times, to show the difference between
while loops and an
if block. When you trace the program, trace more than one iteration through the body of the loop, to emphasize the multiple times.