Random & Loops Flashcards
(11 cards)
How to generate random numbers?
By using the randi function:
randi(value) generates random numbers from 1 to value
randi(value, m, n) this generates random numbers from 1 to value in an mxn matrix
How do we use logical equality and not assignment?
By using == (double equality)
3 == 3 (True)
2 == 5 (False)
How to create conditionals?
Using the “if - end” statement
If condition
process if condition is true
End
How to display strings or variables?
Using the display command:
disp(“hello world”)
disp(var)
Var must contain something
How can we divide our code in sections?
By using double percentages
%% Creates a section
How do we run for loops?
for i = initial:final
Statement
end
For loops with jumps
for initial: jump : final:
Statement
end
How to time the process of a section of code?
Using tic toc built in function:
Tic
Section of code with several lines
Toc
(We can assign toc to a variable)
How do we create while loops?
while condition
Statements
end
If condition is true, statement will execute, otherwise, it will jump the the while loop
How to force the computer to terminate the execution of a program?
Ctrl + C
How to stop a loop after achieving what you wanted?
By using the “break” command.