SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
Branching and Loops
INFORMATION AND COMMUNICATION TECHNOLOGY – 9
GRAYZON L. GONZALES
Objectives
To be familiarize on the use of branching operation.
To know how endless execution works.
To understand the use and types of the Loops.
Branching
•Remember, in the second chapter you learned that the computer processes a
program one statement at a time, in order from the top to bottom.
•However, there’s a special statement that can make the computer jump to
another statement out of order.
In the program above, we assigned a value of 1 to the variable i. And then we
added a new statement which ends in a colon (:)
This is called a label. Labels are like bookmarks that the computer
understands. You can name the bookmark anything and you can add as many
labels as you want in your program, as long as they are all uniquely named.
Another statement here is:
This just tells the computer to add 1 to the
variable i and assign it back to i. So if the
value of i was 1 before this statement, it will
be 2 after this statement is run.
This is the part that tells the computer
that if the value of i is less than 25,
start executing statements from the
bookmark start.
Endless Execution
•Using the Goto statement you can make the computer repeat something any number of times.
For
•Example, you can take the Even or Odd program and modify it like below, and the program will
run for ever.
•You can stop the program by clicking on the Close (X) button on the top right corner of the
window.
Small Basic - Branching and Loop
For Loop
• This program prints out numbers from 1 to 24 in order.
• This process of incrementing a variable is very common in programming that
programming languages usually provide an easier method of doing this.
• The above program is equivalent to the program below:
Here’s the output:
Notice that we’ve reduced the 8 line program to a 4 line program, and it still does
exactly the same as the 8 line program!
There are usually several ways of doing the same thing and this is a good example.
• For..EndFor is, in programming terms, called a loop. It allows you to take
a variable, give it an initial and an end value and let the computer
increment the variable for you.
• Every time the computer increments the variable, it runs the statements
between For and EndFor.
Step
But if you wanted the variable to be incremented by 2 instead of 1 – like say, you wanted
to print out all the odd numbers between 1 and 24, you can use the loop to do that too.
The Step 2 part of the For statement tells the computer to increment the value of i by 2
instead of the usual 1.
Step
By using Step you can specify any increment that you want. You can even specify a negative
value for the step and make the computer count backwards, like in the example below
While Loop
The While loop is yet another looping method, that is useful especially when the
loop count is not
known ahead of time. Whereas a For loop runs for a pre-defined number of
times, the While loop runs until a given condition is true.
In the example below, we’re halving a number until the result is greater than 1.
• In the program above, we assign the value 100 to number and run the while loop as
long as number is greater than 1.
• Inside the loop, we print out the number and then we divide it by two, effectively
halving it.
• And as expected, the output of the program is numbers that are progressively
getting halved one after another.
• It’ll be really hard to write this program using a For loop, because we don’t know how
many times the loop will run.
• With a while loop it’s easy to check for a condition and ask the computer to either
continue the loop or quit.
• It’ll be interesting to note that every while loop can be unwrapped into an If..Then
statement.
• For instance, the program above can be rewritten as follows, without affecting the end
result.
While Loop

More Related Content

PPT
Algorithm analysis
PPT
Regular expressions-Theory of computation
PPT
Backtracking Algorithm.ppt
PPTX
Huffman Coding Algorithm Presentation
PPTX
sum of subset problem using Backtracking
PPTX
Pseudocode
PPTX
ASSIGNMENT STATEMENTS AND
PPTX
System call
Algorithm analysis
Regular expressions-Theory of computation
Backtracking Algorithm.ppt
Huffman Coding Algorithm Presentation
sum of subset problem using Backtracking
Pseudocode
ASSIGNMENT STATEMENTS AND
System call

What's hot (20)

