C program

Thread Starter

jjimenez01

Joined Dec 8, 2006
14
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.
 

sci-3d

Joined Aug 22, 2006
51
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.
 

Dave

Joined Nov 17, 2003
6,970
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:

Rich (BB code):
main()
 
{

While (1)​
{​
if ( condition2)​
{​
Perform A​
}​
else​
{​
Perform B​
}​
} //end while loop Statement D​
} // 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
 

hgmjr

Joined Jan 28, 2005
9,029
hello,

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

Rich (BB code):
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 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.
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
 

new2Eworld

Joined Feb 4, 2007
6
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!!
 
Top