0% found this document useful (0 votes)
63 views11 pages

Algoritma Perancangan Saintifik-5 GFH

The document discusses different types of statements in programming including assignment statements, branching statements, and looping statements. It provides examples of using assignment operators, if/else conditional branching, and for/while loops. It also presents exercises for building a program to compute exponential functions and a number guessing game using looping and branching statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views11 pages

Algoritma Perancangan Saintifik-5 GFH

The document discusses different types of statements in programming including assignment statements, branching statements, and looping statements. It provides examples of using assignment operators, if/else conditional branching, and for/while loops. It also presents exercises for building a program to compute exponential functions and a number guessing game using looping and branching statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Minggu-8 – Pernyataan

Algoritma &
Pemrograman
Saintifik
Gatot F. Hertono, Ph.D

Departemen Matematika
SCMA601401
Breaking News
90

80

70

60

50

40

30

20

10

0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

Sebaran Nilai UTS


Statements
In general, in the programming, statements can be distinguished as:

• Assignment:
To "assign" a variable means to symbolically associate a
specific piece of information with a name. Any operations
that are applied to this "name" (or variable) must hold true
for any possible values.

• Branching:
When an "Algorithm" makes a choice to do one of two (or
more things) this is called branching. The most common
programming "statement" used to branch is the "IF"
statement.

• Looping:
A Loop is used to repeat a specific block of code a over and
over. There are two main types of loops, for loops and while
loops.
Source: http://www.cs.utah.edu/~germain/PPS/Topics/
Variable Assignment
The '=' symbol is the assignment operator which SHOULD NEVER be used
for equality (which is the double equals sign).

Warning, while the assignment operator looks like the traditional


mathematical equals sign, this is NOT the case. The equals operator is
'=='

Syntax: variable_name = expression;

 Expression could be a number : variable_name = 5;


 or a math expression : variable_name = 10 + 5 / 3 - 7;
 or a funciton call : variable_name = sin( 5 );

To evaluate an assignment statement:


1. Evaluate the "right side" of the expression (to the right of the equal sign).
2. Once everything is figured out, place the computed value into the variables
bucket.
Example
B1 = 10.5;
A1 = input(‘What is the current odometer reading?’);
A2 = input(‘How many gallons of gas did you pump?’);
Miles = A1 - B1;
Mileage = Miles/A2;
A = ‘The mileage is’;
Tally = Tally + 1;
disp(A);
disp(Mileage);

Prev Curr Next


Prev = 0; 0
Curr = 1; 1
Next = Prev + Curr; 1
Prev = Curr; 1
Curr = Next 1
Branching
In a computer program, the algorithm often must choose to do one of two
things depending on the "state" of the program.

As an example:
If the grade is greater than 90, then
give the student an A,
otherwise
if the grade is greater than 80,
give the student a B,... etc.

if ( something is true )
do this code;
Syntax: do all code before the end or else;
do not do anything in the else "block" of code
else
% if the something is false (NOTE: we don't have to test this)
do other code;
end
Source: http://www.cs.utah.edu/~germain/PPS/Topics/
Examples
grade = % some_number;
Y
if ( grade > 75 )
fprintf('congrats, your grade %d is passing\n', grade);
end N

if (money < 5) Y
do this;
elseif (money < 10)
N
do that;
Y
elseif (money < 1000)
do a lot of stuff;
else
N
do a default action when nothing above is true; Y
end
N
Looping
Loops - or repeating yourself
Loops allows us to repeat a single (or several) lines of code over and over again.
This allows us to "write once" and then "execute many times“.

There are TWO loops that you must memorize.


 The For Loop: Which is used when we know how many times the loop will
execute.
 The While Loop: When we don't know how many times, but want to
continue until a certain condition is not true.
Nesting Loops
It is perfectly legal to place the code for one loop inside the code of another loop.
What this means is that the "inner" loop is executed one time in its entirety for
every time the outer loop executes.
for i = 1 to 10
As an example, suppose the outer loop
for j = 1 to 10
executes 10 times and the inner loop do these codes
executes 10 times, the code inside the inner end
loop is executed 100 times! end
Source: http://www.cs.utah.edu/~germain/PPS/Topics/
Looping (cont.)
Syntax:
for var_index = start_value : increment_value : end_value
% Do this code
end

% implied increment by 1
for var_index = start_value : end_value
% Do this code
end

while ( condition is true / boolean expression )


% do something
% Note: the "something" should eventually result
% in the condition being false
end
Examples
number = input(‘Input any number: ‘);
count = 0;
while (number > 1) N
number = number / 2;
count = count + 1; Y
end ;

for i=1:10
while ( y < 10 )
disp(i);
x = x + 1;
for j=1:i
end;
Infinite disp(j);
loops for k=1:j
while ( true ) disp(i*j*k)
disp('hello'); end
end; end
end
Exercises
Build a program to compute:

𝑥 𝑥 2 𝑥 3 𝑥 𝑛
ex = 1 + + + +⋯ , −∞ < 𝑥 < ∞
1! 2! 3! 𝑛!

and
Number Guess Games:
The computer will come up with a random
number between 1 and 1000. The player must
then continue to guess numbers until the player
guesses the correct number. For every guess, the
computer will either say "Too high" or "Too low",
and then ask for another input. At the end of the
game, the number is revealed along with the
number of guesses it took to get the correct
number.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy