switch conditional. switch x case v1 ... case {v2, v3} ... otherwise ... end if conditional. if x ... elseif y ... else ... end if x and if x ~= 0 represent the same condition. The expression x ~= 0 should be treated as a variable whose value is \(\begin{cases} 1 & \text{if} x \neq 0 \\ 0 & \text{if} x = 0 \\ \end{cases}\). while x and while x ~= 0 represent the same loop for the same reason. x = 10; switch mod(x, 4) case 0 x + 1 case {1, 2} x * 2 otherwise x ^ 3 end x = 10 if x < 10 && ~mod(x, 2) x + 1 elseif ~mod(x , 3) x * 2 else x ^ 3 end function z = f(x, y) is called, \(0\), \(1\), or \(2\) arguments can be provided. switch can be used here to perform different tasks when different number of arguments are given. nargin is the number of input arguments provided when the function is called. varargin represents an arbitrary number of input variables. function y = f(x1, x2, x3, varargin). varargin{i}. varargout represents an arbitrary number of output variables. nargout represents the number of output variables assigned when the function is called. x = size([1 2; 3 4]) assigns \(x\) the value \(\begin{bmatrix} 2 & 2 \end{bmatrix}\) and [x, y] = size([1 2; 3 4]) assigns \(x\) the value \(2\). log() returns \(1\), log(x) returns natural log \(\log\left(x\right)\), and log(x, n) returns \(\log_{n}\left(x\right)\). function z = log(x, y) switch nargin case 1 z = log(x); case 2 z = log(x) / log(y); otherwise z = 1; end end function z = log(x, varargin) if nargin == 1 z = log(x); else z = [log(x) zeros(1, nargin - 1)] for t = 2:nargin z(t) = log(varagin{t - 1}) end end end function z = f(x) if x ... base case z = ... else recursion z = ... f(x') ... end function z = f(x) if x == 0 z = 1; else z = x * f(x - 1); end end function z = fib(x) if x < 3 z = 1; else z = fib(x - 1) + fib(x - 2) end end fib(5) function z = combin(x) if y == 0 || y == x z = 1; else z = combin(x - 1, y) + combin(x - 1, y - 1); end end combin(3, 2) Last Updated: September 11, 2025 at 10:54 PM