Thread: crazy if statement

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    11

    crazy if statement

    hi i have this code
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    //#include <iof1.h>
    #include <stdarg.h>
    #include <time.h>
    
    
    int main()
    {
            int PORTE,i;
            int trigarray[5];
            int trigmask;
            int totalmask;
            int trig;
    
        trigarray[0]=0;
        trigarray[1]=0;
        trigarray[2]=0;
        trigarray[3]=0;
        trigarray[4]=0;
        trigarray[5]=0;
    
    
        while(1)
        {
            PORTE=4;
    
      if((PORTE=1)&& (trigarray[0]==0)){trigarray[0]=1; trig=1;}
      if((PORTE=2) &&(trigarray[1]==0)){trigarray[1]=1; trig=2;}
      if((PORTE=4) &&(trigarray[2]==0)){trigarray[2]=1; trig=4;}
      if((PORTE=8) &&(trigarray[3]==0)){trigarray[3]=1; trig=8;}
      if((PORTE=16) &&(trigarray[4]==0)){trigarray[4]=1; trig=16;}
      if((PORTE=32) &&(trigarray[5]==0)){trigarray[5]=1; trig=32;}
    
    
    totalmask+=trig;
    printf("%d",totalmask);;
    
         }
    }
    and what i want it to only run each of the if statement so it doesnt mess up my mask

    sooo i only want the because coz i need the while loop it will bump my total mask up beacuse it will keep running the same if statement soo i try to use trigarray and set it to 1 and make the if statements need to requirements to run but its not working my knowledge is not that great can you guys help?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your code has infinite while loop.

    your explanation why it should not run infinitely and when it should exit tell me nothing.

    so could you explain once again what do you want to achieve? and why do you need a loop?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    and what i want it to only run each of the if statement so it doesnt mess up my mask
    I take this to mean that you only want it to run one of the if statements? . . .

    Code:
      if((PORTE=1)&& (trigarray[0]==0)){trigarray[0]=1; trig=1;}
      else if((PORTE=2) &&(trigarray[1]==0)){trigarray[1]=1; trig=2;}
      else if((PORTE=4) &&(trigarray[2]==0)){trigarray[2]=1; trig=4;}
      else if((PORTE=8) &&(trigarray[3]==0)){trigarray[3]=1; trig=8;}
      else if((PORTE=16) &&(trigarray[4]==0)){trigarray[4]=1; trig=16;}
      else if((PORTE=32) &&(trigarray[5]==0)){trigarray[5]=1; trig=32;}
    , in that case, that should be what you're looking for.

    Also, as vart points out, you have a nice infinite loop up there.

    May I point you here as well?
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if assignments in the ifs are by design - this code is alitle bit obfuscated and tells me nothing about its purpose...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    well i want to build up a mask by adding 1 , 2 ,4 ,8 ,16 , 32 which i will then use to make my lights flash but i can only have the 1 be used once or the 2 be used once or the 18 . so i can get get a maximum of 63 if all the ifs add up the final mask.

    its like i only want to allow the if statement to only run once but allow the others to run but when one of them have been used stop it from being used

    its a little hard to understand maybe that helped


    im using this code as if the switch is flicked on make the light blink dont keep running the if statement and if the switch is flicked back down the light will still be blinking

  6. #6
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    So are you looking for something that toggles bits on and off, then? . . .
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what you want - is bit manipulation

    instead of + use

    mask |= (1 << n) ; to set the bit n to 1
    and
    mask &= ~(1<<n); to set it to 0

    I still cannot understand the loop procedure you want to achieve

    could you write it down in word like

    1st iteration set bit 1
    2nd iteration clear bit 1 set bit 2
    ...
    32st iteration clear bit 31 set bit 32
    exit loop
    clear bit 32

    or something like that
    Last edited by vart; 03-19-2009 at 03:04 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    yea that sounds kinda what im trying to do when its on stay on and dont change kinda thing

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by jamespond88 View Post
    yea that sounds kinda what im trying to do when its on stay on and dont change kinda thing
    kinda very far from explanation I have asked
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    soz i was replying to the other guy i think i want to toggle i think what i want is

    if (port1)&&(toggle off) mask=X, toggle on

    so when it then comes back around it wont execute that if because toggle is now on

  11. #11
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Well . . . .

    Just go something like this to set a bit . . .

    Code:
    /* to set a bit on . . . in this case, the third */
    flags |= 0x08;
    /* to remove a bit */
    flags &= ~0x08;
    Make sense? The ~ operator inverts all the bits in a given variable.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    Quote Originally Posted by Nightowl View Post
    Well . . . .

    Just go something like this to set a bit . . .

    Code:
    /* to set a bit on . . . in this case, the third */
    flags |= 0x08;
    /* to remove a bit */
    flags &= ~0x08;
    Make sense? The ~ operator inverts all the bits in a given variable.
    i dont understand if i set the"bit" on what will happen it dont know about bit shifting ect

  13. #13
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Quote Originally Posted by jamespond88 View Post
    i dont understand if i set the"bit" on what will happen it dont know about bit shifting ect
    I'm afraid I can't explain any further before you explain what you're looking for. We're not psychic! We cannot read your mind. You have to explain what the problem is, in enough detail for someone like me, without your previous experiences, to come in and help solve the problem.

    Explain the problem further, then me and vart might be able to help you.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    okky i will try and explain
    i have a bigger program which i will put all this into. i have a while infinity loop in my main program becasue it is responsible for making the zones flash


    zone1=1 000001
    zone2=2 000010
    zone3=4 000100
    zone4=8 001000
    zone5=16 010000
    zone6=32 100000

    these all need to be toggled on ONCE because if zone 1 is used once it will mess up my binary mask. soo thats what i was trying to do with the if statement

    if(TOGGLE=0)&&(zone1) toggle=1

    soo it has to have 2 true requirements for it to run the if but after it runs it once toggle=1 soo only 1 of the requirements is now true so that if will not run

    sorry im not the best of explaining things

  15. #15
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    So, you want to toggle everything on once, then just leave it? Toggle everything on within the amount of time it takes to run 5 instances of a while loop? Or are you thinking of blinking lights?

    I'm sorry, but I don't understand completely what you're trying to do here.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

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, 05:19 AM
  2. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  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