www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/29/03:26:00

Mime-Version: 1.0
To: Brad <krusty AT iaccess DOT com DOT au>, djgpp AT delorie DOT com
From: Nate Eldredge <nate AT cartsys DOT com>
Subject: Re: Programming Prob.
Date: Tue, 28 Apr 1998 20:38:06 -0700
Message-ID: <19980429033703.AAO3301@ppp104.cartsys.com>

At 11:38  4/26/1998 +1000, Brad wrote:
>In the following program, I have to print the largest integer using the
>if statement. It works as it is using " else " but I was wondering why I
>can't get it to work without the" else ".As I understand it, if I leave
>out the else the program should skip " answer = int1 ".It doesn't , that
>is why I had to add " else ".If you can help please email at

What evidence do you have that it doesn't? As far as I can tell, there are
two other possibilities, neither of which are correct:

if (int1 > int2)
  answer = int1;
printf(...);

This will leave `answer' containing random garbage if `int2' is greater.

if (int1 > int2)
  answer = int1;
answer = int2;
printf(...);

This will always set `answer' to `int2', which is also wrong.

The way you are doing it now is the correct way. I can't think of any other
way to do it. (Actually I can, but they're highly overcomplicated.)

>
>krusty AT iaccess DOT com DOT au
>
>Thanks in advance
>Brad.
>
>#include <stdio.h>
>
>void main(void)
>
>{
>int int1,int2,answer;
>
>printf("Please enter 2 integers.\n");
>
>scanf("%i %i",&int1 ,&int2);
>
>     if (int1 > int2)
>  answer = int1;
>     else
>
>     answer = int2;
>
>     printf(" The larger integer is %i \n"),answer;
>
>
>}
>
>
>

Nate Eldredge
nate AT cartsys DOT com



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019