PPTX
Pushdown Automata Theory
PPT
Minimum spanning tree
PDF
I. Mini-Max Algorithm in AI
PPTX
A presentation on prim's and kruskal's algorithm
PPT
Symbol table management and error handling in compiler design
PPTX
Linked List - Insertion & Deletion
PPT
4 greedy methodnew
PPTX
1.10. pumping lemma for regular sets
PDF
Fundamentals of data structures ellis horowitz & sartaj sahni
PDF
Python Decision Making
PPTX
Naive string matching
PPTX
Turing machine-TOC
PPT
Ip addressing classful
DOC
Branch and bound
PPTX
Encapsulation C++
PPTX
Pseudocode-Flowchart
PPT
Function overloading(c++)
PPTX
IP addressing seminar ppt
PPTX
Procedural vs. object oriented programming
Pushdown Automata Theory
Minimum spanning tree
I. Mini-Max Algorithm in AI
A presentation on prim's and kruskal's algorithm
Symbol table management and error handling in compiler design
Linked List - Insertion & Deletion
4 greedy methodnew
1.10. pumping lemma for regular sets
Fundamentals of data structures ellis horowitz & sartaj sahni
Python Decision Making
Naive string matching
Turing machine-TOC
Ip addressing classful
Branch and bound
Encapsulation C++
Pseudocode-Flowchart
Function overloading(c++)
IP addressing seminar ppt
Procedural vs. object oriented programming
Ad

Similar to Small Basic - Branching and Loop (20)

PPTX
classVII_Coding_Teacher_Presentation.pptx
PPTX
1.4 conditions and loops
PDF
Cis 1403 lab1- the process of programming
PPTX
Algorithm Design and Problem Solving [Autosaved].pptx
PPTX
vingautosaved-230525024624-6a6fb3b2.pptx
PPTX
DOCX
Lab 4 ,5 & 6 Submission (3) 14136_Mohsin Alvi.docx
PDF
Cis 1403 lab5_loops
PDF
4 coding from algorithms
PDF
PROGRAMMING FUNDAMENTALS BASICS LECTURE 1
PDF
Fundamental of Information Technology - UNIT 6
PPTX
Application development with Python - Desktop application
PPTX
C++ good tutorial
PPTX
Algorithms - Introduction to computer programming
PPTX
Unit 3.1 Algorithm and Flowchart
PPTX
Python-Certification-Training-Day-1-2.pptx
PPTX
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
PPTX
classVI_Coding_Teacher_Presentation.pptx
PPTX
classVI_Coding_Teacher_Presentation.pptx
PPTX
ForLoops.pptx
classVII_Coding_Teacher_Presentation.pptx
1.4 conditions and loops
Cis 1403 lab1- the process of programming
Algorithm Design and Problem Solving [Autosaved].pptx
vingautosaved-230525024624-6a6fb3b2.pptx
Lab 4 ,5 & 6 Submission (3) 14136_Mohsin Alvi.docx
Cis 1403 lab5_loops
4 coding from algorithms
PROGRAMMING FUNDAMENTALS BASICS LECTURE 1
Fundamental of Information Technology - UNIT 6
Application development with Python - Desktop application
C++ good tutorial
Algorithms - Introduction to computer programming
Unit 3.1 Algorithm and Flowchart
Python-Certification-Training-Day-1-2.pptx
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
ForLoops.pptx
Ad

More from Grayzon Gonzales, LPT (13)

PPTX
Lesson 2.2 Online Safety And Security.pptx
PPTX
Lesson 2.1 Rules of Netiquette.pptx
PPTX
Lesson 1 Introudctioin to ICT.pptx
PPTX
ICT as a Platform for Change
PPTX
Computer hardware servicing practice occupational health and safety procedure
PPTX
HTML5 - create hyperlinks and anchors
PPTX
HTML5- Create divisions in a web page
PPTX
HTML5 - Insert images and Apply page backgrounds
PPTX
HTML5 - My First Webpage
PPTX
PPTX
HTML Start Up - Introduction to HTML
PPTX
Adobe Photoshop - Brush tool
PPTX
Adobe Photoshop and its role in society ( Introduction)
Lesson 2.2 Online Safety And Security.pptx
Lesson 2.1 Rules of Netiquette.pptx
Lesson 1 Introudctioin to ICT.pptx
ICT as a Platform for Change
Computer hardware servicing practice occupational health and safety procedure
HTML5 - create hyperlinks and anchors
HTML5- Create divisions in a web page
HTML5 - Insert images and Apply page backgrounds
HTML5 - My First Webpage
HTML Start Up - Introduction to HTML
Adobe Photoshop - Brush tool
Adobe Photoshop and its role in society ( Introduction)

Recently uploaded (20)

PDF
Paper PDF World Game (s) Great Redesign.pdf
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PDF
The Internet -By the Numbers, Sri Lanka Edition
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PPTX
Funds Management Learning Material for Beg
PPTX
artificial intelligence overview of it and more
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
E -tech empowerment technologies PowerPoint
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PPTX
innovation process that make everything different.pptx
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PPTX
Internet___Basics___Styled_ presentation
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Paper PDF World Game (s) Great Redesign.pdf
Design_with_Watersergyerge45hrbgre4top (1).ppt
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
The Internet -By the Numbers, Sri Lanka Edition
Power Point - Lesson 3_2.pptx grad school presentation
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Funds Management Learning Material for Beg
artificial intelligence overview of it and more
Unit-1 introduction to cyber security discuss about how to secure a system
Module 1 - Cyber Law and Ethics 101.pptx
E -tech empowerment technologies PowerPoint
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
innovation process that make everything different.pptx
WebRTC in SignalWire - troubleshooting media negotiation
introduction about ICD -10 & ICD-11 ppt.pptx
Cloud-Scale Log Monitoring _ Datadog.pdf
Internet___Basics___Styled_ presentation
international classification of diseases ICD-10 review PPT.pptx
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION

Small Basic - Branching and Loop

  • 1. Branching and Loops INFORMATION AND COMMUNICATION TECHNOLOGY – 9 GRAYZON L. GONZALES
  • 2. Objectives To be familiarize on the use of branching operation. To know how endless execution works. To understand the use and types of the Loops.
  • 3. Branching •Remember, in the second chapter you learned that the computer processes a program one statement at a time, in order from the top to bottom. •However, there’s a special statement that can make the computer jump to another statement out of order.
  • 4. In the program above, we assigned a value of 1 to the variable i. And then we added a new statement which ends in a colon (:) This is called a label. Labels are like bookmarks that the computer understands. You can name the bookmark anything and you can add as many labels as you want in your program, as long as they are all uniquely named.
  • 5. Another statement here is: This just tells the computer to add 1 to the variable i and assign it back to i. So if the value of i was 1 before this statement, it will be 2 after this statement is run. This is the part that tells the computer that if the value of i is less than 25, start executing statements from the bookmark start.
  • 6. Endless Execution •Using the Goto statement you can make the computer repeat something any number of times. For •Example, you can take the Even or Odd program and modify it like below, and the program will run for ever. •You can stop the program by clicking on the Close (X) button on the top right corner of the window.
  • 8. For Loop • This program prints out numbers from 1 to 24 in order. • This process of incrementing a variable is very common in programming that programming languages usually provide an easier method of doing this. • The above program is equivalent to the program below:
  • 9. Here’s the output: Notice that we’ve reduced the 8 line program to a 4 line program, and it still does exactly the same as the 8 line program! There are usually several ways of doing the same thing and this is a good example.
  • 10. • For..EndFor is, in programming terms, called a loop. It allows you to take a variable, give it an initial and an end value and let the computer increment the variable for you. • Every time the computer increments the variable, it runs the statements between For and EndFor.
  • 11. Step But if you wanted the variable to be incremented by 2 instead of 1 – like say, you wanted to print out all the odd numbers between 1 and 24, you can use the loop to do that too. The Step 2 part of the For statement tells the computer to increment the value of i by 2 instead of the usual 1.
  • 12. Step By using Step you can specify any increment that you want. You can even specify a negative value for the step and make the computer count backwards, like in the example below
  • 13. While Loop The While loop is yet another looping method, that is useful especially when the loop count is not known ahead of time. Whereas a For loop runs for a pre-defined number of times, the While loop runs until a given condition is true. In the example below, we’re halving a number until the result is greater than 1.
  • 14. • In the program above, we assign the value 100 to number and run the while loop as long as number is greater than 1. • Inside the loop, we print out the number and then we divide it by two, effectively halving it. • And as expected, the output of the program is numbers that are progressively getting halved one after another.
  • 15. • It’ll be really hard to write this program using a For loop, because we don’t know how many times the loop will run. • With a while loop it’s easy to check for a condition and ask the computer to either continue the loop or quit. • It’ll be interesting to note that every while loop can be unwrapped into an If..Then statement. • For instance, the program above can be rewritten as follows, without affecting the end result. While Loop