Home > Software engineering >  why is my program running both the if and else conditions?
why is my program running both the if and else conditions?

Time:11-04

I am a newbie to coding. its been like 2 to 3 weeks since I started learning c language. I have learnt using if and else statements from Youtube tutorials and made this small block of code :

#include <stdio.h>
#include <stdlib.h>
  float c,f;
main()
{
     int t=2;
     int o;
     printf("select any one option by entering '1' or '3'  \n 1. celcius to farenheit  \n 3. farenheit to celcius  \n enter any option ");
     scanf("%d",&o);
        if(t<o)
         { 
          printf(" \n enter the value of temperature in celcius ");
          scanf("%f",&c);
          f=(c*1.8) 32;
          printf(" \n the value of temperature of %f celcius in farenheit is %f ",c,f);
         }
        else(t>o);
         {
          printf("  \n enter the value of temperature in farenheit ");
          scanf("%f",&f);
          c=(f-32)/1.8;
          printf("  \n the value of temperature of %f farenheit in celcius is %f ",f,c);
         }
         return 0;
}

I have also tried this way

#include <stdio.h>
#include <stdlib.h>
 float c,f;
 
main()
{
     int o;
     printf("select any one option by entering 'a' or 'b'  \n 1. celcius to farenheit  \n 2. farenheit to celcius  \n enter any option ");
     scanf("%d",&o);
        if(o=1)
         { 
          printf(" \n enter the value of temperature in celcius ");
          scanf("%f",&c);
          f=(c*1.8) 32;
          printf(" \n the value of temperature of %f celcius in farenheit is %f ",c,f);
         }
        else(o=2);
         {
          printf("  \n enter the value of temperature in farenheit ");
          scanf("%f",&f);
          c=(f-32)/1.8;
          printf("  \n the value of temperature of %f farenheit in celcius is %f ",f,c);
         }
}

so the problem is this code is running both the conditions one after the other. so please critique this and also recommend me some good beginner c language self learning books or tutorials if you have any. thank you.

CodePudding user response:

You had a ; after else which you need to remove.

else//;REMOVED THIS SEMICOLON and no need to pass arguments or use else if

Note in your 1st code snippet in the else syntax you don't need to use () . Second you had a semicolon ; after the else which need not be there.

And in your 2nd code snippet you are using = while you should be using == .

You can use else if for your 1st code snippet as follows:

else if(t > o)
{
   ...
}

CodePudding user response:

Firstly:

else(o=2); // <-- notice the semicolon
{
      printf("  \n enter the value of temperature in farenheit ");
      scanf("%f",&f);
      c=(f-32)/1.8;
      printf("  \n the value of temperature of %f farenheit in celcius is %f ",f,c);
}

Will get elvauate to:

else
{
    o = 2;
}
// this will run no matter the condition
{
      printf("  \n enter the value of temperature in farenheit ");
      scanf("%f",&f);
      c=(f-32)/1.8;
      printf("  \n the value of temperature of %f farenheit in celcius is %f ",f,c);
}

Secondly, if(o=1) assign o with 1, and because o is a nonzero value, if(o=1) will return true everytime. You should use == instead.

Thirdly, you else has the wrong systax, else systax look like this

if (somecondition)
{
   // code
}
else
{
   // code
}

Base on what you write, you should use else if instead

You code should look like this:

if(o == 1)
{
    // code
}
else if(o == 2)
{
    // code
}

CodePudding user response:

If you want to check the equality condition you need to substitute the = with ==. In addition you can also use the else-if statement, so your code will be:

if(t<o){
    //actions
}
else if(t>o){
    //actions
}
  • Related