© Fort Street High School Robotics
In Engineering, we use a common pattern called "input-process-output" that exists across many fields in both software and hardware. In Robotics, we use it as a model the work your code will do when interfacing with the environment.
For robots:
For solving problems in C:
scanf
if
printf
scanf
functionIn Section 3, we were able to print out that we had 10 chickens.
#include <stdio.h>
int main () {
int numberOfChickens = 10;
printf("I have %d chickens.\n", numberOfChickens);
return 0;
}
What if we wanted the user to input that number instead? We can do exactly that with scanf
:
#include <stdio.h>
int main () {
int numberOfChickens;
printf("Enter the number of chickens: "); // ask for the number of chickens
scanf("%d", &numberOfChickens); // record the user's input into numberOfChickens
printf("I have %d chickens.\n", numberOfChickens);
return 0;
}
Try and run this program. When you run it, it will wait for you to input some text. When you type a number and hit enter, the program will continue. In this case:
Enter the number of chickens: 6
I have 6 chickens.
We use if
statements to make decisions. Decisions usually have the following structure:
The best way to illustrate this is by example.
// Check if a person is 18 and above, or not.
#include <stdio.h>
int main () {
int age;
printf("Enter an age: "); // ask for the number of chickens
scanf("%d", &age); // record the user's input into numberOfChickens
if (age >= 18) {
printf("The person is 18 and above.\n");
}
return 0;
}
Code | Comment |
---|---|
if ( condition ) { ... } |
The code between the curly brackets will only run if the condition is true. |
age >= 18 |
A condition. There are many more symbols to compare two numbers. |
We can add more to this by using else
.
if (age >= 18) {
printf("The person is 18 and above.\n");
}
else {
printf("The person is under 18.\n");
}
The program will now choose two paths - the code in the first { ... }
will run if age >= 18
, otherwise it will run the code in the second { ... }
.
Sometimes we want to add even more conditions. We can do this by using else if
.
if (age >= 18) {
printf("The person is 18 and above.\n");
}
else if (age < 7) {
printf("The person is very young.\n");
}
else {
printf("The person is under 18.\n");
}
Things to note:
true
statement. So, this code will only print one line.(age >= 18) == false
, then (age < 7) == false
before running the else
code.Try and figure out what this will do before running it. Note how the indentation is there to help you!
if (age >= 18) {
printf("The person is 18 and above.\n");
}
else {
if (age < 7) {
printf("The person is very young.\n");
}
printf("The person is under 18.\n");
}
There are many more conditions we can use. These are called relational operators, much like the arithmetic operators we met in the previous topic.
Operator | Comment | Example |
---|---|---|
== |
Equals. Very different from = . |
5 == 10 is false. |
!= |
Not equals | 5 != 10 is true. |
< |
Less than, checks if left is less than right. | 5 < 10 is true. |
> |
Greater than, checks if left is greater than right. | 5 > 10 is false. |
<= |
Less than or equal to. | 5 <= 10 is true. |
>= |
Greater than or equal to. | 5 >= 10 is false. |
You can string together multiple conditions in one condition.
if (age >= 18 && age <= 65) {
printf("The person between 18 and 65\n");
}
else if (age == 13 || age == 14) {
printf("The person is 13 or 14\n");
}
else if (!(age == 16)) {
printf("The person is not 16\n");
}
else {
printf("The person is 16\n");
}
Operator | Comment | Example |
---|---|---|
&& |
AND | If two conditions are true. |
|| |
OR | If one of two conditions are true. |
! |
NOT | Inverts the condition. |
Be careful about joining too many. You need to place parentheses to avoid confusion.
* (true || false) && false
is true. Calculate (true || false)
first.
* true || (false && false)
is also true. Calculate (false && true)
first.