Thread: Basic question on "if" routing

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3

    Basic question on "if" routing

    I am starting to learn C for use with a small AVR single board computer. The board came with C code examples and I am learning how C works by modifying the code and adding sections etc.

    I am stuck with use of the "If" decision making. I have a book and have also tried to follow the example on the tutorial here.

    Rather then put all my code down I have abbreviated a lot, sorry for this but I am more interested in the programme flow. I have attached an jpg image of the wanted flow control with the continues all going out of the if block.

    Below is my attempt at replicating it in C with what I believe is the correct formatting.

    My question is, am I correct? There may well be a better way and I would love to see it but for now yes or no and if no where have I gone wrong. Sorry for the long pre-amble.

    Code:
    if (a)	{
    		if (b){
    				do 1;
    				continue;
    			}
    		else
    			{
    			if (c){
    					if (d)
    						{
    						do 2;
    						continue;
    						}
    					else
    						{
    						continue;
    						}
    					}
    				else
    				{
    				if (e) 
    					do 3;
    				else 
    					continue;
    				}
    			}
    		}
    else
    	continue;

    Cheers

    Adrian
    Last edited by AdrianH; 09-19-2008 at 02:07 PM. Reason: forgot to end the post

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    C has something called a switch case statement.

    Example:
    Code:
    switch(condition)
    {
      case 1: // This just signifies that the condition is equal to 1
         break;
      case 2: // ditto, but 2
         break;
      case 3:
         break;
      default: // Last but never least, this is what happens if none of your special cases are matched.
    }

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    if (a)
    {
    	if (b)
    	{
    		do 1;
    		continue;
    	}
    	else
    	{
    		if (c)
    		{
    			if (d)
    			{
    				do 2;
    				continue;
    			}
    			else
    			{
    				continue;
    			}
    		}
    		else
    		{
    			if (e) 
    				do 3;
    			else 
    				continue;
    		}
    	}
    }
    else
    	continue;
    You need to learn to indent properly or you end up with unreadable code.
    And you should also choose a style and stick with it.
    I notice but one error. A missing instruction in the block highlighted by red.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3
    Elysia;

    Thanks for the response, took me a while to notice even after your response to see I had missed a continue; from that section.

    Sorry for the formatting my editor notepad++ and the forum may well have different tab settings it did not look as bad when I entered it first off. And a style or recognised style is something I guess I will pick up as I go along.

    master5001;

    Thanks for your pointer to an alternative method I will try that as I go.

    Cheers

    Adrian

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    One thing to be aware of about each case statement is that must refer to a constant.

    For example:
    Code:
    switch(key_pressed)
    {
      case user_key_1:
         open_menu();
         break;
    
       ...
    Won't cut it since you can't have a variable as a case. Just constants. I know some dirty tricks around that, however since they are dirty they are also potentially undefined and are definitely undefined according to the standard.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    UK
    Posts
    3
    Hi again master!

    OK some terms are still quite vague to me, so please persevere.

    When you say a constant is that like... value = 6 or similar?

    My book shows an example with a character variable c: and goes on with an example on determining what keyboard character has been used, so I thought it was mean for strings.

    In my present use (a) (b) (c) are all function calls that will return 1 or 0.

    As I say a long way to go, but I will get there, slowly.

    Adrian

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    (In C) Yes, constant means a value (like a number) or a #define.
    In C++, it has a different meaning - meaning that any expression that can be determined at compile time, whether it be a #define, a number or a constant variable.
    Of course, since you're using C, the first line applies to you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    (In C) Yes, constant means a value (like a number) or a #define.
    In C++, it has a different meaning - meaning that any expression that can be determined at compile time, whether it be a #define, a number or a constant variable.
    Of course, since you're using C, the first line applies to you.
    Although modern C also has adopted the "constant variable" concept from C++, so it's the same in C if we assume that the C99 standard is being used.

    Certainly, function calls are NOT constants in either of the two languages. Even if the function always returns a constant value, the call to the function is still a function call [even if the compiler can under some circumstances replace the entire function call with a constant value, it is not a constant from the stricter sense that it needs to be to be a valid case statement].

    It is valid (in both C and C++) to have a statement like this, however:
    Code:
       case 1:
       case 1+2:
    and it will be the same as case 1 and case 3 respectively. Anything that the compiler can figure out what it is during the compile process.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    Certainly, function calls are NOT constants in either of the two languages. Even if the function always returns a constant value, the call to the function is still a function call [even if the compiler can under some circumstances replace the entire function call with a constant value, it is not a constant from the stricter sense that it needs to be to be a valid case statement].
    But that will change, at least to an extent, in the coming C++ standard! Yay.
    I don't know how long C will be left out of that feature, if not always.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  2. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  3. Visual Basic Question
    By Xeavor in forum Tech Board
    Replies: 5
    Last Post: 12-02-2004, 09:59 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM