Home > Software engineering >  Basic Question about If and If else implementation
Basic Question about If and If else implementation

Time:11-19

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)

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.

CodePudding user response:

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.

CodePudding user response:

In the original code, else statement executes only when both conditions row == 0 || columns == 0 and row == Num-1 || columns ==Num-1 are false.

In the updated code, else is executed when row == Num-1 || columns ==Num-1 is false.

If row == 0 || columns == 0 is true, both first if block and else statement are executed, and '*' is written to the array, but then it is overwritten with ' '.

CodePudding user response:

if statements check for all multiple available if . while else if check when if statements fails , if statement return true it will not check for else if.

so it is depend on scenario how your requirement is.

CodePudding user response:

The code doesn't work because the last else will be executed for all occurrences where row != 3 and col != 3. This will in effect overwrite the first row and column of stars. if-else is needed to exclude those occurrences from happening.

You can see the original, correct code as:

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

CodePudding user response:

That final "else" path must not be executed when the first condition is true.

With the change it is executed regardless of the first condition, if the second condition is false.

You can reformulate the logic to three independent "if" statements, but then you would repeat yourself, which is generally discouraged. Therefore you can use this pattern to specify mutually exclusive paths of control flow.

CodePudding user response:

This is because in case if you remove the else from the second else if, the second and third condition will form the if-else pair.

For example, let row = 0, col = 0, Num = 4 :

It will satisfy the first condition if (row == 0 || columns == 0) and set the value arr[row][col] = arr[0][0] = '*';

But then it will encounter second condition (row == Num-1 || columns == Num-1) which returns False as row = col = 0 and Num-1 = 3.

So it will go to its else part where

arr[0][0] = ' ' will become null character again.

Basically, your program will behave as :

    if {
        ....
    }

   { 
     if {
        ....
     }
     else {
      ...
     }
   }

Hope this explanation helps you :)

  • Related