SlideShare a Scribd company logo
Introduction to
Coding
Grade VIII
Version 1.0
Ethics of coding
• Respect others privacy
• Honor confidentiality
Chapter 1: Conditionals in detail
At the end of this this chapter you will get to understand
conditionals in detail. You will know:
• IF ELSE and ELSE IF statement
• Using logical operators with IF ELSE statement
• Nested Condition
Types of control structures
• In programming, control structure is a block of program that accepts and analyses
different variables and chooses the direction in which to go based on the given
parameters.
• There are three basic types of control structures in programming:
• Sequential
• In a sequential control structure, the statements in a program are executed sequentially,
i.e., step-by-step in an order that they are written.
• Selection / Conditional
• A selection (conditional) control structure is used to test a condition in a program. This
control structure takes a decision to execute one statement/operation over another
statement/operation depending on the condition.
• Iteration
• This control structure executes a set of statements for a certain number of times till the
mentioned condition is true. Loops are examples of iterative statements.
IF ELSE and ELSE IF
Simple illustration of conditionals using
flowchart, to determine if a number is odd or
even.
Logical Operators
The three most important logical operators are AND, OR and NOT.
• The AND operator is used to see if two or more
conditions are true. If all the conditions are true,
the AND operator returns TRUE. If any one of the
conditions fail, the AND operator returns FALSE.
AND
• The OR operator is used to see if either one of two
or more conditions is TRUE. If any of the condition is
true, the OR operator returns TRUE. If all the
conditions fail, the OR operator simply returns FALSE.
OR
• We use the NOT operator to reverse or negate a
condition. If the condition is true, NOT will return
false and vice-versa.
NOT
Relational Operators
Operator Symbol Example Meaning
Greater than > x > y x greater than y
Equal to == x == y x is equal to y
Less than < x < y x is less than y
Greater than or equal to >= x >= y x is greater than or equal to
y
Less than or equal to <= x <= y x is less than or equal to y
Not equal to != x! = y x not equal to y
Combined use of logical
& relational operators
Can you create a Triangle?
Operators in code
Precedence of Logical Operators
• Just like arithmetic operators, logical operators also
have precedence that determines how the operations
are grouped in the absence of parentheses.
• In an expression, the operator with highest
precedence is grouped with its operands first, then
the next highest operator will be grouped with its
operands, and so on.
• In a situation where there are many logical operators
of the same precedence, they will always be examined
from left to the right.
• The figure of the left demonstrates the precedence of
logical operators.
Operator Precedence
NOT (!) High
AND (&&) Medium
OR (||) Low
Nested conditions
Chapter 2: Get creative with loops
At the end of this this chapter you will understand
Loops in programming. You will know:
• What are the different types of Loops?
• What is the definition of different types of Loops?
• What are Break and Continue Loops?
What are Loops?
• Everyday there are some tasks which need to be done repeatedly.
• Loops are programming elements which repeat a portion of the code for a
set number of times till the desired process is complete.
• Repetitive tasks are common in programming and loops are important to
save time and minimize errors.
Types of loops
Loops make our code more manageable and organized. Let us now see what the different
types of loops are:
While Loop
For Loop
Nested Loop
The While loop
• The While loop can execute a set of commands
till the condition is true
• While Loops are also called conditional loops.
• Once the condition is met then the loop is
finished
For example - Print from 1 to 10
Here, if we want to derive the loop
from this scenario, we have the
following conditions:
Condition: Write from 1 to 10
And the decision we are deriving is:
Decision: Have we reached 10
X=0
While X is not 10
X=X+1
Print (X)
The For loop
• A for loop is used for iterating over a sequence.
• The loop continues to execute until it reaches
the end of the sequence.
For example – We need to calculate
the square of numbers present in a list
Numbers = [1,3,5,7,13]
#List of Integer Numbers
Numbers= [1,3,5,7,13]
#Variable to store square of a number
sq=0
for x in numbers:
sq=x*x
Print(sq)
Break & continue in
loops
• Statements break and continue may be used
within the loops to alter the control flow.
• Break statement terminates the loop and causes
the execution to resume after the loop.
• Continue leaves the current iteration and
executes with the next value in the loop.
Continue skips the statements after the continue
statement and keeps looping.
Nested loop
• Nested loop is a loop that occurs in another loop.
• When we nest one loop within another, the outer
loop takes control of the total number of complete
repetition of the inner loop.
The program shown below
demonstrates the use of nested loops.
Exit Criteria
• Exit criteria is defined as a condition that must be met before completing a
specific task.
• It is a set of conditions that must exist before you can declare a program to be
complete.
• Exit criteria is one of the most important components while defining a loop.
• As without an exit criterion, the program tends to enter in an infinite loop.
• These criteria differ from program to program as per the requirement.
Chapter 3: Functions in depth
At the end of this chapter, you will understand
functions in programming. You will have a better
understanding of functions. You will know:
• How using functions helps us?
• Create and use functions in block coding
What are functions?
• If there is a block of code that
you want your computer to
repeat lots of times, then you
might want to use a function
• A function is a block of code
made up of a set of steps that
results in a single specific
action
Advantages of using functions
Increases readability
By making code organized and easy to
understand
Code reusability increases
Redundant code is removed and replaced
by functions
Parameters and
return types Function to calculate area of a circle
• Variables accepted by a
function to perform a set of
actions defined in its body
are called function
parameters
• Type of value returned by a
function are called return
types
Return
type
Parameter
Functions called
within another
function
• One or more functions can be
defined within a program.
• These functions can then be
called from other functions in
the same program.
Function to calculate sum of area &
perimeter of a square by calling other
functions
Chapter 4: Programming with arrays
• In this chapter we will see how we can work with
large set of data in programming.
• What are arrays and its practical implementation in
computers?
• Overview of collections
Arrays in
programming
What are arrays:
• Arrays in programming are a collection of similar data
type variables.
• Arrays do not support different data types in same
collection.
• Arrays improve readability of code by using a single
variable for a large set of data.
Limitations of using arrays are:
• You can only store variables with homogenous data
types in an array.
• Arrays are always static and fixed in length and size.
• The variables in an array are always ordered
sequentially with index starting with 0.
Arrays in
programming
Shown above is a character array of length 7
Arrays in
programming
Suppose we have three variables to store three
different colors:
Color1 = “red”
Color2 = “green”
Color3 = “blue”
We can use an array to store three different colors in
a single variable.
Shown below is how we can store three different
colors using a single variable using an array:
Color = [“red”, ”green”, “blue”]
Arrays in
programming
Suppose we have an array named Color.
Color = [“red”, ”green”, “blue”]
• We can access the elements of the Color array the
following way:
Color[0] = “red”
Color[1] = “green”
Color[2] = “blue”
• We can alter the value at an index of an array by assigning
it a new value as shown below:
Color[1] = “yellow”
Sorting an
array
Sorting an array can be defined as ordering the elements in
the array either in ascending or descending order.
(SORT)
In the above scenario, we are sorting the elements of an
integer array in ascending order.
Some of the algorithms used to sort an array are:
• Bubble sort
• Quick sort
1 5 4 3 2
1 2 3 4 5
Sorting an
array using
bubble sort
Bubble sort is a method of sorting that works by repeatedly
swapping adjacent elements if they are in incorrect order.
Let us consider a set of numbers 1, 5, 4, 3, 2; that needs to be
sorted in ascending order.
We use an integer array of length 5 to store these numbers.
1 5 4 3 2
Sorting an
array using
bubble sort
1 5 4 3 2
• To start with, we compare the first two numbers and find
out which one is greater.
• We start with 1 and 5. Since 5 is already greater than 1, no
change is made
• Then we compare the numbers 5 and 4
• Since 5 is greater than 4, we will swap these two numbers.
1 5 4 3 2
Sorting an
array using
bubble sort
1 4 5 3 2
• Next, we compare 5 and 3
• Since 5 is greater than 3, we will swap the two numbers.
• Lastly, we compare 5 and 2
• Since 5 is greater than 2, we will swap these two numbers
1 4 3 5 2
Sorting an
array using
bubble sort
1 4 3 2 5
• The list of the numbers is rearranged as follows:
• Notice 5 is the largest number in this list and arranged at
the last position.
• We again start from the beginning and compare 1 with 4.
Since 4 is greater than 1, no change is made.
1 4 3 2 5
Sorting an
array using
bubble sort
1 4 3 2 5
• Then we compare the numbers 4 and 3
• Since 4 is greater than 3, we will swap these two numbers.
• Next, we compare 4 and 2
• Since 4 is greater than 2, we will swap the two numbers.
1 3 4 2 5
Sorting an
array using
bubble sort
1 3 2 4 5
• Lastly, we compare 4 and 5. Since, 5 is greater than 4, no
change is made.
• We again start from the beginning and compare 1 with 3.
Since 3 is greater than 1, no change is made.
1 3 2 4 5
Sorting an
array using
bubble sort
1 3 2 4 5
• Then we compare the numbers 3 and 2
• Since 3 is greater than 2, we will swap these two numbers.
• Next, we compare the numbers 3 and 4. Since 4 is greater
than 3, no change is made.
1 2 3 4 5
Sorting an
array using
bubble sort
1 2 3 4 5
• We again start from the beginning and compare 1 with 2.
Since 2 is greater than 1, no change is made.
• The numbers are now all sorted in ascending order.
• The exercise of sorting is done until no more numbers
need to be swapped.
1 2 3 4 5
Chapter 5: Advanced Sequencing
• In this chapter we will learn what is advanced
sequencing in programming.
• What is a sequence?
• Why is sequencing important in programming?
• Sequencing with Loops and Conditions
What is a Sequence?
• A series of actions performed in a specific order is called sequence.
• We follow sequence in many things that we do on daily basis.
• Our daily routine is also a series of actions that we perform like:
• Wake up
• Have breakfast
• Take a shower
• Go to School
• Attend classes
• Come back home
• Do homework
• Have dinner
• Go to bed
• However, this routine might vary from person to person but for a specific person this
might be the routine.
What is a Sequence? (Contd.)
• An algorithm is a set of steps to solve a problem.
• A program is written based on an algorithm to get the required result.
• Sequencing in algorithms is arranging the steps of an algorithm in a correct
sequence so that we get the correct outcome.
• Sequencing is important in algorithms.
• If the steps of an algorithm are not in correct sequence, we will not get the
required result.
Algorithm to calculate sum of two numbers
• Consider a simple algorithm to calculate the sum
of two numbers.
• The steps of this algorithm would look like as
shown in the flowchart on the left.
• However, we need to note that if we swap 2
steps and add Num1 and Num2 before taking
Num1 as the input, the program will not give the
required result or give an error.
• This is called as a bug.
Sequencing with
Loops and Conditions
• Most programs consist of 3 main
components – Sequence, Selections
(Conditions) and Loops.
• If we write a program to get all the
numbers divisible by 3 between 0 and
100. The flowchart for the algorithm
would be as shown in the image on the
left.
• In this example, we see that this
program has a sequence of conditions
and loops.
Thank You

