mirror of
https://github.com/Brandon-Rozek/website.git
synced 2025-10-10 15:01:15 +00:00
Website snapshot
This commit is contained in:
parent
ee0ab66d73
commit
50ec3688a5
281 changed files with 21066 additions and 0 deletions
66
content/ta/spring2018/cpsc220/feb1.md
Normal file
66
content/ta/spring2018/cpsc220/feb1.md
Normal file
|
@ -0,0 +1,66 @@
|
|||
# Lecture for February 1st
|
||||
|
||||
## Control Structures
|
||||
|
||||
In this class we will talk about three types of control structures
|
||||
|
||||
- Sequential
|
||||
- Selection
|
||||
- Repetition
|
||||
|
||||
Sequential is what is most familiar to us. Write the lines from top to bottom and it executes it in that order
|
||||
|
||||
### Selection
|
||||
|
||||
Selection depends on the question of `if`.
|
||||
|
||||
If it is raining, wear boots
|
||||
|
||||
```java
|
||||
if (raining) {
|
||||
wearingBoots = true;
|
||||
}
|
||||
```
|
||||
|
||||
If you want something to happen also when it is not true, consider an `if-else` statement
|
||||
|
||||
If the light is off, turn it on.
|
||||
|
||||
Otherwise, turn it on
|
||||
|
||||
```java
|
||||
if (lightIsOn) {
|
||||
lightIsOn = false;
|
||||
} else {
|
||||
lightIsOn = true;
|
||||
}
|
||||
```
|
||||
|
||||
Sometimes you can have multiple branches depending on a condition. Let us take a stop light as an example
|
||||
|
||||
```java
|
||||
if (light == "red") {
|
||||
car.stop()
|
||||
} else if (light == "yellow") {
|
||||
car.slow()
|
||||
} else {
|
||||
car.go()
|
||||
}
|
||||
```
|
||||
|
||||
## String comparison
|
||||
|
||||
There is a specific method in the `String` class when it comes to checking for string equality
|
||||
|
||||
```java
|
||||
boolean equals(String s)
|
||||
```
|
||||
|
||||
Let us look at an example
|
||||
|
||||
```java
|
||||
String word = "hello";
|
||||
boolean ans = word.equals("hello"); // Returns true
|
||||
boolean ans2 = word.equals("Hello"); // Returns false
|
||||
```
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue