> For the complete documentation index, see [llms.txt](https://wenbo-notes.gitbook.io/cs1010-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wenbo-notes.gitbook.io/cs1010-notes/lec-tut-lab-exes/lecture/lec-04-conditionals.md).

# Lec 04 - Conditionals

Slides:

{% embed url="<https://nus-cs1010.github.io/2425-s1/slides/cs1010-lec04.html#1>" %}
Lecture Slides
{% endembed %}

## Conditional Statement

### Skipping `else`

We do not write an `else` after a `return` statement, since it is **redundant**. For example,

```c
if (<a>)
  return <x>;
if (<b>)
  return <y>;
```

## Logical Expression

### Logical Operators

In C, we cannot chain the comparison operators together. For example, the following is prohibitted.

```
1995 <= birth_year <= 2005
```

### Short-Circuiting

When evaluating the logical expressions that involve `&&` and `||`, C uses "short-circuiting". If the program already knows, for sure, that a logical expression is true or false, there is no need to continue the evaluation. The corresponding `true` or `false` value will be returned.

The two cases of shorting-circuiting used with logical operators:

1. `<a> || <b>`: If `<a>` is **true**, `<b>` won't be evaluated and the expression will return **true**. Otherwise, it will evaluate `<b>`.
2. `<a> && <b>`: If `<a>` is **false**, `<b>` won't be evaluated and the expression will return **false**. Otherwise, it will evaluate `<b>`.

Another reason to keep short-circuiting in mind is that the order of the logical expressions matter: we would want to put the logical expression that involves more work in the second half of the expression. Take the following example:

{% code lineNumbers="true" %}

```c
if (number < 100000 && is_prime(number)) {
    :
}
```

{% endcode %}

Checking whether a number is below 100,000 is easier than checking if a number is prime. So, we can skip checking for primality if the `number` is too big. Compare this to:

{% code lineNumbers="true" %}

```c
if (is_prime(number) && number < 100000) {
    :
}
```

{% endcode %}

Suppose `number` is a gigantic integer, then we would have spent lots of effort checking if `number` is a prime, only to find out that it is too big anyway!

{% hint style="info" %}
Short-Circuiting can also be used to deal with the **edge cases in array problem**! This is done by **checking the boundary first** then **check the content in the array**. So, let's say if you are already out of bound, you won't access the content and trigue the "out of bound" warning!
{% endhint %}

## Assertion

> An assertion is a **logical expression** that **must always be true** for the program to be correct.
