0% found this document useful (0 votes)
5 views

Term2 Comp Scratchalgo

Uploaded by

srrm.nnn
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)
5 views

Term2 Comp Scratchalgo

Uploaded by

srrm.nnn
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/ 10

Algorithms- IF- Else statement

Conditional Statements: if, else


The single if statement in is used to execute the code if a condition is true. It is also called one-way selection statement.
Syntax
If(condition)Then
'Statement(s) 'will execute if the condition is true
End
If-else statement
The if-else statement is used to execute the code if condition is true or false. It is also called two-way selection statement.
Syntax
If(condition)Then
'statement(s) will execute if the condition is true
Else
'statement(s) will execute if the condition is false
End If

1
1.Algorithm & Flowchart to find the smallest of two numbers: 2. Algorithm & Flowchart to find the largest of two numbers:

3. Write an algorithm to determine a student’s final grade and Write an algorithm that accept an integer number from the
indicate whether it is passing or failing. The final grade is user, in case of the number is Positive, check and print out
calculated as the average of four marks. whether it is Even or Odd number.

2
Write An Algorithm That Read Student's Mark As Integer Then Print the Equivalent Grade Depends On The Following Table:

3
Conditionals in scratch:
Conditional statements have conditions and the programs flow based on the true
or false value of the condition. They are found in the control programming blocks
as shown in the picture.
Here, if() then and if() then else are the conditional blocks.

1. if() then block


 It is a control block. This block works on the condition’s value.
 If the condition is true, then the set of code is executed else nothing
happens.
 Here, the condition is only checked once, and if the condition is changed to
false while the script is running, then the script will keep running until it is
finished.

2. if-then-else
 It is a control block. This block is an extension of the if-then value.
 If a condition is true, then a set of lines gets executed.
 If a condition is false, then else part is executed.

Simple if statements can be used when we want to execute a command only if


some condition is True. If we want to execute one set of statements if a condition
is True and a different set if that condition is False, we use an if-else statement.

Operators in scratch:The Operators blocks are used to compare 5) There are blocks for working with text strings:
variables and values, do calculations with numbers, and work with
4
strings (text). a) join block

The hexagonal ‘Boolean’ blocks are used as conditions, and return


true or false. These blocks can be used in Control blocks with a
hexagonal input.
You can use the join block from the Operators block to
1)Comparison operators used to compare numbers join text and variables to make longer strings.

2) Math operators used to perform mathematical calculations

b) “length of ()” Operator block:

3) Pick random numbers:

You can choose random numbers between the smallest and largest This operator block reports the number of characters that a
number (including those numbers) using below operator. given string (input) contains.

Letter () block:
4) And, or, not Operators:

The and, or, and not operators can be used to combine conditions.
This block reports the specified character of the given text.
Even though the block says "letter", it will report all
characters, including letters, numbers, symbols, and even
spaces.
Variables in Scratch:
Here,
5
In Scratch programming language variables are similar to variables  Variable: This block is used to reports the value of a
in other programming languages. variable
 set variable to 0: This block sets a selected variable
A variable is a named value or data storage location that can hold a
to a specified value.
value or information that can be changed during the execution of a
program.  change variable by 1: This block changes the value
of the selected variable.
Variables are used to store data that a program can access and  show variable: This block displays the specified
manipulate as needed. variable in the project player.
 hide variable: The block reverses the “show
Scratch contains the following variables blocks:
variable” block.

Sample Scratch programming:


Product of two numbers:
Program to add to numbers:
6
Division of two numbers:
Program to check whether the entered number is odd or
even.
7
Program to check whether entered number is a multiple of 5
or 7 or both.

Program to enter marks of two subjects and then check if total is


8
greater than 50 then print Congratulations message else print
Welcome back to CP5 again ( IF – ELSE condition looping).

Program using join , length of () , letter () operator.


9
10

You might also like