Hello do you know why my output not looping ?
Even I already use "do" to make this code for looping..

#include <stdio.h>
#include <conio.h>


int main ()
{
int hari;
double duit;

do
{
printf ("Mari rent kerete...");
printf ("\nkalau 1 hari = 100");
printf ("\nkalau 2-4 hari = 70");
printf ("\nkalau 5 hari dan seterusnya = 50\n");
scanf ("%d", &hari);
    if(hari==1)
    {
        duit = 100;
        printf ("Duit%lf", duit);
    }
    else if((hari<=2)&&(hari>5))
    {
        duit = 100+(hari-1)*70;
        printf("Duit : %lf", duit);

    }
    else if(hari>=5)
    {
        duit = 100+(hari-3)*70+(hari-4)*50;
        printf ("Duit : %lf", duit);
    }
    else
    break;
}
while (hari<1);
getch();
return 0;
}

Kindly reply your answer..

it will only loop as long as the necessary conditions are met in the while statement to continue the loop

also the first else if condition cannot happen, you cant have a value less than or equal to 2 and at the same time greater than 5

Edited 4 Years Ago by zeroliken: n/a

Beacuse your two statements are contradictory :
while(hari < 1)
and
else
break

Make the change that zeroliken just mentioned. Also change your while to while(hari > 1). You will not require the else part also in that case

Thank you very much for your explanation..
Anyway I changed the

while(hari >= 1)

and

else if((hari>=2)&&(hari<5))

Thank you very much for your explanation..
Anyway I changed the

while(hari >= 1)

and

else if((hari>=2)&&(hari<5))

Just another suggestion how you could do it.. (No need of using do while)

while(1)
{
  if(...)
  {

  }
  else if(...)
  {

  }
  else if(...)
  {

  }
  else
     break;
}

Edited 4 Years Ago by DJSAN10: n/a

Thnank you for your suggestion.
But I still don't understand why we need to put number 1...

while(1)

The condition tested in a while loop can be any valid C expression. As long as that condition remains true, the while loop will continue. You can create a loop that will never end by using the number 1 for the condition to be tested. Since 1 is always true, the loop will never end, unless a break statement is reached.
Not that while(1) is nothing but an infinite loop which we 'break'. If we don't provide some way of getting out of the loop (like break,goto) , the loop will go on forever.

Edited 4 Years Ago by DJSAN10: n/a

Just another suggestion how you could do it.. (No need of using do while)

while(1)
{
  if(...)
  {

  }
  else if(...)
  {

  }
  else if(...)
  {

  }
  else
     break;
}

This is a terrible suggestion.
A while loop is designed to exit when a condition is met, which is exactly what the OP's loop needs to do.

There are some programs that can legitimately use an endless loop as you described it, but don't use one just for the fun of it. Use the commands as designed except where necessary.

Surely, while must be used with conditions. I was just giving out one way of doing it. Anyways , you are senior , thanks for your suggestion/correction. :)

This question has already been answered. Start a new discussion instead.