-4

I am new to Programming and currently learning C, so I don't know much about this concept but I was learning Conditional Instructions and at that time, my instructor [I am learning online from YouTube] explained me about logical operators.

He explained that logical operators are used with if...else to decrease the indentation and increase the readability.

After some time, I was solving a problem to find the greatest of four numbers and here, he contradicted the theory. He described nested if to solve this question instead of using logical operators.

Now; I am confused, what approach should I go with and why?

Also; when should I use arithmetic instructions and when should I use nested if...else?

Code written by me:

#include <stdio.h>

int main()
{
    int number1, number2, number3, number4;

    printf("\nEnter the vlaue of number1: ");
    scanf("%d", &number1);

    printf("\nEnter the value of number2: ");
    scanf("%d", &number2);

    printf("\nEnter the value of number3: ");
    scanf("%d", &number3);

    printf("\nEnter the value of number4: ");
    scanf("%d", &number4);

    if(number1 > number2 && number1 > number3 && number1 > number4)
    {
        printf("\n%d is the greatest of four numbers.\n", number1);
    }

    else if(number2 > number3 && number2 > number4)
    {
        printf("\n%d is the greatest of four numbers.\n", number2);
    }

    else if(number3 > number4)
    {
        printf("\n%d is the greatest of four numbers.\n", number3);
    }

    else
    {
        printf("\n%d is the greatest of four numbers.\n", number4);
    }

    return 0;
}

Code written by my instructor:

#include <stdio.h>

int main()
{
    int number1, number2, number3, number4;

    printf("\nEnter the vlaue of number1: ");
    scanf("%d", &number1);

    printf("\nEnter the value of number2: ");
    scanf("%d", &number2);

    printf("\nEnter the value of number3: ");
    scanf("%d", &number3);

    printf("\nEnter the value of number4: ");
    scanf("%d", &number4);

    if(number1 > number2)
    {
        if(number1 >number3)
        {
            if(number1 > number4)
            {
                printf("\n%d is the greatest of four numbers.\n", number1);
            }
        }
    }

    else if(number2 > number3)
    {
        if(number2 > number4)
        {
            printf("\n%d is the greatest of four numbers.\n", number2);
        }
    }

    else if(number3 > number4)
    {
        printf("\n%d is the greatest of four numbers.\n", number3);
    }

    else
    {
        printf("\n%d is the greatest of four numbers.\n", number4);
    }

    return 0;
}
  • 1
    the most readable solution would be to put all your numbers into an array and sort the array. Getting the smallest number is using the first item, Getting the biggest number is using the last item. – Raildex Jan 06 '23 at 11:28
  • 2
    @Raildex Using an O(n*log(n)) sort algorithm is overkill for finding the largest number, which should run in O(n). The reason the posted code is bad is because it's needlessly using a full if-else tree. What it should do is keep the largest value in a variable, then test each number in turn to see if it's larger. – Tom Karzes Jan 06 '23 at 11:39
  • @Raildex Actually; I do not know what an Array is so I just got curious to know what was right or I say, more precise – mohammedanaf Jan 06 '23 at 12:01
  • The instructor’s code is wrong. If you enter `2 1 3 4`, it prints nothing. – Eric Postpischil Jan 06 '23 at 12:03
  • @EricPostpischil I didn't knew that before but when I tried these values, it really isn't printing anything – mohammedanaf Jan 06 '23 at 12:09

3 Answers3

2
  1. The instructor’s code is wrong. If you enter 2, 1, 3, and 4 for the four numbers, it prints nothing, when it should print that 4 is the greatest.

  2. The logical operators, && and ||, are generally used to combine other conditions. Decreasing indentation and increasing readability is a separate goal. Do not fixate on either of these; simply learn to use the operators to perform desired computations and practice making your programs readable.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

Increasing indentation doesn't mean decrease readability. If you have a very long if-condition, it will decrease indentation, but also decrease readability. You should use the only way that allows you to keep your code clean and as simple as possible. Efficiency of code is also necessary since you are talking about C. Logical operators helps to combine two or more if-conditions in one and sometimes it really improve readability, sometimes - not. Sometimes, nested-ifs are better choice, you should think about your situation and choose the best way.

As stated by other commenters, your instructor is wrong and code has logical errors.

Bolderaysky
  • 268
  • 1
  • 8
-2

If number1 = 5, number2 = 3, number3 = 6, number4 = 2

It would print 5 is the greatest of four numbers, whilst 6 is. So this is not correctly coded.

If conditional statements is the topic, your teacher has the more correct answer, however elaborate this may look.

Because on a practical note, you could code:

    /* be sure to #include <stdlib.h> */

    printf("\n%d is the greatest of four numbers.\n", 
          max(max(max(number1,number2),number3),number4);
Dutchpaddy
  • 20
  • 1
  • They both look correct to me. Why do you think 5 would be printed? – interjay Jan 06 '23 at 11:42
  • And there is no `max` function in stdlib.h (or in any other standard library header). – interjay Jan 06 '23 at 11:49
  • @Dutchpaddy When I put these values in VS Code, it displays the correct answer. ''' Enter the value of number1: 5 Enter the value of number2: 3 Enter the value of number3: 6 Enter the value of number4: 2 6 is the greatest of four numbers. ''' – mohammedanaf Jan 06 '23 at 12:02
  • @interjay Right, I stand corrected. Good as conditional statement practice. You should rely on a max() function however, and as you rightly proved it's been a while since I coded C. But I am sure there is a way to get the maximum of four numbers in one go be it with a function or a macro or whatever way. – Dutchpaddy Jan 06 '23 at 14:38