Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Switch statement in Day-5 #560

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/day-05/arithmetic-Operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,22 @@ int result = 10 % 3; // result will be 1

```
These operators can be used with variables, constants, or expressions. They follow the usual rules of precedence and associativity. Additionally, parentheses can be used to enforce a specific order of evaluation.

## Precedence and Associativity of Arithmetic Operators

### Precedence

Precedence determines the order in which different operators are applied in an expression. In C++, the arithmetic operators are grouped into two precedence levels:

- High Precedence:
- Multiplication(*)
- Division(/)
- Modulus(%)
- Low Precedence:
- Additio(+)
- Subtraction(-)

### Associativity

The associativity of arithmetic operators in C++ is left-to-right,i.e, when two operators of the same precedence appear in an expression, they are evaluated from left to right.

57 changes: 57 additions & 0 deletions docs/day-05/if-else-statement.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,60 @@ int main(){
```
NOTE:
> In the above code "?" refers to if-statement and statement after the ":" is executed if the if-statement is `false`.

## 8. Switch Statement
The `switch` statement in C++ is a control statement that allows a variable to be tested against a list of values, each with a corresponding block of code. It is a better alternative to a series of `if-else` statements, specially when dealing with multiple conditions based on the value of a single variable.

### Syntax of the Switch Statement
The basic syntax of a `switch` statement in C++ is as follows:

```cpp
switch (expression) {
case constant1:
// Code to be executed if expression == constant1
break;
case constant2:
// Code to be executed if expression == constant2
break;
// More cases can be added if required
default:
// Code to be executed if expression does not match any case
}
```

### Example
```cpp
#include <iostream>

int main() {
int day = 3;

switch (day) {
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std::cout << "Tuesday" << std::endl;
break;
case 3:
std::cout << "Wednesday" << std::endl;
break;
case 4:
std::cout << "Thursday" << std::endl;
break;
case 5:
std::cout << "Friday" << std::endl;
break;
case 6:
std::cout << "Saturday" << std::endl;
break;
case 7:
std::cout << "Sunday" << std::endl;
break;
default:
std::cout << "Invalid day" << std::endl;
}

return 0;
}
```