terminal

codeando_simple

terminal

menu

terminal

search_module

guest@codeandosimple: ~/system/search $ grep -r "" .

Press [ENTER] to execute search

Status

Engine: Ready

Database: Online

Index: V2.1.0_LATEST

bash -- cat logica-estructuras-ciclicas.md
guest@codeandosimple: ~/blog/logic $ cat cyclic-structures.md

Logic - Cyclic Structures_

// "80% of success is just showing up" - Woody Allen

In the world of software development, Cyclic or Repetitive Structures are the pillar of efficiency. We will learn to master loops, cycles, and repetitions, which are executed through fundamental actions: FOR - WHILE.

Video thumbnail

# What are Cyclic Structures?

In this article we will see cyclic structures, which through conditions allow repeating the execution of a set of actions automatically.

This is something that isn't always so clear and seems difficult, but it's used extensively in software engineering! When we see examples you will understand it and realize its importance for writing clean and efficient code.

# Logic and Diagrams of Cyclic Structures

The logical syntax is written as follows to ensure correct flow control:

Basic architecture of a repetitive cycle

We have a block with an iterative instruction (loop, cycle) that has a logical condition that determines execution:

Evaluation of the entry condition

If the condition is met, the block is entered and all instructions within the block are executed. When the last instruction of the block is executed, the processor re-evaluates the condition, and if it is met, the cycle repeats.

The loop is exited when the condition is not met; in that case, it continues with the line below the block, releasing the program flow.

Cycle break and flow exit

# And what is the purpose of repeating instructions?

Let's suppose we have a program that asks us to print numbers from 1 to 3. In traditional development, we could write each line manually:

Inefficient code without loops

With 3 print lines we solve it, the text "number" concatenated (added as a word) to the number (1, 2, 3).

What if they ask to add 4 and 5? We add the manual printing of number 4 and number 5.

Manual code redundancy

But if they ask for 100 or 10,000, we won't write it 100 times! For repetitive situations and processing massive data we have loops. Let's see two types of high-performance cyclic structures: FOR and WHILE.

# The FOR Cycle in Detail

The FOR cycle iterates over a defined sequence, ideal for when we know in advance how many repetitions we have to do.

FOR cycle syntax

This cycle will iterate from the initialValue to the finalValue, executing instructions A, B, and C in each step. The counterVariable will have one added automatically in each iteration. Once it exceeds the finalValue, it exits the cycle.

If we have to print from 1 to 100, how do we do it? The first number to print is 1 and the last is 100; the cycle has to go through all those numbers sequentially.

So we define our parameters:

  • initial value = 1
  • final value = 100

Then, the logic of the algorithm becomes:

Optimized implementation with FOR

For number 1 to 100 print number end of cycle. In just three lines of professional code we solved the problem!

# The WHILE Cycle: Control by Conditions

On the other hand we have another very powerful cyclic structure, the WHILE which executes while a logical condition of state is met.

WHILE cycle logic

The condition is evaluated; if it's true, the block is entered and instructions A, B, and C are executed. After executing instruction C, the condition is re-evaluated; if it's true, it enters the block again, and repeats until the condition is false, in which case it continues with the next line of code.

While cycle flowchart

info Important for Programmers

Inside the block I must make sure to change the parameters of the condition, so that at some point it exits the cycle.

Let's notice something: the cycle might never execute if the first time it evaluates the condition it is false, it never enters the block.

For example, for the case of printing numbers from 1 to 100, the condition could be that the number is less than or equal to 100. Now, where does number start? It has to be initialized.

Counter increment in Java and Python

When number is 100, we increment number, and it will be 101, so when re-evaluating it will not meet the condition and exits the cycle safely.

# Practice Workshop: Exercise 1 (Data Counting)

Data analysis with loops

We are going to have 20 people, a fixed number, which in programming can be defined as a constant.

For each of these 20, we must ask for the age; then a variable will be age (an integer number).

Data filtering algorithm
  • We declare the constant PEOPLE that is 20, and variables age, amountOfAdults, and counter.
  • We initialize the variable amountOfAdults: at the start there are zero adults, we initialize it to zero.
  • We set up a FOR cycle that goes from 1 to PEOPLE, managing the 20 ages.

# Practice Workshop: Exercise 2 (Input Verification)

Character validation logic

The algorithm will read characters until the entered character is a vowel. This indicates that we will need a variable; we can call it letter of type character (char).

Flowchart with data input
  • Variables: letter of type character, counter integer.
  • We load the counter at zero, because no letter has been entered yet, and we read the first entry.
  • We start the cycle: if the condition is met, that is if letter is not a vowel, we increment the counter and read letter again.

# Practice Exercises

Here are some solved exercises for you to test your knowledge of algorithmic logic:

In the next article, we will see functions, something that allows us to divide a complex problem into simple problems, easy to solve.

Resources used