This document provides examples and explanations of different programming constructs used in Karel including for loops, while loops, if/else statements, and conditions Karel can check. It shows how to use a for loop to repeat an action a set number of times and a while loop to repeat an action until a condition is met. It also lists common conditions Karel can check like frontIsClear, beepersPresent, and facing directions and provides the opposite checks. Finally it demonstrates if and if/else statements and examples of their usage.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
38 views5 pages
Count I++) ( : For (Int I 0 I
This document provides examples and explanations of different programming constructs used in Karel including for loops, while loops, if/else statements, and conditions Karel can check. It shows how to use a for loop to repeat an action a set number of times and a while loop to repeat an action until a condition is met. It also lists common conditions Karel can check like frontIsClear, beepersPresent, and facing directions and provides the opposite checks. Finally it demonstrates if and if/else statements and examples of their usage.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5
for loop
for (int i = 0; i < count; i++) {
… }
private void turnRight() {
for (int i = 0; i < 3; i++) { turnLeft(); } } while loop
while ( condition ) { … }
private void moveToWall() {
while (frontIsClear()) { move(); } } Conditions Karel can check for Test Opposite What it checks frontIsClear() frontIsBlocked() Is there a wall in front of Karel? leftIsClear() leftIsBlocked() Is there a wall to Karel’s left? rightIsClear() rightIsBlocked() Is there a wall to Karel’s right? beepersPresent() noBeepersPresent() Are there beepers on this corner? beepersInBag() noBeepersInBag() Any there beepers in Karel’s bag? facingNorth() notFacingNorth() Is Karel facing north? facingEast() notFacingEast() Is Karel facing east?
facingSouth() notFacingSouth() Is Karel facing south?
facingWest() notFacingWest() Is Karel facing west?
This is Table 1 on page 18 of Karel course reader
if statement
if ( condition ) { … }
private void safePickUp() {
if (beepersPresent()) { pickBeeper(); } } if-else statement
if ( condition ) { … } else { … }
if (frontIsClear()) { move(); } else { turnLeft(); }