C program

Discussion in 'Programmer's Corner' started by jjimenez01, Dec 21, 2006.

  1. jjimenez01

    Thread Starter Member

    Dec 8, 2006
    14
    0
    hello,

    I have a programming question, say I have the following C program:

    main()

    {

    While (1)

    {

    if ( condition2)

    {
    Perform A
    }

    else
    {
    Perform B
    }

    Statement D

    } //end while loop

    } // end main loop

    My question here is: will the program ever reach statementD? What I am thinking is that if condition 2 is met, it will perform A, and if not, then it will perfrom B. Then it will never get to the statement D because Im thinking that once the statement in the If section or the else section is performed, the program will go back to the begining and this process will continure for ever and not reach statment D ever.
     
  2. sci-3d

    Well-Known Member

    Aug 22, 2006
    51
    0
    The program reach the statement D absolutely unless in statement A or B you jump it to the beginning by using exit(), break, continue something like that.
     
  3. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    143
    It is important to think about the position of Statement D in the code fragment. As sci-3d says, in the current code Statement D will execute on each iteration of the while-loop, unless there is an exit statement in the if-else block to break from the loop and prevent Statement D from being executed.

    If Statement D is placed outside the while-loop:

    Code ( (Unknown Language)):
    1. main()
    2.  
    3. {
    4.  
    5. [INDENT]While (1)[/INDENT][INDENT]{[/INDENT][INDENT][INDENT]if ( condition2)[/INDENT][INDENT]{ [/INDENT][INDENT]Perform A[/INDENT][INDENT]}[/INDENT][INDENT]else[/INDENT][INDENT]{[/INDENT][INDENT]Perform B[/INDENT][INDENT]}[/INDENT]
    6. } //end while loop
    7.  
    8. Statement D
    9.  
    10.  
    11. [/INDENT]} // end main loop
    Then if there is no mechanism to break or exit the while-loop (i.e. through the exit statement), then Statement D will never be executed.

    Dave
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Greetings jjimenez01,

    Style is the key to keeping things straight when programming. I have taken your code example and editted it to show how style can enhance readability. The liberal use of indenting with spaces or tabs can make a big difference in clarifying the code structure.

    I recommend you adopt this technique as a way assisting you and others in navigating your source code.

    hgmjr
     
  5. aliashar86

    Active Member

    Nov 23, 2006
    71
    0
    The execution order of almost all programming languages is:

    top to bottom and left to right

    so definitly it would reach "Statement D"
     
  6. new2Eworld

    New Member

    Feb 4, 2007
    6
    0
    As a programmer u must b knowing tht flow is d most imp element of a program .Each n every single statement and task happenin within the programm is executed.
    As far as ur program is concerenced the if present has nothing to do with the increment or decrement happening in the while,it will only be performed the number of times the while mets the condition as true so even if the condition for "if" iz true or nt d will b executed it iz related with while nt with if.
    okay!!
     
Loading...