SEC-S20W5 | Iteration in Programming: For, While, and Do-While Loops

in #sec20w5sergeyklast month

Assalam-o_Alaikum!!!

Greetings to all my Steemit members. Hopefully, you all are fine and enjoying your best life on STEEMIT. I am also fine ALHAMDULILLAH. And today I am going to take part in this engagement contest. I will try my best to complete all tasks



Design In Canva

1. Why are loops necessary in programming? Which type of loop did you like the most?

Loops are necessary because they allow programmers to execute a blog many times without rewriting a block of code, automate repetitive tasks, process data collection, and implement algorithms efficiently. The main reason why useful loops are necessary.is always

1. Automate action:
the loop allows repeating tasks without requiring entry. For example, I will print a message a hundred times, but I'll not te the print statement a hundred times. I can use a loop to automate the looping code block until a specification is met or to perform repetitive operations.

2.Efficiency:

Loops reduce redundancy and make code more efficient. Be patient when you have a list of thousands of nummus need to perform the same operation on each number. Without loops, you won't write the operation for each number manually. Which is time-consuming and error-prone to allow coding loops up

3.Scalability:
Loops handle large data sets that would be impractical to manage manually. When working with large data sets, such as thousands or even millions, it is not possible to manually write operations for each piece of data. Allows your program to scale as the data is large, whether you have 10 items or 10 million loops handles them efficiently.

4.Flexibility:
Loops and data here accept different amounts of input enabling your program to work with dynamic scenarios, fo, r example if the size of the data set is not known at the time of coding, a loop can handle any size. From a small list to a very large one, loops can adjust their behavior ac. accordingly

Types of Loops:

  • For Loop

A for loop is a control flow statement that allows you to execute a log of code a specified number of times. It is typically used when the number of iterations is known in advance. Runs the code block for each item in the array

  • While Loop

While Loop repeatedly executes a block of code until a specified condition is true. It checks the condition before executing the code inside the loop. This type of loop is useful when you want to repeat The number may not be known in advance but this particular condition needs to be continued until fulfilled.

  • Do While Loop:

.

This code is just like a while loop but the difference isexecute executes a block of code. The condition is that it checks whether it is true. or false

  • Favorite Loop:

I find for-loops particularly beautiful because they provide a clear structure when declaring a known range or list of elements that will be in different languages.

2⳾ Explain both conditions in words. Which is better and why?
  • ConditIf n 1:

If (n%2 == m%2) check if the remainders when dividing n and m by 2 are the same. This means both numbers are either both even or both odd, which indicates they are on the same side of the street.

  • Condition 2:

if(n%2==0 && m%2==0 || n%2==1 && m%2==1) explicitly checks whether both n and m are even, or both are odd. This is a longer way of expressing what the first condition does.

  • Which is better:

The first condition (n%2 == m%2) is better because it is shorter, clearer, and more efficient.

Question : 3. Output 33 times the number 33 with each kind of loop.

So Now I will share a Program in which I will show you the output 33 times with each kind of loop. So first of all we will show the program with with output output.

First of all, I will share the example of For Loop. Ind thIn i will share one by one other

  • For Loop
image.pngimage.png
  • While Loop
  • Do While Loop:
image.pngimage.png
4⳾ 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, the question I will show you the program output which will show the numbers from 1 to 111, 11,21,31,.....101, 111, But it will not show the number 51

image.png

So the Output is:

image.png

5⳾. Output the multiplication table by a random number, for example by 0:


1 x 0 = 0
2 x 0 = 0
3 x 0 =0
....
10 x 0 = 0
to show the multiplication of a tabnumbers10 number, I will show output the t out of the table of 10. Because I will enter the random number 10

image.png

So the Output is

image.png

6⳾ Output the squares of the numbers. 1,,4,9,16,25,36,49...400

In this, the program I will show output out the put with the program, where squares of the numbers 1,4,9,16,25,36,49, ....400

image.png

So The Output is
image.png

7⳾ Derive powers of some number: 1 2 4 8 16 32 64 128... less than 3000.

In this program, I will derive powers of some number 1,2,4,8,16,32,64,128,....less than 3000

image.png

So the Output is

image.png

8⳾ How many even four-digit numbers are not divisible by 3

First, find the range of even four-digit numbers:

The smallest is 1000 (but not divisible by 2, so we start at 1002).

The largest is 9998.

Step-by-step solution:

  1. Total even four-digit numbers: From 1002 to 9998, the difference is (9998 - 1002)/2 + 1 = 4500 even numbers.
  1. Find how many of these are divisible by 3: An even number divisible by 3 must be divisible by 6. The first such number is 1002, and the last is 9996. There are 1500 such numbers.
  1. Subtract the count of numbers divisible by 6 from the total: 4500 - 1500 = 3000 even four-digit numbers not divisible by 3.

So finally I completed all the tasks. And I tried my best to do this. I hope you will like it and appreciate.

it
Special thanks to @sergeyk

Invite Friends: @ruthjoe , @abdullahw2 , @josepha

Sort:  
Loading...

Congratulations, your post has been upvoted by @scilwa, which is a curating account for @R2cornell's Discord Community. We can also be found on our hive community & peakd as well as on my Discord Server

Manually curated by @ abiga554
r2cornell_curation_banner.png

Felicitaciones, su publication ha sido votado por @scilwa. También puedo ser encontrado en nuestra comunidad de colmena y Peakd así como en mi servidor de discordia