Introduction to if, else, for and while loop

Introduction to if, else, for and while loop

Introduction to if, else, for and while loop

Control flow statements are an essential part of programming, as they allow you to control the flow of execution of your program. They let you execute certain statements based on conditions, and repeat statements multiple times.

image

You can watch the video tutorial of this blog here:

If-else statements

The if-else statement is used to execute a block of code if a certain condition is true, and a different block of code if the condition is false. Here's an example in C++:

#include <iostream>
using namespace std;

int main() {
    int x = 10;
    if (x > 5) {
        cout << "x is greater than 5";
    } else {
        cout << "x is less than or equal to 5";
    }
    return 0;
}

In this example, we define a variable x with a value of 10. We then use an if-else statement to check if x is greater than 5. If it is, we print "x is greater than 5". If it's not, we print "x is less than or equal to 5". The output of this program will be "x is greater than 5".

Here's the same example in Python:

x = 10
if x > 5:
    print("x is greater than 5")
else:
    print("x is less than or equal to 5")

In Python, we don't need to include any libraries like in C++. We simply define the variable x with a value of 10, and use an if-else statement to check if it's greater than 5. If it is, we print "x is greater than 5". If it's not, we print "x is less than or equal to 5". The output of this program will be the same as in C++.

For loops

The for loop is used to execute a block of code a certain number of times. Here's an example in C++:

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        cout << i << endl;
    }
    return 0;
}

In this example, we use a for loop to print the numbers 1 to 5. We define a variable i with an initial value of 1, and use the condition i <= 5 to specify that we want to execute the loop as long as i is less than or equal to 5. We then use the statement i++ to increment i by 1 after each iteration of the loop. Inside the loop, we print the value of i using the cout statement. The output of this program will be:

1
2
3
4
5

image

Here's the same example in Python:

for i in range(1, 6):
    print(i)

In Python, we use the range() function to specify the range of values we want to iterate over. We use the for keyword to start the loop, and then define a variable i to represent the current value of the loop. Inside the loop, we use the print() function to print the value of i. The output of this program will be the same as in C++.

While loops

#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i <= 5) {
        cout << i << endl;
        i++;
    }
    return 0;
}

In this example, we use a while loop to print the numbers 1 to 5. We define a variable i with an initial value of 1, and use the condition i <= 5 to specify that we want to execute the loop as long as i is less than or equal to 5. Inside the loop, we print the value of i using the cout statement, and then use the i++ statement to increment i by 1. The output of this program will be the same as the for loop example above.

Here's the same example in Python:

i = 1
while i <= 5:
    print(i)
    i += 1

In Python, we use the while keyword to start the loop, and then define a variable i to represent the current value of the loop. Inside the loop, we use the print() function to print the value of i, and then use the i += 1 statement to increment i by 1. The output of this program will be the same as in C++.

conclusion

In conclusion, learning about control flow statements is essential when starting out in programming. With these statements, we can make our programs more powerful and efficient, allowing us to control the flow of the program and execute certain code based on specific conditions. As the leader of this coding club, I understand that learning to code can be challenging, which is why I hope this tutorial has been helpful to you all.

To deepen your understanding of control flow statements, I have provided some YouTube video links that can help you. These videos are great resources to learn more about control flow statements in Python and C++, and can assist you in your programming journey.

Here are the links for Python:

Here are the links for C++:

Remember, practice makes perfect. Keep coding, and don't be afraid to make mistakes. The more you code, the more you will learn and improve. Thank you for attending today's class, and I look forward to seeing you all again soon.

Did you find this article valuable?

Support Mojtaba Maleki by becoming a sponsor. Any amount is appreciated!