More Related Content

PPTX
C Language Part 1
PPTX
Python Introduction controll structures and conprehansion
PPTX
CSE-113 UNIT-1 with the basic knowledge of the computer science.pptx
PPTX
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
PPTX
Guide to Java.pptx
PDF
Slide 6_Control Structures.pdf
PPTX
classVII_Coding_Teacher_Presentation.pptx
PPTX
Operators loops conditional and statements
C Language Part 1
Python Introduction controll structures and conprehansion
CSE-113 UNIT-1 with the basic knowledge of the computer science.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
Guide to Java.pptx
Slide 6_Control Structures.pdf
classVII_Coding_Teacher_Presentation.pptx
Operators loops conditional and statements

Similar to classVIII_Coding_Book018979929470479.pdf (20)

PPTX
CPP04 - Selection
PDF
introduction of Data structure with example
PPTX
NUMERICAL METHODS WITH MATLAB PROGRAMMING
PPTX
JAVA programming language made easy.pptx
PPTX
Module_2_1_Building Python Programs_Final.pptx
PPTX
Chapter 3 summaryv2(Pointers) in c++.pptx
PPTX
Java-Programming.forBSITSTUDENTfreespptx
PPT
Data structure
PPTX
Review of C programming language.pptx...
PPTX
C language (Part 2)
PDF
c++ Data Types and Selection
PPTX
Review Python
PPT
Programming.pptVBVBBMCGHFGFDFDHGDFKJKJKKJ;J
PPTX
unit1 python.pptx
PPTX
Programing techniques
PDF
Programming techniques
PPTX
Week 4.pptx computational thinking and programming
PPTX
Basic syntax : Algorithm,Flow chart
PPTX
Shell Programming Language in Operating System .pptx
PPTX
Android webinar class_java_review
CPP04 - Selection
introduction of Data structure with example
NUMERICAL METHODS WITH MATLAB PROGRAMMING
JAVA programming language made easy.pptx
Module_2_1_Building Python Programs_Final.pptx
Chapter 3 summaryv2(Pointers) in c++.pptx
Java-Programming.forBSITSTUDENTfreespptx
Data structure
Review of C programming language.pptx...
C language (Part 2)
c++ Data Types and Selection
Review Python
Programming.pptVBVBBMCGHFGFDFDHGDFKJKJKKJ;J
unit1 python.pptx
Programing techniques
Programming techniques
Week 4.pptx computational thinking and programming
Basic syntax : Algorithm,Flow chart
Shell Programming Language in Operating System .pptx
Android webinar class_java_review
Ad

