Statement inside a statement.

This is a discussion on Statement inside a statement. within the C Programming forums, part of the General Programming Boards category; Following the tutorial at this site. I now have to write a program that counts from 1 to 10 and ...

  1. #1
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59

    Statement inside a statement.

    Following the tutorial at this site. I now have to write a program that counts from 1 to 10 and displays a "special" output when it reaches the numbers 3 and 10.

    I came up with the following but I am wondering if there is any other way to do this that is more easy using conditional statements.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(void)
    {
        int counter;
        
        for(counter = 0; counter < 11; counter++)
        {
                    printf("The value of the counter is now %d.\n", counter);
                    
                    if(counter == 3)
                    printf("Hey damned, you made it to a value of 3.\n");
                    
                    if(counter == 10)
                    printf("You are at the end now.\n");
        }
        
        system("PAUSE");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    No, you're not going to get much simpler than that. You should indent the lines after "if" though.

    That will count [0,10] though. You said you want to start at 1.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Code:
    #include<stdio.h>
    
    int main(void)
    {
    	for(int counter = 1; counter <= 10; counter++)
    	{
    		printf("The value of the counter is now %d.\n", counter);
    
    		if(counter == 3)
    			printf("Hey damned, you made it to a value of 3.\n");
    		else if(counter == 10)
    			printf("You are at the end now.\n");
    	}
    
    	getchar();
    	return 0;
    }
    Don't use system, it's horrible. Also, use if..else, because the counter can be either 3 or 10, not both at the same time. Declare the counter inside the for, C99 has been there for years now.
    By the way, what about the title? How does it relate to the code?
    Last edited by MisterIO; 11-05-2009 at 12:56 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Don't use system, it's horrible.
    Fair enough, though something to substantiate your claim would be helpful.

    >Also, use if..else, because the counter can be either 3 or 10, not both at the same time.
    I'd call this a style issue since it doesn't affect the behavior either way. The if..else only makes it clear that the two conditions are related.

    >Declare the counter inside the for, C99 has been there for years now.
    This I take issue with. Yes, C99 has been around for years (nearly 11, to be exact). However, it has not been widely adopted by compiler vendors and C95 remains the de facto standard. Until C99 support is the rule rather than the exception, it's best to use an intersection of features for maximum portability.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by Prelude View Post
    >Don't use system, it's horrible.
    Fair enough, though something to substantiate your claim would be helpful.

    >Also, use if..else, because the counter can be either 3 or 10, not both at the same time.
    I'd call this a style issue since it doesn't affect the behavior either way. The if..else only makes it clear that the two conditions are related.

    >Declare the counter inside the for, C99 has been there for years now.
    This I take issue with. Yes, C99 has been around for years (nearly 11, to be exact). However, it has not been widely adopted by compiler vendors and C95 remains the de facto standard. Until C99 support is the rule rather than the exception, it's best to use an intersection of features for maximum portability.
    1) It's very simple: system passes its string argument to the shell for execution in an implementation-defined way. So for example, you could end up on a system where there's no "PAUSE" to call, like unix for example.

    2) It's not just style, he's uselessly always making 2 checks.

    3) Could you make an example of a c compiler that doesn't compile for(int x =...)? Not that it's a problem anyway, I'm just curious. I don't know any that doesn't support this.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So for example, you could end up on a system where there's no "PAUSE" to call, like unix for example.
    Or worse, you could end up with a malicious third party adding or replacing the pause program such that your program creates a huge security hole.

    >2) It's not just style, he's uselessly always making 2 checks.
    As opposed to always making two checks except in the single case where counter is 3? While I certainly prefer the if..else, I don't delude myself into thinking that two if statements makes one lick of difference here. The call to printf will likely outweigh any branching optimizations over the whole range in excess of an order of magnitude anyway.

    >3) Could you make an example of a c compiler that doesn't compile for(int x =...)?
    Microsoft's C compiler is the first heavy hitter that springs to mind. Last I checked, one or two people used it.
    My best code is written with the delete key.

  7. #7
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by MisterIO View Post
    1) It's very simple: system passes its string argument to the shell for execution in an implementation-defined way. So for example, you could end up on a system where there's no "PAUSE" to call, like unix for example.
    When I started I googled for the solution to the problem I had that the program was shutting down before I could see the output.

    What would be the correct way to make the program wait so you can view the output instead of "PAUSE"?

    Next to that I have absolutely no idea what this "C95" and "C99" thing is. Remember that I am just beginning without any school classes to back me up and all those "fancy" terms seem like a foreign language to me.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What would be the correct way to make the program
    >wait so you can view the output instead of "PAUSE"?

    MisterIO's code shows one such way to do it. Replace the system call with a call to getchar. This too has problems, but it should work for now.
    My best code is written with the delete key.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,309
    Quote Originally Posted by JOZZY& Wakko
    When I started I googled for the solution to the problem I had that the program was shutting down before I could see the output.

    What would be the correct way to make the program wait so you can view the output instead of "PAUSE"?
    There are several ways.

    Quote Originally Posted by JOZZY& Wakko
    Next to that I have absolutely no idea what this "C95" and "C99" thing is.
    C90 (or C89) refers to the 1990 edition of the C standard. C99 refers to the 1999 edition of the C standard. As you can tell, they don't care about Y2K
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by JOZZY& Wakko View Post
    When I started I googled for the solution to the problem I had that the program was shutting down before I could see the output.

    What would be the correct way to make the program wait so you can view the output instead of "PAUSE"?

    Next to that I have absolutely no idea what this "C95" and "C99" thing is. Remember that I am just beginning without any school classes to back me up and all those "fancy" terms seem like a foreign language to me.
    I've already put that into the code, it's getchar()(for example).

  11. #11
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Thanks for the help and explanations people time for me to head off to the next subject array's.

    Ow, should I look into the C standards for '99 or is this of no importance at the moment. And if it is where should I take a look to learn about it?

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by Prelude View Post
    >So for example, you could end up on a system where there's no "PAUSE" to call, like unix for example.
    Or worse, you could end up with a malicious third party adding or replacing the pause program such that your program creates a huge security hole.

    >2) It's not just style, he's uselessly always making 2 checks.
    As opposed to always making two checks except in the single case where counter is 3? While I certainly prefer the if..else, I don't delude myself into thinking that two if statements makes one lick of difference here. The call to printf will likely outweigh any branching optimizations over the whole range in excess of an order of magnitude anyway.

    >3) Could you make an example of a c compiler that doesn't compile for(int x =...)?
    Microsoft's C compiler is the first heavy hitter that springs to mind. Last I checked, one or two people used it.
    2) I didn't say that it's a major optimization that will skyrocket the performance of code, but it's still an optimization. Is that the main reason why I prefer it? No, the main reason is that it makes the code more self-documenting, because it better expresses what's its expected logical execution. In this little example, that too is not very important. In a large piece of code, it helps a lot to better understand what the original programmer intended.

    3) I knew that Microsoft C++ wasn't C99 compliant, but I thought it could compile this simple extension, which is also present in c++. After a little check on the net, it seems that you're right, it can't compile that in c. Wow! Microsoft is even worse than I thought! And it's not like I thought anything good of it either.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >should I look into the C standards for '99 or is this of no importance at the moment.
    I'd recommend that you be aware of the standards, but don't bother with studying them just yet. You have more important things to worry about.

    >2) I didn't say that it's a major optimization [...]
    Good, then we agree.

    >Wow! Microsoft is even worse than I thought!
    Actually, I'd say that's a point in favor of Microsoft. If they don't support C99 then that code shouldn't compile. Personally, I don't care much for the C99-specific stuff, and the C95 support in Microsoft's compiler is first class, so that compiler is one of my regulars.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by JOZZY& Wakko View Post
    Thanks for the help and explanations people time for me to head off to the next subject array's.

    Ow, should I look into the C standards for '99 or is this of no importance at the moment. And if it is where should I take a look to learn about it?
    Looking at C99 right now is not as important as using a good c compiler like gcc. (If you use windows, try mingw, even if probably, for windows, the intel compiler is better[not sure though])
    Last edited by MisterIO; 11-05-2009 at 03:30 PM.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Quote Originally Posted by MisterIO View Post
    3) I knew that Microsoft C++ wasn't C99 compliant, but I thought it could compile this simple extension, which is also present in c++. After a little check on the net, it seems that you're right, it can't compile that in c. Wow! Microsoft is even worse than I thought! And it's not like I thought anything good of it either.
    GCC defaults to C95 compatibility as well. You need to pass a special option to compile in C99 mode.
    bit∙hub [bit-huhb] n. A source and destination for information.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 06:19 AM
  2. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 09:13 AM
  3. cin.get() inside switch statement
    By timberwolf5480 in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 01:26 AM
  4. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 10:00 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21