CS4ALL Computer Science For All Of Us
  • Home
  • Community
  • Workshop
    • Workshop Info
    • Schedule
    • Location
    • Sponsors
  • Resources
    • Scratch Resources
CS4ALL
  • Home
  • Community
  • Workshop
    • Workshop Info
    • Schedule
    • Location
    • Sponsors
  • Resources
    • Scratch Resources
  • Home
  • Processing
  • Beginner
  • If Statements

If Statements

July 9, 2016 Leave a Comment Written by Keith Atkinson

What if you wanted to award points to a player in a game based on how many gold tokens they collected? If they get more than 3 tokens, they will get 5 extra points, but if they get less than that, their score will not change. An easy way to program this is to use an if statement.

If Statement
The if statement tells your program to execute a certain section of code only if a particular condition is true. A condition must be a
boolean
statement, which means that it must evaluate to either true or false.

Note: You may have noticed that there are no semicolons at the end of an if statement. You should not put a semicolon after a closing curly bracket ( } )

Relational Operators In order to construct a boolean statement, you must compare two values with a relational operator. A relational operator determines if one value is equal to, not equal to, greater than, less than, greater than or equal to, or less than or equal to the other value.

Operator Meaning Example
== is equal to a == b
!= is not equal to a != b
< less than a < b
> greater than a > b
<= less than or equal to a <= b
> greater than or equal to a >= b

The Double Equal Sign When you compare two values to see if they are equal, you must use a double equal sign ( == ). In Processing, a single equal sign is used to assign a value to a variable and two equal signs is used as a test for equality

It is easy get the two confused (even experienced programmers make this mistake all the time), but just re- member that whenever you are comparing two things to see if they are equal, to use a double equal sign.

From the Moving Circle tutorial the circle moved across the screen and then disappeared forever. Now we can make a circle that will scroll continuously across the screen. The circle will appear on the left side of the screen and then move towards the right side of the screen. When it disappears off the right side of the screen, it will then reappear on the left side of the screen.

int xPos = 0;<br>
void setup() {<br>
size(500, 300);<br>
}<br>
void draw() {<br>
background(255, 255, 255);<br>
fill(39, 58, 150);<br>
// Draw the ellipse<br>
ellipse(xPos, 150, 100, 100);<br>
//Shift the circle over by one pixel<br>
xPos += 1;<br>
/* If the circle moves off the right edge of the<br>
screen (the screen has a width of 500), then reset<br>
the x coordinate of the ellipse to zero */<br>
if (xPos > 500) {<br>
 xPos = 0;<br>
}<br>
}<br>
Beginner, Processing
Moving Shapes
Bouncing Circle

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *