[C] Output of this simple program

  • Thread starter Thread starter Jay_
  • Start date Start date
  • Tags Tags
    Output Program
Click For Summary

Discussion Overview

The discussion revolves around the output of a simple C program involving a for loop, conditional statements, and the use of break and continue. Participants explore the behavior of the program and its execution flow.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant, Jay, believes the printf statement would execute six times for values of i from 5 to 10, based on their interpretation of the loop's logic.
  • Another participant argues that the break statement exits the loop immediately, suggesting that no output will be printed at all.
  • A third participant clarifies that the break statement exits the for loop, not just the if-else structure, reinforcing the idea that the printf statement is never executed.
  • A fourth participant provides a reformatted version of the code, suggesting that better indentation could help avoid misunderstandings in the future.

Areas of Agreement / Disagreement

Participants generally disagree on the output of the program, with some asserting that no output will occur while others, including Jay, believe it would print multiple times. The discussion remains unresolved.

Contextual Notes

There is a lack of consensus on the parsing of the if-else statement and the implications of the break and continue statements within the loop. The absence of a compiler for verification is noted by the original poster.

Who May Find This Useful

This discussion may be useful for individuals learning C programming, particularly those interested in understanding control flow and loop behavior in code.

Jay_
Messages
181
Reaction score
0
Hi, its been some time since I did C prog (its not my field), but I do recall somethings from what I did 4 years ago.

What would be the output of the below prog (I know its a simple one), but I think it would execute for i = 5,6,7,8,9,10 (6 times), the printf statement. Yet some of my friends say it would only execute once and others are saying it would not even execute once.

*****

main()
{
int i;
for (i = -1; i <=10; i++)
{
if (i <5)
continue;

else
break;

printf("Gets printed only once!");
}
}

****

My current logic goes like this: at i = -1 to 4, the condition i<5 is true. So it would just go back to the for loop to keep incrementing i (from i++). At i =5, the statement i<5 is false. So it would go to else. It would encounter break; and then get out of the if-else. Being still inside the for loop it would print "Gets printed only once!". After that it would make i = 6, break from if-else and do the same. Likewise for 7,8,9,10. So total 6 times.

I don't have a C compiler to verify, but I would like to know the truth.

Thanks.
Jay.
 
Technology news on Phys.org


Hey Jay_ and welcome to the forums.

I don't know about the if-else statement how it's parsed, but if it's parsed like it should be, you will never get any printed output whatsoever.

The reason is that you call a break statement which exits the loop as soon as it is encountered. The continue statement will go back to the start of the loop, so you won't get the print statement happening, but the break will terminate from the loop immediately.
 


Jay_ said:
It would encounter break; and then get out of the if-else.

Hi Jay. The break statement doesn't cause it to exit the if-then, it causes it to exit the "for" loop. So, as chiro said, the printf statement is never executed at all.
 


Code:
main()
{
    int i;
    for (i = -1; i <=10; i++)
    {
        if (i <5)
            continue;  // goes to next iteration of loop
        else
            break; // breaks out of loop
        printf("Gets printed only once!"); // executes never
    }
}

If you used a nesting (indentation) system you would find the code easier to read and would perhaps avoid this problem in future.

There are plenty of good free compilers. Try Visual Studio Express (Microsoft) or gcc (Mac and Linux).
 
Last edited:
thanks guys.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K