For without the use of key

0
#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");
}

I want to know if loop is can be used without { after its argument?

Is it possible to translate this code for me? I see the loop coming out of the first for , checking the condition of the second and giving print after that it will add in column and return to add in row also, and getting row = 2 and column = 2 , this way it will give another print .

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

How does this snippet work?

The program starts and releases * as output because column <= row = TRUE (both are 1), then 1 is added to column , making column < = row = FALSE program exits the internal loop and skips a line, adding 1 to row and making column (2) <= row(2) = TRUE again, because of this the program will release one more * on the line below and then of this will add +1 to column and make olumn(3) <= row(2) = FALSE , causing it to exit the internal loop and go to the outer loop.

With this in mind I'm imagining that a vertical row will be formed from * since column is only -1 that row , always, when that -1 ceases to exist with

  •     
    asked by anonymous 15.12.2016 / 01:14

    1 answer

    2

    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 was not properly indented it meant 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 made up of only one statement ( statement ). A statement can be in a row, may not have its own row, or have multiple rows (it only ends with ; or otherwise indicates its end, such as key closing, for 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 this 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 even 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")
    ;
    }
    

    Some people like to do this. I do not like it, it causes confusion, maintenance can easily create an bug easily if the programmer is careless. It is easy to leave the statement "lame", especially when the block passes have more than one "line" during maintenance. Without the key, only a "line" is part of the block.

    I prefer it more clearly, but it's the same thing. I think reading this, it will be clear from 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. It does not have to, but a lot of things will start from 0, get used to it.

    #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");
    }
    

    See working just like yours at ideone and at Coding Ground .

    Make a table test .

    Your explanation is difficult to understand, so I will not even comment on it.

    Read What happens if I do not specify the {}? . It's another language, but it's worth it.

        
    15.12.2016 / 01:51