Skip to content

Commit

Permalink
Update L3_4_Conditionals_and_Loops.md
Browse files Browse the repository at this point in the history
  • Loading branch information
tiendu authored Oct 12, 2023
1 parent 971ebb8 commit f6f834e
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion basic_prog_for_bioinfo/L3_4_Conditionals_and_Loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,42 @@

3. **Comparison operators** such as `==` (equal), `!=` (not equal), `<` (less than), `>` (greater than), `<=` (less than or equal to), and `>=` (greater than or equal to) are often used in the boolean conditions.

* Python

```python
if x == y:
print("x and y are equal")
```

* R

```R
if (x == y) {
print("x and y are equal")
}
```

4. **Logical operators** such as `and`, `or`, and `not` in Python, and `&`, `|`, and `!` in R, can be used to combine or invert boolean conditions.

* Python

```python
if x > 0 and y > 0:
print("Both x and y are positive numbers")
```

* R

```R
if (x > 0 & y > 0) {
print("Both x and y are positive numbers")
}
```

5. **Membership operators** such as `in` keyword in Python and the `%in%` operator in R check for membership or containment within a collection such as a list, tuple, or array. In Python, `in` is not limited to collections, it can also be used to check for substrings within a string. On the other hand, in R, we have to use `grepl` to check for substrings.
6. **Membership operators** such as `in` keyword in Python and the `%in%` operator in R check for membership or containment within a collection such as a list, tuple, or array. In Python, `in` is not limited to collections, it can also be used to check for substrings within a string. On the other hand, in R, we have to use `grepl` to check for substrings.

* Python

```python
# Check if an element exists in a list
fruits = ["apple", "banana", "cherry"]
Expand All @@ -36,6 +58,8 @@
print("Substring found.")
```

* R

```r
fruits <- c("apple", "banana", "cherry")
if ("banana" %in% fruits) {
Expand All @@ -50,6 +74,25 @@
}
```

* Bash

```bash
fruits=("apple" "banana" "cherry")

contains() {
local n=$#
local value=${!n}
for ((i=1;i < $#;i++)) {
if [ "${!i}" == "${value}" ]; then
echo "Yes, '${value}' is in the fruits array."
return 0
fi
}
return 1
}

contains "${fruits[@]}" "banana"
```
7. Conditional statements can be *nested*, meaning you can have `if-else` statements within `if-else` statements to check for further conditions.

**Loops:**
Expand Down Expand Up @@ -118,6 +161,24 @@
# Output: 0, 1, 2, 3, 4, 6, 7, 8, 9
```
* Bash
```bash
for i in {0..9}
do
echo $i
done
```
```bash
i=0
while [ $i -lt 10 ]
do
echo $i
((i++)) # Increment i
done
```
8. Loops can also be *nested*, meaning you can have loops within loops. This is useful when working with multi-dimensional data structures like matrices or nested lists.
Remember that understanding the concepts of conditionals and loops is foundational to learning how to program, and they are used in almost every piece of code, especially in bioinformatics where dealing with large amounts of data is common.
Expand Down

0 comments on commit f6f834e

Please sign in to comment.