Recently uploaded (20)

PDF
Lecture1 pattern recognition............
PDF
annual-report-2024-2025 original latest.
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PDF
Foundation of Data Science unit number two notes
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PDF
Business Analytics and business intelligence.pdf
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
Introduction to machine learning and Linear Models
Lecture1 pattern recognition............
annual-report-2024-2025 original latest.
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
oil_refinery_comprehensive_20250804084928 (1).pptx
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Business Ppt On Nestle.pptx huunnnhhgfvu
.pdf is not working space design for the following data for the following dat...
IBA_Chapter_11_Slides_Final_Accessible.pptx
Foundation of Data Science unit number two notes
Qualitative Qantitative and Mixed Methods.pptx
Reliability_Chapter_ presentation 1221.5784
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Business Analytics and business intelligence.pdf
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
Galatica Smart Energy Infrastructure Startup Pitch Deck
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
Introduction to machine learning and Linear Models
Ad

classVIII_Coding_Book018979929470479.pdf

  • 2. Ethics of coding • Respect others privacy • Honor confidentiality
  • 3. Chapter 1: Conditionals in detail At the end of this this chapter you will get to understand conditionals in detail. You will know: • IF ELSE and ELSE IF statement • Using logical operators with IF ELSE statement • Nested Condition
  • 4. Types of control structures • In programming, control structure is a block of program that accepts and analyses different variables and chooses the direction in which to go based on the given parameters. • There are three basic types of control structures in programming: • Sequential • In a sequential control structure, the statements in a program are executed sequentially, i.e., step-by-step in an order that they are written. • Selection / Conditional • A selection (conditional) control structure is used to test a condition in a program. This control structure takes a decision to execute one statement/operation over another statement/operation depending on the condition. • Iteration • This control structure executes a set of statements for a certain number of times till the mentioned condition is true. Loops are examples of iterative statements.
  • 5. IF ELSE and ELSE IF Simple illustration of conditionals using flowchart, to determine if a number is odd or even.
  • 6. Logical Operators The three most important logical operators are AND, OR and NOT. • The AND operator is used to see if two or more conditions are true. If all the conditions are true, the AND operator returns TRUE. If any one of the conditions fail, the AND operator returns FALSE. AND • The OR operator is used to see if either one of two or more conditions is TRUE. If any of the condition is true, the OR operator returns TRUE. If all the conditions fail, the OR operator simply returns FALSE. OR • We use the NOT operator to reverse or negate a condition. If the condition is true, NOT will return false and vice-versa. NOT
  • 7. Relational Operators Operator Symbol Example Meaning Greater than > x > y x greater than y Equal to == x == y x is equal to y Less than < x < y x is less than y Greater than or equal to >= x >= y x is greater than or equal to y Less than or equal to <= x <= y x is less than or equal to y Not equal to != x! = y x not equal to y
  • 8. Combined use of logical & relational operators Can you create a Triangle?
  • 10. Precedence of Logical Operators • Just like arithmetic operators, logical operators also have precedence that determines how the operations are grouped in the absence of parentheses. • In an expression, the operator with highest precedence is grouped with its operands first, then the next highest operator will be grouped with its operands, and so on. • In a situation where there are many logical operators of the same precedence, they will always be examined from left to the right. • The figure of the left demonstrates the precedence of logical operators. Operator Precedence NOT (!) High AND (&&) Medium OR (||) Low
  • 12. Chapter 2: Get creative with loops At the end of this this chapter you will understand Loops in programming. You will know: • What are the different types of Loops? • What is the definition of different types of Loops? • What are Break and Continue Loops?
  • 13. What are Loops? • Everyday there are some tasks which need to be done repeatedly. • Loops are programming elements which repeat a portion of the code for a set number of times till the desired process is complete. • Repetitive tasks are common in programming and loops are important to save time and minimize errors.
  • 14. Types of loops Loops make our code more manageable and organized. Let us now see what the different types of loops are: While Loop For Loop Nested Loop
  • 15. The While loop • The While loop can execute a set of commands till the condition is true • While Loops are also called conditional loops. • Once the condition is met then the loop is finished For example - Print from 1 to 10 Here, if we want to derive the loop from this scenario, we have the following conditions: Condition: Write from 1 to 10 And the decision we are deriving is: Decision: Have we reached 10 X=0 While X is not 10 X=X+1 Print (X)
  • 16. The For loop • A for loop is used for iterating over a sequence. • The loop continues to execute until it reaches the end of the sequence. For example – We need to calculate the square of numbers present in a list Numbers = [1,3,5,7,13] #List of Integer Numbers Numbers= [1,3,5,7,13] #Variable to store square of a number sq=0 for x in numbers: sq=x*x Print(sq)
  • 17. Break & continue in loops • Statements break and continue may be used within the loops to alter the control flow. • Break statement terminates the loop and causes the execution to resume after the loop. • Continue leaves the current iteration and executes with the next value in the loop. Continue skips the statements after the continue statement and keeps looping.
  • 18. Nested loop • Nested loop is a loop that occurs in another loop. • When we nest one loop within another, the outer loop takes control of the total number of complete repetition of the inner loop. The program shown below demonstrates the use of nested loops.
  • 19. Exit Criteria • Exit criteria is defined as a condition that must be met before completing a specific task. • It is a set of conditions that must exist before you can declare a program to be complete. • Exit criteria is one of the most important components while defining a loop. • As without an exit criterion, the program tends to enter in an infinite loop. • These criteria differ from program to program as per the requirement.
  • 20. Chapter 3: Functions in depth At the end of this chapter, you will understand functions in programming. You will have a better understanding of functions. You will know: • How using functions helps us? • Create and use functions in block coding
  • 21. What are functions? • If there is a block of code that you want your computer to repeat lots of times, then you might want to use a function • A function is a block of code made up of a set of steps that results in a single specific action
  • 22. Advantages of using functions Increases readability By making code organized and easy to understand Code reusability increases Redundant code is removed and replaced by functions
  • 23. Parameters and return types Function to calculate area of a circle • Variables accepted by a function to perform a set of actions defined in its body are called function parameters • Type of value returned by a function are called return types Return type Parameter
  • 24. Functions called within another function • One or more functions can be defined within a program. • These functions can then be called from other functions in the same program. Function to calculate sum of area & perimeter of a square by calling other functions
  • 25. Chapter 4: Programming with arrays • In this chapter we will see how we can work with large set of data in programming. • What are arrays and its practical implementation in computers? • Overview of collections
  • 26. Arrays in programming What are arrays: • Arrays in programming are a collection of similar data type variables. • Arrays do not support different data types in same collection. • Arrays improve readability of code by using a single variable for a large set of data. Limitations of using arrays are: • You can only store variables with homogenous data types in an array. • Arrays are always static and fixed in length and size. • The variables in an array are always ordered sequentially with index starting with 0.
  • 27. Arrays in programming Shown above is a character array of length 7
  • 28. Arrays in programming Suppose we have three variables to store three different colors: Color1 = “red” Color2 = “green” Color3 = “blue” We can use an array to store three different colors in a single variable. Shown below is how we can store three different colors using a single variable using an array: Color = [“red”, ”green”, “blue”]
  • 29. Arrays in programming Suppose we have an array named Color. Color = [“red”, ”green”, “blue”] • We can access the elements of the Color array the following way: Color[0] = “red” Color[1] = “green” Color[2] = “blue” • We can alter the value at an index of an array by assigning it a new value as shown below: Color[1] = “yellow”
  • 30. Sorting an array Sorting an array can be defined as ordering the elements in the array either in ascending or descending order. (SORT) In the above scenario, we are sorting the elements of an integer array in ascending order. Some of the algorithms used to sort an array are: • Bubble sort • Quick sort 1 5 4 3 2 1 2 3 4 5
  • 31. Sorting an array using bubble sort Bubble sort is a method of sorting that works by repeatedly swapping adjacent elements if they are in incorrect order. Let us consider a set of numbers 1, 5, 4, 3, 2; that needs to be sorted in ascending order. We use an integer array of length 5 to store these numbers. 1 5 4 3 2
  • 32. Sorting an array using bubble sort 1 5 4 3 2 • To start with, we compare the first two numbers and find out which one is greater. • We start with 1 and 5. Since 5 is already greater than 1, no change is made • Then we compare the numbers 5 and 4 • Since 5 is greater than 4, we will swap these two numbers. 1 5 4 3 2
  • 33. Sorting an array using bubble sort 1 4 5 3 2 • Next, we compare 5 and 3 • Since 5 is greater than 3, we will swap the two numbers. • Lastly, we compare 5 and 2 • Since 5 is greater than 2, we will swap these two numbers 1 4 3 5 2
  • 34. Sorting an array using bubble sort 1 4 3 2 5 • The list of the numbers is rearranged as follows: • Notice 5 is the largest number in this list and arranged at the last position. • We again start from the beginning and compare 1 with 4. Since 4 is greater than 1, no change is made. 1 4 3 2 5
  • 35. Sorting an array using bubble sort 1 4 3 2 5 • Then we compare the numbers 4 and 3 • Since 4 is greater than 3, we will swap these two numbers. • Next, we compare 4 and 2 • Since 4 is greater than 2, we will swap the two numbers. 1 3 4 2 5
  • 36. Sorting an array using bubble sort 1 3 2 4 5 • Lastly, we compare 4 and 5. Since, 5 is greater than 4, no change is made. • We again start from the beginning and compare 1 with 3. Since 3 is greater than 1, no change is made. 1 3 2 4 5
  • 37. Sorting an array using bubble sort 1 3 2 4 5 • Then we compare the numbers 3 and 2 • Since 3 is greater than 2, we will swap these two numbers. • Next, we compare the numbers 3 and 4. Since 4 is greater than 3, no change is made. 1 2 3 4 5
  • 38. Sorting an array using bubble sort 1 2 3 4 5 • We again start from the beginning and compare 1 with 2. Since 2 is greater than 1, no change is made. • The numbers are now all sorted in ascending order. • The exercise of sorting is done until no more numbers need to be swapped. 1 2 3 4 5
  • 39. Chapter 5: Advanced Sequencing • In this chapter we will learn what is advanced sequencing in programming. • What is a sequence? • Why is sequencing important in programming? • Sequencing with Loops and Conditions
  • 40. What is a Sequence? • A series of actions performed in a specific order is called sequence. • We follow sequence in many things that we do on daily basis. • Our daily routine is also a series of actions that we perform like: • Wake up • Have breakfast • Take a shower • Go to School • Attend classes • Come back home • Do homework • Have dinner • Go to bed • However, this routine might vary from person to person but for a specific person this might be the routine.
  • 41. What is a Sequence? (Contd.) • An algorithm is a set of steps to solve a problem. • A program is written based on an algorithm to get the required result. • Sequencing in algorithms is arranging the steps of an algorithm in a correct sequence so that we get the correct outcome. • Sequencing is important in algorithms. • If the steps of an algorithm are not in correct sequence, we will not get the required result.
  • 42. Algorithm to calculate sum of two numbers • Consider a simple algorithm to calculate the sum of two numbers. • The steps of this algorithm would look like as shown in the flowchart on the left. • However, we need to note that if we swap 2 steps and add Num1 and Num2 before taking Num1 as the input, the program will not give the required result or give an error. • This is called as a bug.
  • 43. Sequencing with Loops and Conditions • Most programs consist of 3 main components – Sequence, Selections (Conditions) and Loops. • If we write a program to get all the numbers divisible by 3 between 0 and 100. The flowchart for the algorithm would be as shown in the image on the left. • In this example, we see that this program has a sequence of conditions and loops.