SEC-S20W5 Iteration in Programming: For, While, and Do-While Loops
Hello Everyone
I'm AhsanSharif From Pakistan
Greetings you all, hope you all are well and enjoying a happy moment of life with steem. I'm also good Alhamdulillah. |
---|
Design On Canva
Why are loops in programming? Which of the types of cycles did you like better? |
---|
Loops/Cycles
The main purpose of the cycle used in programming which we also know as loops is to run our block of code over and over again. This allows our code to run repeatedly. Why this is important and why it is beneficial is as follows.
Why Necessary In Programming?
Loops give us an advantage in our code where we need to write code repeatedly. It eliminates the need to write the same batch of code and run it over and over again. This is especially useful in places where we need iteration like iterating over arrays or aggregations.
This makes our code efficient because we only write the code once so it doesn't have to be written over and over again. It creates a clearance in the code and shortens the code which makes it easier for us to understand.
Loops provide us with a flexible environment that allows us to create interactive programs. We can use loops until a specific condition is met. Like getting input from any user and passing it to the end of the file. Loops enable dynamic behavior based on conditions.
Many algorithms inherently require repetitive operations such as sorting an object or finding someone from an object. Loops therefore facilitate the implementation of these algorithms by giving us an easy way to organize complex logic.
When we have to process a large amount of data, we have to resort to loops which are very important for this work. Like reading records from a database etc. And so is the collection of the data set.
Types
There are three types of loops that we generally use.
For Loop:
We use this loop when we know the number of iterations in advance, for example, to iterate through an array.
While Loop:
We use it when we don't know the number of iterations and it fires until a certain condition is met.
Do-While Loop:
This is almost the same as a while loop, but it guarantees that the body of the loop must be executed at least once.
My Favorite One?
If I were to make a personal recommendation, I would say that I like the For Loop, which looks attractive.
This loop gives us clarity that we can understand at a glance. When we look at this, we get a clear understanding of how many times a loop will run and how the loop variable changes with each iteration.
The purpose of using this loop is that it shortens your code and allows you to specify the increment and decrement in a single line. Loops control variable conditions.
This loop is versatile and we use it for many tasks. Such as from iterating through arrays to running simulations. That's why this loop is my favorite loop and I use it.
Example Of For Loop:
Input Code
Output
Explain both conditions in words. Which is better and why? |
---|
First Condition:
if(n%2==0 && m%2==0 || n%2==1 && m%2==1)
First Condition We have two statements in the given statement. The first statement is that if the two buildings N and M have the same number, they are on the same side of the street.
In the second condition, it will check that the buildings n
and m
are odd-numbered, which means they are on the same side of the street.
Logical OR is used to check these two conditions. And by this, he will check whether the two buildings are even or both are of odd numbers which in both cases are on the same side.
Second Condition:
if(n%2 == m%2)
The second condition is that it checks that if we divide both the numbers we named n
and m
by two, the remainder is the same.
if(n%2 == m%2)
are equal in both buildings it means that from here both are equal or odd i.e. they are both on the same side of the road.
Which is better and why?
In my view, the second condition is better because it expresses simplicity. The expression simplicity means that it directly expresses the logic that two numbers are on the same side of the road either it is even or it is odd.
This condition effectively compares one instead of two, which makes it slightly more efficient.
This condition makes it easy to read because the code is clear and easy to read so everyone can understand it in a good way which reduces the chances of errors.
Output 33 times the number 33 with each kind of loop. |
---|
Here I am describing three types of programs which all have the same purpose. They have to make 33 numbers show 33-time outputs. For this, I will use for loop while loop the do-while loop as below.
Using For Loop:
Using While Loop:
Using Do-While Loop:
Output the numbers from 11 to 111: 11, 21,31....101, 111 - but do not output the number 51 (one type of cycle) |
---|
In this exercise, I am going to create a program that I have developed with a do-while loop. And our main goal here is that we have to show the output from 11 to 111. But we have to take care that we don't make 51 shows. For everyone else we have to put on a show, the program is below.
Using Do-While Loop:
Input Code:
Output:
Output the multiplication table by a random number |
---|
As we have been shown the table of zero as an example, we have to create a table of our own. But I'm going to show a table of eight in my program that I made with a for-loop.
Input Code:
Output:
Output the squares of the numbers. 1,,4,9,16,25,36,49...400 |
---|
Here we are expected to write a program in which we have to find the square from 1 to 400 and show the output. For as the square of one is one, the square of two is four, and the square of three is nine, then the square of four is 16. Thus we have gone to 400 as the square of 20 will be 400. We have to show our output till there so the program for that I have created below.
Input Code:
Output:
Derive powers of some number: 1 2 4 8 16 32 64 128... less than 3000 |
---|
Here I have created a program using a while loop in which we have to write the sum of numbers that are less than three thousand.
Input Code:
Output:
How many even four-digit numbers are not divisible by 3 |
---|
First of all, in this program, we will check what our four-digit numbers are. These numbers run from one thousand to 9999. Between them now we have to find those numbers which are not divisible by three. And also we will check that is divisible by three.
So between these numbers, we have a total of 4500 numbers which are divisible by three and not divisible by three. We have to separate them of which 1500 is a number that is divisible by three. And among them, three thousand is a number that is not divisible by three.
For its implementation, I have made a program through which it will tell us the separate numbers so the program is below.
Input Code:
Output:
Thank you so much for staying here. I do my best to perform this task. I hope you all guys like it. I invite my friends @kouba01, @malikusman1, and @suboohi to join this challenging task.
Cc:
@sergeyk
X:
https://x.com/AhsanGu58401302/status/1845376299247993154
Congratulations on bringing a quality content. You have earned a positive vote from team 2, and it is delivered by @ashkhan.
Many Blessings...🙏🏻
Thank you so much dear @ashkhan for your kind support and love.