Thread: Help me with what syntax is best

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    3

    Help me with what syntax is best

    Hi All!

    Basically, I want to run a number of actions in a sequential order with temperature being the trigger:

    Do w until 25°C
    Do x until 100°C
    Do y until 400°C
    Do z for the rest

    I have been thinking an if else statement but I'm pretty sure there's a better way of doing it (while loops?).

    Thanks!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Do ... until statements usually mean a loop is best, but make sure there is some sort of progression. If the temperature is constant it would mean you are programming an infinite loop.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    Yeah that's what I was scared of. But in my case the particle temperature does not stay constant. And if it falls under a certain range then can code it to just go for the default "do z for the rest".

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    do {     
       //
    }while(temp<N);
    Usually works best whenever a loop must go around at least once, since the test is at the bottom of the loop. A while loop, on the other hand, tests right at the top of the loop, so it will only go through the loop, if the initial test is true. That's a nice distinction to have.

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    That I did not know, thanks! It makes more sense that way if it was a sequential action. I shall let you know how it fares

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Leepox View Post
    Yeah that's what I was scared of.
    Don't be afraid!

    Try something like this:
    Code:
    while (more)
    {
        if      (t <  25)  t = w(t);
        else if (t < 100)  t = x(t)
        else if (t < 400)  t = y(t);
        else               more = z(???);
    }
    or this:
    Code:
    while (t <  25) t = w(t);
    while (t < 100) t = x(t);
    while (t < 400) t = y(t);
    while (more)    more = z(???);
    The second one is probably better since the first performs the if tests every iteration. I'm just representing the fact that t must be updated by passing it in and out. Do what you will.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Syntax Versus Pointer Syntax
    By LyTning94 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2011, 10:56 AM
  2. K & R Syntax for c!!
    By osfreak in forum C Programming
    Replies: 9
    Last Post: 09-07-2010, 03:52 AM
  3. syntax help!!!!!!
    By 12oclock in forum C Programming
    Replies: 4
    Last Post: 02-22-2008, 05:56 PM
  4. map syntax
    By Perspective in forum C++ Programming
    Replies: 4
    Last Post: 01-25-2008, 10:35 AM
  5. About syntax and std::everything
    By Cobras2 in forum C++ Programming
    Replies: 13
    Last Post: 06-04-2003, 04:42 PM