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.
Aucun commentaire:
Enregistrer un commentaire