Thread: If Statement, reading switches

  1. #1
    Registered User
    Join Date
    Apr 2019

    If Statement, reading switches

    Hi everyone,

    I'm quite new to C and have not had much practise.

    I have a question in 'revision handout' that asks me to:

    Design c code (non-blocking) that uses two switches, SW1 and SW2, so that SW1 will increment a variables value up to 99 and SW2 will decrement the value down to 0. (Skeleton of the code)

    My idea was this: (assuming switch presses toggle the value to 1)
    int SW1;
    int SW2;
    int MyVariable;
    if(SW1 == 1 & MyVariable <= 0)
    MyVariable ++;
    else(SW2 == 1 & MyVariable >=99)
    MyVariable --;

    I don't know what IDE to use at home to test this, can anyone suggest one suitable for a beginner such as myself

    Any help is greatly appreciated


  2. #2
    Registered User
    Join Date
    Apr 2019
    The easiest way to get started would be Visual Studio Community. It's a free compiler and IDE from Microsoft and there is no shortage of documentation, tutorials and videos regarding how to install and use it.

    However, it's quite cumbersome. For example, to compile a single .c file, you end up needing to create a directory and a project and it's a bit of a pain. If you're comfortable using the command line or even Linux, there are some better options I can tell you about.

  3. #3
    Registered User
    Join Date
    May 2009
    FYI: "&" is the bitwise operator you likely want "&&", instead.
    Bitwise operations in C - Wikipedia

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Melbourne Australia
    I'd recommend code blocks - I've been using it for years. It is quite popular (so there is lots of help online), it's completely free and open source.

    Setting up Code::Blocks on Windows -

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    New York, NY
    @Joel RD:

    You might want to consider compiling and testing on the command line. Learning an IDE can for many, get in way of learning the language itself.

    If you are on Windows, consider Cygwin, or MinGW64.

    gcc is a better compiler than any MS compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading file then if statement not working
    By patrink in forum C Programming
    Replies: 3
    Last Post: 12-17-2010, 01:48 AM
  2. Cat program with switches
    By Heat23 in forum Linux Programming
    Replies: 3
    Last Post: 12-12-2010, 01:16 PM
  3. Best speed switches for gcc
    By rafe in forum Linux Programming
    Replies: 9
    Last Post: 11-06-2002, 12:43 PM
  4. Switches
    By Zophixan in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2002, 08:13 AM
  5. I need help with switches plz
    By frenchfry164 in forum C++ Programming
    Replies: 4
    Last Post: 01-14-2002, 04:32 PM

Tags for this Thread