Skip to content

Commit

Permalink
Added info the conditional statements and loops. (#37)
Browse files Browse the repository at this point in the history
* Updated the Statements and Functions folder

* Update readme.md
  • Loading branch information
kusumita29 committed Oct 2, 2022
1 parent 168cd49 commit ffac154
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 1 deletion.
188 changes: 188 additions & 0 deletions Statements and Functions/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# Conditional Statements

The conditional statements are a very fundamental part of any program as it helps to change the flow of control (which is generally top to bottom) depending on a given condition.

Let us consider an example where we print whether a given number is Odd or Even.

Code snippet:
```python
if number % 2 == 0:
print ("Number is even")
else:
print ("Number is odd")
```

Now, if the entered number is 5, the if statement will check if the remainder of 5 divided by 2 is 0 or not. As it is not 0, it skips the lines of code within the if statement and the flow of control is shifted to the else part and the lines of code written under it is executed.
<br>

------------


**There are two types of conditional statements**:
## 1. if - else if - else
**Here is a code that prints the grade based on the marks.**


* In C++:
```cpp
if (marks >= 90) {
cout << "A";
}
else if (marks >= 80) {
cout << "B";
}
else if (marks >= 70) {
cout << "C";
}
else if (marks >= 40) {
cout << "D";
}
else {
cout << "F";
}
```
**In Python,** **`else if` is written as `elif`.**


* In Python:
```python
if marks >= 90:
print ("A")
elif marks >= 80:
print ("B")
elif marks >= 70:
print ("C")
elif marks >= 40:
print ("D")
else:
print ("F")
```



## 2. switch case statements

In switch case, we take a value from user which is generally integer or of a character data type based on which the corresponding case is executed.

It is used in-place of if statement if the decision making branches are independent of each other.

*Let us assume that a player is picking a colour for playing Ludo, and based on what he enters, i.e., R, B, Y and G, he is given Red, Blue, Yellow and Green respectively. In case, they enter any other alpahabet, the code returns an error message as no colour is associated with that alphabet.*

* A code in C++ to implement the same:

```cpp
switch (alphabet) {
case 'R':
cout << "Red";
break;
case 'B':
cout << "Blue";
break;
case 'Y':
cout << "Yellow";
break;
case 'G':
cout << "Green";
break;
default:
cout << "No other colour exists";
}
```
* *Note: Python does not have switch case statements.*
------------
# Loops
Loops are used to execute a block of code as many times we want without writing the same code over and over again. The flow of control breaks out from the loop when the provided condition is false or a `break` statement is used.
Let us take an example where we print all the even numbers upto 10.
#### **There are two types of loop statements**:
## 1. for statement
**Let us take an example where we print all the even numbers upto 10.**
* In C++:
```cpp
for (int i = 2; i <= 10; i+=2) {
cout << i << "\n";
}
```
`int i = 2` initializes the value of i to 2

and `i += 2` keeps on incrementing the value of i by 2

until ` i <= 10 ` becomes `false`

<br>
* In Python:
```python
for i in range(2, 11, 2):
print (i)
```

***Output:
2
4
6
8
10**

The range function takes in three parameters - the first paramter is the **starting value**, the second parameter is the **ending value + 1**, and the third paramter is the **increment value**.

Here,` range(2, 11, 2)` tells the program that it should run the loop for all the values of i between 2 to (11-1) and keep incrementing the value of i by 2.



## 2. while / do while statements

A while statement always checks if the condition provided is True or False at the beginning of the loop and in case it is True, the block of code within the loop is executed. Otherwise, the loop is terminated.


* A code in C++ to implement the same:
```cpp
int i = 2;
while (i <= 10) {
cout << i << "\n";
i += 2;
}
```


* In Python:
```python
i = 2
while i <= 10:
print (i)
i += 2
```

In the above codes, the while loop runs until the value of i is 10.

In a do while loop, the validity of the condition is checked at the end of loop. Hence, the code runs atleast once even if the provided condition is False.

If I modify the above code to a do while loop, it will print even numbers upto 12 as it checks the condition after the loop is executed.

Code snippet for do-while:

```cpp
int i = 2;
do {
cout << i << "\n";
i += 2;
}while (i <= 10);
```

**Output:
2
4
6
8
10
12**


* *Note: In Python, we do not have do-while loops.*
------------
22 changes: 21 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,33 @@ Non-primitive data types are also known as reference data types. They are create
- [Function](Data%20Types/readme.md#function)
- [Class](Data%20Types/readme.md#class)

## [Statements and Functions](Not-Added)
## [Statements and Functions](Functions%20and%20Statements/readme.md)
In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out. A program written in such a language is formed by a sequence of one or more statements. A statement may have internal components (e.g., expressions).

A function is a block of statements that performs a specific task. Functions accept data, process it, and return a result. Functions are written primarily to support the concept of reusability. Once a function is written, it can be called easily, without having to write the same code again and again.

Different functional languages use different syntax to write a function.

<hr>

There are two main types of statements in any programming language that are necessary to build the logic of a code.
1. [Conditional-Statements](Statements%20and%20Functions/readme.md#conditional-statements)

There are two types of conditional statements mainly:
- if else
- switch case

<br>

2. [Loops](Statements%20and%20Functions/readme.md#loops)

There are three types of conditional statements mainly:
- for loop
- while loop
- do - while loop (a variation of while loop)

<hr>

## [Data Structures](Data%20Structures/readme.md)
In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.

Expand Down

0 comments on commit ffac154

Please sign in to comment.