Why doesn't this c program print the statement

WBahn

Joined Mar 31, 2012
26,077
I disagree.

Switch...case is a shortcut structure that could be implemented by a series of if...else structures.
Of course it can be implemented by a series of if()..else structures. One of the tenets of structured-programming is that it is a completely generic abstraction of computation and all programs could be written using only those constructs. That goes for a program that uses nothing but labeled gotos, which is good since that's how most programs are eventually implemented in hardware.

I’d like it if the break was not required, but it’s inclusion adds flexibility to the construct.
Far, far more than just adding flexibility. The instances in which a switch() statement without any break statements within it is actually useful are very few and far between. Interestingly, if you draw the equivalent structured-programming flowchart of a switch statement having a break at the end of every case and one which has flow-through, the former is far cleaner than the latter in most instances.

While the switch...case construct doesn’t violate structured programming standards, it flexibility allows this to happen.
I'm far from certain that I understand quite what you mean here. If the switch() statement without break statements doesn't violate structured programming because an equivalent structure can be constructed using if()..else constructs, then how does the use of break statements within a switch() statement do so since an equivalent structure can also be constructed using if()..else constructs. What do you have in mind is required to violate or not violate structured programming standards?
 

djsfantasi

Joined Apr 11, 2010
6,724
Of course it can be implemented by a series of if()..else structures. One of the tenets of structured-programming is that it is a completely generic abstraction of computation and all programs could be written using only those constructs. That goes for a program that uses nothing but labeled gotos, which is good since that's how most programs are eventually implemented in hardware.

Far, far more than just adding flexibility. The instances in which a switch() statement without any break statements within it is actually useful are very few and far between. Interestingly, if you draw the equivalent structured-programming flowchart of a switch statement having a break at the end of every case and one which has flow-through, the former is far cleaner than the latter in most instances.

I'm far from certain that I understand quite what you mean here. If the switch() statement without break statements doesn't violate structured programming because an equivalent structure can be constructed using if()..else constructs, then how does the use of break statements within a switch() statement do so since an equivalent structure can also be constructed using if()..else constructs. What do you have in mind is required to violate or not violate structured programming standards?
You’ve changed my mind. I began to have doubts as I was typing my reply. Your response clinched it!

If a statement can be used in a structured way, but also can be used to write an unstructured program, then how the h*|| can I justify it as a structured construct? I can’t and that’s all there is to it.

I was wrong.
 
Last edited:

WBahn

Joined Mar 31, 2012
26,077
If a statement can be used in a structured way, but also can be used to write an unstructured program, then how the h*|| can I justify it as a structured construct?
I've never thought of it just that way, but that strikes me as a very succinct and useful way of looking at it. I'll have to remember that.
 

MrSoftware

Joined Oct 29, 2013
1,866
Why doesn't this c program print the statement hello

I think it should be print 11 times because loop executed 11 time

Can someone please explain the reason behind it?

Code:
#include<stdio.h>

int main()
{
    int x;

    for(x=-1; x<=10; x++)
    {
        if(x < 5)
            continue;

        else
            break;

        printf("Hello");
    }
 
return 0;
}
I'm jumping in here late after skimming; but be sure you're using a debugger that allows you to step through the code. This way you can run the program, then step line by line and see exactly what's happening. It's not only a good debugging tool, but great for learning. If you're on windows, it's difficult to beat Microsoft Visual Studio. Say what you will about Microsoft in general, but Visual Studio is an excellent IDE that is feature rich and very capable.
 
Top