[C] Output of this simple program

In summary, the conversation is about whether a specific C program will output a certain statement six times or not at all. The expert explains that the break statement will cause the program to exit the loop, and the continue statement will go back to the start of the loop, resulting in the printf statement never being executed. They also suggest using a nesting system and recommend some free compilers to use for verification.
  • #1
Jay_
183
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
  • #2


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.
 
  • #3


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.
 
  • #4


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:
  • #5
thanks guys.
 

What is the purpose of this simple program?

The purpose of this simple program is to produce an output based on the input provided by the user. It may perform calculations, manipulate data, or perform other tasks depending on the specific program.

How do I run this program?

To run this program, you will need to have the necessary software or programming environment installed on your computer. Once you have the program open, you can either click on the "run" button or use a specific command to execute the program.

What is the syntax of this program?

The syntax of a program refers to the specific rules and structure that must be followed for the program to function properly. This can include the use of specific keywords, punctuation, and formatting. Each programming language has its own unique syntax.

What is the output of this program?

The output of a program can vary depending on the specific code and input provided. It could be a single value, a series of values, or even a visual representation such as a graph or chart. The output is typically the result of the program's calculations or manipulations.

Can I modify the output of this program?

Yes, it is possible to modify the output of a program by changing the input or making changes to the code. However, this may require knowledge of the programming language used or may result in unexpected results if not done correctly.

Similar threads

  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
942
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top