Home > Net >  Syntax error with if-then-else statement? [duplicate]
Syntax error with if-then-else statement? [duplicate]

Time:10-09

I'm making an algorithm in OCaml (which is supposed to print a pyramid). However, I've run into a syntax error with my if-then-else statement: the first else triggers a syntax error. It's probably a stupid mistake on my part but I can't solve it for the life of me.

let rec build_line_pyramid m n (a,b) = 
  if m >= m/2 then print_string (a) ; build_line_pyramid (m - 1) n (a,b)
  else if n = 0 then print_string (b) ; build_line_pyramid m (n - 1) (a,b)
    else if m >= 0 then print_string (a) ; build_line_pyramid (m - 1) n (a,b)
      else print_newline ()

CodePudding user response:

You should parenthesis your statement

print_string (a) ; build_line_pyramid (m - 1) n (a,b)

like

( print_string (a) ; build_line_pyramid (m - 1) n (a,b) )

because your trailing ';' take precedence over your 'else' statement. Thus closing your if statement.

  • Related