Somoene can help me with those algoritms please ? Thank you in advance

1 - What is the value of x after executing this code ?

count := 0; x := 3;
WHILE (count < 3) DO
    count := count + 1;
    y := (1+2*count) % 3;
    SWITCH (y) DO
        default : 
        case 0 : x := x-1; BREAK;
        case 1 : x := x+5;
    END_SWITCH
END_WHILE

Potential answers : 0, 3, 6, 9, 12 !
My answer is x = 6 !

2 - What is output of this code.

    i:=0;
    j:=0;
    WHILE (i < 10 AND j < 5) DO
        i := i+1;
        j := j+1;
    END_WHILE
    Write(i)
    Write(j)

potential answers : error, 0, 2, 5

My answer : error.

I translated your code to JS and got 6 in the first case and 5 in the second (not sure why you’d get error unless Write function has some specifics (you can copy/paste the code to the browser console).

TIL: default in switch don’t have to be the last case :astonished:

// count := 0; x := 3;
let count = 0;
let x = 3;

// WHILE (count < 3) DO
while (count < 3) {
  // count := count + 1;
  count = count + 1;
  // y := (1+2*count) % 3;
  const y = (1 + 2 * count) % 3;
  // SWITCH (y) DO
  switch (y) {
    // default :
    default:
    case 0:
      // case 0 : x := x-1; BREAK;
      x = x - 1;
      break;
    case 1:
      // case 1 : x := x+5;
      x = x + 5;
  }
  // END_SWITCH
}
// END_WHILE

console.log({ x });
let i = 0;
// i:=0;
let j = 0;
// j:=0;

// WHILE (i < 10 AND j < 5) DO
while (i < 10 && j < 5) {
  // i := i+1;
  i = i + 1;
  // j := j+1;
  j = j + 1;
}
// END_WHILE

// Write(i)
console.log({ i });
// Write(j)
console.log({ j });
2 Likes

Thank you so much, well for the second i thought the code wasn’t write correctly, and i was totally wrong !