Home > Software design >  While loop with two conditions joined by an AND operator in HLA. Converting C to HLA
While loop with two conditions joined by an AND operator in HLA. Converting C to HLA

Time:10-25

I want to translate (or manually compile) my program from c into HLA. The program reads an inputted number. Then subtracting off three and tens or only tens, determine if that value ends in a zero or a three. Three such numbers in a row win the game! One value that does not end in those numbers lose the game.

I don't know how can I do a while loop with two conditions joined by an AND operator in HLA.

while ((iend != 1) && (iscore < 3))

This is the full code I wrote in C and I want to translate it to HLA:

#include <iostream>
using namespace std;

int main() {
  int inum;
  int iend = 0;
  int iscore = 0;
  int icheckthree; //To check if it ends in 3
  int icheckzero; //To check if it ends in zero

  while ((iend != 1) && (iscore < 3)) {
    cout << "Gimme a number: ";
    cin >> inum;

    //Case 1: ends in three
    icheckthree = inum - 3;
    while (icheckthree > 0) {
      icheckthree = icheckthree - 10;
      if (icheckthree == 0) {
        cout << "It ends in three!" << endl;
        iscore  ;
      }
    }

    icheckzero = inum;
    while (icheckzero > 0) {
      icheckzero = icheckzero - 10;
    }
    //Case 2: ends in zero
    if (icheckzero == 0) {
      cout << "It ends in zero!" << endl;
      iscore  ;
    }
    //Case 3: Loose the game
    else {
      if (icheckzero != 0) {
        if(icheckthree != 0) {
          iend = 1;
        }
      }
    }
    
    if (iend == 1) {
      cout << "\n";
      cout << "Sorry Charlie!  You lose the game!" << endl;
    }
    else if (iscore == 3) {
      cout << "\n";
      cout << "You Win The Game!" << endl;
    } else {
      cout << "Keep going..." << endl;
      cout << "\n";
    }
  }
}

CodePudding user response:

Use logical transformations.

For example, the statement:

if ( <c1> && <c2> ) { <do-this-when-both-true> }

can be translated to:

if ( <c1> ) {
    if ( <c2> ) {
        <do-this-when-both-true>
    }
}

These two constructs are equivalent, but the latter does not use the conjunction.


A while loop can be taken to if-goto-label as follows:

while ( <condition> ) {
    <loop-body>
}

Loop1:
    if ( <condition> is false ) goto EndLoop1;
    <loop-body>
    goto Loop1;
EndLoop1:

Next, separately, an if statement involving the inversion of a conjunction, &&, as follows:

if ( <c1> && <c2> is false ) goto label;

aka

if ( ! ( <c1> && <c2> ) ) goto label;


Is simplified as such:

if ( ! <c1> || ! <c2> ) goto label;

This is by Demorgan's law of logic that relates negation with conjunction and disjunction.


And, lastly, that above disjunction can be easily simplified (similar to the conjunction simplification from above) as follows:

   if ( ! <c1> ) goto label;
   if ( ! <c2> ) goto label;

If the condition of a while loop is a conjunction (&&), you can put the above transformations together to create a conditional exit disjunction sequence.

  • Related