View on GitHub

LetsLearnProgramming

How we can program FRC robots in C++

Let’s Learn unConditionally!

<- Previously: Let’s Learn Functions!


Objectives: Learn about booleans and if/else statements.

Process:

Lets talk about the truth, namely logical truth. Which of the following statements are false?

Hopefully you answered that the second and third statements are false. In programming, we ask what is true a lot so that our code can make decisions. If we were asking if the above statements are true, we would write in our code like this:

We use these statements when we write if statements.

Look at this example.

We have if(x > 5) which is our if statement. It is followed by an open curly brace { with some code and a closed curly brace }. This is called the if body. The code in the if body only runs when the statement inside the if() is true.

What will our code print? What if we change the value of x to 3?

Now lets look at this code and run it. Change a and b so they are equal and run it.

Seems fishy, right? Lets fix it with an else block.

On a new line after the closed curly brace, lets add our else block:

else{
//your code here
}

Move the last printf statement so that we print it in the else block. Run your code. Change the values of a and b and run your code again. Does it work?

How does the else block work? When we have an if statement, the computer only runs the code inside the if block if the statement is true. If there is an else block attached to the end of the if statement, the computer will run the else block if the statement is false.

We can also make an else if statement which is like an else and an if statement combined together. Look at the following code:

if(temperature > 90)
{
    printf("Its really hot out!");
}
else if(temperature > 50)
{
    printf("Its nice outside!");
}
else
{
    printf("Its cold outside!");
}

What do you think it prints if the temperature is 99? What about 60? What about 32?

If a chain of if/if else/else blocks, only one block will run and they check in order. If the first if is true, then the computer doesn’t execute the other blocks in the chain. Additionally, we can’t have multiple else blocks unless they are else-if blocks.

This won’t work:

if(x > 1)
{
  //code
}
else
{
//code
}
else
{
//code
}

Neither will this:

if(x < 1)
{
  //code
}
else
{
//code
}
else if(x > 3)
{
//code
}

Lets Practice!

Change the values of foo and bar and see if you’re correct!

Now write your own code. Using the template, write a function that returns whichever number is larger and return 0 if the numbers are equal.


And Or

Katie has the final say on what music can play at the bunker, her rules are: If it is clean and it is on spotify, then she will add it to the bunker playlist.

How could we write that as code? We could do:

if(song is clean)
{
    if(song is on spotify)
    {
        song is added to playlist
    }
} 

A faster way to write it would be:

if(song is clean && song is on spotify)
{
    song is added to playlist
} 

The && means AND. x AND y only returns true if and only if x is true and y is true.

X Y X AND Y
0 0 0
0 1 0
1 0 0
1 1 1

FMJ has rules about when you have to wear camo: If it is a competition or it is an outreach event, you must wear your camo.

We write this as

if(competition || outreach)
{
    wear your orange camo
}

The || means OR. x OR y returns true is either x is true or y is true or both.

X Y X OR Y
0 0 0
0 1 1
1 0 1
1 1 1

Gatsby (the dog) has one rule about food: If the food is not stale, he will eat it.

We can represent that as follows:

if(!stale)
{
    gatsby eats the food
}

The ! is NOT. NOT x returns true when x is false.

X NOT X
0 1
1 0

Lets practice with AND, OR, and NOT with this code. Finish my code to correctly identify a super human and if they are a hero or villian.


As a group, let’s write a function that determines if the given year is a leap year with these rules: There is a leap year every year whose number is perfectly divisible by four - except for years which are both divisible by 100 and not divisible by 400. The second part of the rule affects century years. For example; the century years 1600 and 2000 are leap years, but the century years 1700, 1800, and 1900 are not.

Our function will print “[year] is a leap year” or “[year] is not a leap year”


Who gets into Stanford?

Everyone knows that there are only 3 criteria that are used to get into Stanford: GPA, FRC, and Family Tree. You can get into Stanford if you have at least a 3.95 GPA (on a 4.0 scale) but if you are an FRC student, you only need a 3.3. However, if you are related to Darth Vader - all bets are off and you cannot get in! Write a function that returns 1 if a student would get in and a 0 if the student would not get in. The function will take in three parameters: GPA, FRC student (1 or 0), and relation to Darth Vader (1 or 0).

Starter Code

Next: Let’s Loop de Loop! ->

Back to Main