Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

if statement - How can I turn this If else function to a loop one

How can I turn this If else function to a loop one. I tried using arrays to store the possible input and output then tried suing a for function but it has failed in vain so I would really appreciate it if someone would solve it for me.

void displayoutput (volatile uint8_t input) { volatile uint8_t output;

if(input == 0x00)
{
    output = 0x3F;        //to print character 0 
}
else if (input == 0x01)
{
    output = 0x06;        //to print character 1
}
else if (input == 0x02)
{
    output = 0x5B;         //to print character 2
}


else if (input == 0x03)
{
    output = 0x4F;          //to print character 3
}
else if (input == 0x04)
{
    output = 0x66;          //to print character 4
}
else if (input == 0x05)
{
    output = 0x6D;          //to print character 5
}
else if (input == 0x06)
{
    output = 0x7D;          //to print character 6
}
else if (input == 0x07)
{
    output = 0x07;           //to print character 7
}
else if (input == 0x08)
{
    output = 0x7F;           //to print character 8
}
else if (input == 0x09)
{
    output = 0x6F;           //to print character 9
}
else if (input == 0x0A)
{
    output = 0x77;            //to print character A
}
else if (input == 0x0B)
{
    output = 0x7C;             //to print character b
}
else if (input == 0x0C)
{
    output = 0x39;             //to print character C
}
else if (input == 0x0D)
{
    output = 0x5E;              //to print character d
}
else if (input == 0x0E)
{
    output = 0x79;               //to print character E 
}
else if (input == 0x0F)
{
    output = 0x71;                 //to print character F
}
else 
{
    output = 0x4C;                //to print character error
}

PORTB = output;
Delay100msUsingTimer0();

}

question from:https://stackoverflow.com/questions/65910815/how-can-i-turn-this-if-else-function-to-a-loop-one

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

QUICK EXAMPLE
Here you have a little example in C of something like you want

#include <stdlib.h>
#include <iostream>
#include <string.h>

int main(){
        std::string input;                          // Declaring var input
        while ( true ){                             // While loop you need to use
                std::cin>>input;                    // Reading user input  
                if ( input == "0x00" ){             // Use the quotes correctly in 0x00, 0x01, etc
                        output = "0x3F";
                }else if ( input == "0x01" ){
                        output = "0x06";
                }
        }

        return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...