For without the use of key

Asked

Viewed 493 times

-1

#include<stdio.h>

int main ( void ){
    int row;
    int column;
    for ( row = 1; row <= 7; row++ ){
        for (column = 1; column <= row; column++ )
            printf("*");
            printf("\n");
} printf("\n");
}

Saída de dados

I want to know if the loop for can be used without { after your argument?

Is it possible to translate this code for me? ' loop coming out of the first for, verifying the condition of the second and giving print after that it will add in column and return to add in row also and staying row = 2 and column = 2, this way will give one more print.

The problem is that if so, this second loop would no longer agree with the figure since the printf is only triggered when column <= row.

How this piece of code works?

The program starts and releases * as output because column <= row = TRUE ( both are 1), then 1 is added to the column, making column < = row = FALSE, the program leaves the loop internal and skips a line, adding 1 to Row and making again column (2) <= row(2) = TRUE, because of this the program will release one more * in the line below and after that will add +1 to the column and make olumn(3) <= row(2) = FALSE, causing him to leave the loop internal and go to the external loop.

With that thought I’m imagining you’ll graduate a vertical row of * since column is only -1 that row, always, when this -1 ceases to exist with the increment of the loop internal, a line is added by loop external, in this way, does not even form the pattern.

1 answer

3


First let’s organize the code to make it easy to understand:

int main(void) {
    int row;
    int column;
    for (row = 1; row <= 7; row++) {
        for (column = 1; column <= row; column++)
            printf("*");
        printf("\n");
    }

Perhaps because it is not indented correctly it implied something else.

We can simplify by declaring the variable in the loop itself:

int main(void) {
    for (int row = 1; row <= 7; row++) {
        for (int column = 1; column <= row; column++)
            printf("*"); //isso pertence ao for interno
        printf("\n"); //isso já é só do for externo
    }
    printf("\n");
}

And yes, the command block does not need a key if it is composed of only one command (statement). A statement may be in a line, may not have its own line, or may have several lines (it only ends with the ; or another way that indicates its end, such as key closing, for example. See another way:

int main(void) {
    for (int row = 1; row <= 7; row++) {
        for (int column = 1; column <= row; column++) printf("*"); //for interno
        printf("\n"); //isso já é só do for externo
    } //está encerrando o statement do for externo
    printf("\n");
}

It’s awful, but it works too:

int 
main
(void) {
    for (
        int row = 1;
        row <= 7;
        row++) {
            for (int column = 1;
                column <= row;
                column++)
                    printf("*"); //isso pertence ao for interno
            printf("\n"); //isso já é só do for externo
    }
    printf
    ("\n")
    ;
}

Or worse:

int 
main
(void) {
for (
int row 
= 1;
row <=
 7;
row++
) {
for (int column = 1;
column <= row;
column
++)
printf("*"); //isso pertence ao for interno
printf("\n"); //isso já é só do for externo
}
printf
("\n")
;
}

There are those who like to do this. I don’t like it, it creates confusion, a maintenance can create a bug easily if the programmer is careless. It is easy to leave the statement "manco", mainly when the block passes have more than one "line" during maintenance. Without the key, only one "line" is part of the block.

I prefer it more clearly, but it’s the same thing. I think reading like this will be clear in the face:

int main(void) {
    for (int row = 1; row <= 7; row++) {
        for (int column = 1; column <= row; column++) {
            printf("*");
        }
        printf("\n");
    }
    printf("\n");
}

Finally, I would start from 0. No need, but a lot will start from 0, get used to.

#include<stdio.h>

int main(void) {
    //na primeira passada declara e atribui valor 0 para row
    for (int row = 0; row < 7; row++) { //em cada passada incrementa row e testa se é menor que 7
        //faz o mesmo aqui, vai entrar e ficar até a condição ficar falsa
        for (int column = 0; column < row; column++) { //o segredo é que cada entrada aqui row está um número maior
            printf("*");
        } //quando sair do laço vai executar a linha abaixo
        printf("\n");
    } //e vai tentar repetir o laço
    printf("\n");
}

Behold functioning as its no ideone. And in the repl it.. Also put on the Github for future reference.

Make a table test.

Your explanation is hard to understand, so I won’t even comment on it.

Read What happens if I don’t specify the { }?. It’s another language, but it’s worth it.

  • Program starts. column < Row = FALSE, program skips a line, Row is added 1. column < Row = TRUE, program releases () like print, column is added 1, column < Row = FALSE, program skips a line, Row is added 1. column (1) < Row (2) = TRUE, program releases () like print, 1 is added to the column by making column < Row = FALSE, program skips one more line, Row is added plus 1. Notice the problem of the way I’m seeing? that way the column will always be a value more than the Row and if it is that way until now would be *

  • within () is to be * and the last two * * * is vertical

  • GUY! I was breaking my head here, but I think maybe this is the reason I’m not understanding: the column value after incrementing in +1 inside the loop until the condition is false is, after exiting the loop, reset right?

  • 3

    I’m sorry, but it’s very difficult to understand the things you write, it’s almost ciphertext, it seems that you want to summarize so much that you eat important pieces. Perhaps this also occurs with your understanding. What you described doesn’t make any sense, so much so that I don’t even know where to start to say what’s wrong.

  • I think I figured out where I was going wrong. I thought the value incremented to the variable column was saved inside the loop, so it wouldn’t work. But the code works precisely pq when I exit the internal loop and go to the external column value back to 0. anyway you helped me because you showed me a more tidy code and asked questions regarding the use of FOR, besides giving test tips. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.