Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Basic Question about If and If else implementation

I am trying to understand why if else is needed for this kind of implementation

Here is my working code

#include <stdio.h>

int main()
{
    int Num, row, columns; //Let Num = User Input 
    
    printf("゚。✧ʕっ•ᴥ•ʔっ PLease Enter a Number from 4 and 50 ⊂ʕ •ᴥ•⊂ ʔ゚。✧: \n");
    scanf("%d", &Num);
    
    char arr[Num][Num];
    
    if (Num >= 4 && Num <= 50){
        for (int row=0; row<Num; row++)
        {
            for (int columns=0; columns<Num; columns++){
                if (row == 0 || columns == 0){
                    arr[row][columns]='*';
                }
                else if (row == Num-1 || columns == Num-1){
                    arr[row][columns]='*'; 
                }
                else
                    arr[row][columns] = ' ';
            }
        }
    }
    else 
        printf("Error! Please enter a valid number :)");

    for(int i = 0; i < Num; i++){
        for(int j = 0; j < Num; j++ ){
            printf("%c", arr[i][j]);
        }
        printf("\n");
    }
 
    return 0;
}

The output of the code is (depending on inputed Num)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

4
****
*  *
*  *
****

My question is why does the code not work if I don’t use if else here

for (int columns=0; columns<Num; columns++){
                if (row == 0 || columns == 0){
                    arr[row][columns]='*';
                }
                if (row == Num-1 || columns ==Num-1){
                    arr[row][columns]='*'; 
                }
                else
                    arr[row][columns] = ' ';
            }

The output will be like this

4
   *
   *
   *
****

I know that if-else statements are executed if the first if is false and won’t execute if is true.

Thank you for any responses in advance.

>Solution :

If we reformat the original:

if (row == 0 || columns == 0){
    arr[row][columns]='*';
}
else if (row == Num-1 || columns ==Num-1){
    arr[row][columns]='*'; 
}
else
    arr[row][columns] = ' ';

Into

if (row == 0 || columns == 0){
    arr[row][columns]='*';
}
else{
    if (row == Num-1 || columns ==Num-1){
        arr[row][columns]='*'; 
    }
    else{
        arr[row][columns] = ' ';
    }
}

Then you can clearly see that the difference. Especially considering the stand-alone else part.

With this, if row == 0 || columns == 0 is false then nothing is executed. But without the else if the stand-alone else part will happen for row == 0 and column == 0, overwriting what you set earlier.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading