This document provides an overview of programming loops and different types of loops. It discusses while loops and for loops. While loops repeat code until a condition is met, and for loops iterate over a sequence a set number of times. It provides examples of infinite loops and how to end loops using break or changing conditions. Different types of loops are demonstrated including condition-controlled and count-controlled loops. Activities are included to practice different loop structures.
A while loop in C programming
repeatedly executes a target
statement as long as a given
condition is true.
A while loop in C programming
repeatedly executes a target
statement as long as a given
condition is true.
LESSON 3A. INTRODUCTION TO ITERATION: LOOPS, TRACE TABLES, WHILE LOOPS
Introduction to Iteration and loops. The theory behind loops and how they work. Create and adapt programs using loops. Intro to the random number generator. Learn about trace tabling (white box testing). Example of a trace table and dry run. Wonders of the Fibonacci sequence. Examples of Iteration in game design. Focus on While loops. Challenges, tasks (with solutions), suggested videos, big ideas discussion and research and HW included. Introducing Ada Lovelace and Charles Babbage.
A while loop in C programming
repeatedly executes a target
statement as long as a given
condition is true.
A while loop in C programming
repeatedly executes a target
statement as long as a given
condition is true.
LESSON 3A. INTRODUCTION TO ITERATION: LOOPS, TRACE TABLES, WHILE LOOPS
Introduction to Iteration and loops. The theory behind loops and how they work. Create and adapt programs using loops. Intro to the random number generator. Learn about trace tabling (white box testing). Example of a trace table and dry run. Wonders of the Fibonacci sequence. Examples of Iteration in game design. Focus on While loops. Challenges, tasks (with solutions), suggested videos, big ideas discussion and research and HW included. Introducing Ada Lovelace and Charles Babbage.
In this lecture we will discuss about another flow control method – Loop control.
A loop control is used to execute a set of commands repeatedly
The set of commands is called the body of the loop
MATLAB has two loop control techniques
Counted loops - executes commands a specified number of times
Conditional loops - executes commands as long as a specified expression is true
In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached. Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number.
Python programming language provides the following types of loops to handle looping requirements:
1. While
2. Do While
3. For loop
Python provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition-checking time.
Complete Course Available at: https://github.com/Ebad8931/PythonWorkshop
Basic Concepts of Loops and Conditional Statements in Python are introduced in the presentation. Also covers How to get input from the Console and includes interactive Problems.
CMIS 102 Hands-On Lab
// Week 4
Overview:
This hands-on lab allows you to follow and experiment with the critical steps of developing a program including the program description, analysis, test plan, design (using both flow chart and pseudocode visualization), and implementation with C code. The example provided uses sequential, selection and repetition statements.
Program Description:
This program will calculate the sum of 10 integers. The program will ask the user to 10 integers. If the sum of the numbers is greater than 1000, a message is printed stating the sum is over 1000. The design step will include both pseudocode and flow chart visualization.
Analysis:
I will use sequential, selection and repetition programming statements.
I will define three integer numbers: count, value, sum. Count will store how many times values are entered to make sure we don’t exceed 10 values. Value will store the input integer and sum will store the running sum.
The sum will be calculated by this formula:
sum = sum + value
For example, if the first value entered was 4 and second was 10:
sum = sum + value = 0 + 4
sum = 4 + 10 = 14
Values and sum can be input and calculated within a repetition loop:
while count <10
Input value
sum = sum + value
End while
The additional selection statement will be of this form:
If sum > 1000 then
print "Sum is over 1000"
End If
Test Plan:
To verify this program is working properly the input values could be used for testing:
Test Case
Input
Expected Output
1
value=1
value=1
value=1
value=0
value=1
value=2
value=0
value=1
value=3
value=2
Sum = 12
2
value=100
value=100
value=100
value=100
value=100
value=200
value=200
value=200
value=200
value=200
Sum = 1200
Sum is over 1000.
3
value=-100
value=-100
value=-200
value=0
value=200
value=100
value=0
value=200
value=-300
value=-200
Sum = -400
Pseudocode:
// This program will calculate the sum of 10 integers.
// Declare variables
Declare count, value, sum as Integer
//Initialize Counter, Sum to 0
Set count=0
Set sum = 0
// Loop through 10 integers
While count < 10
Print “Enter an Integer”
Input value
sum = sum + value
count=count+1
End While
// Print results and messages
Print “Sum is “ + sum
If (sum > 1000)
Printf “Sum is over 1000”
End if
Flow Chart:
C Code
The following is the C Code that will compile in execute in the online compilers.
// C code
// This program will calculate the sum of 10 integers.
// Developer: Faculty CMIS102
// Date: Jan 31, 2014
#include <stdio.h>
int main ()
{
/* variable definition: */
int count, value, sum;
/* Initialize count and sum */
count = 0;
sum = 0;
// Loop through to input values
while (count < 10)
{
printf("Enter an Integer\n");
scanf("%d", &value);
sum = sum + value;
count = count + 1;
}
printf("Sum is %d\n " , sum );
if (sum >1000)
printf("Sum is over 1000\n");
return 0;
}
Setting up the code and the input parameters in ideone.com:
Note the input integer.
In this lecture we will discuss about another flow control method – Loop control.
A loop control is used to execute a set of commands repeatedly
The set of commands is called the body of the loop
MATLAB has two loop control techniques
Counted loops - executes commands a specified number of times
Conditional loops - executes commands as long as a specified expression is true
In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached. Typically, a certain process is done, such as getting an item of data and changing it, and then some condition is checked such as whether a counter has reached a prescribed number.
Python programming language provides the following types of loops to handle looping requirements:
1. While
2. Do While
3. For loop
Python provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition-checking time.
Complete Course Available at: https://github.com/Ebad8931/PythonWorkshop
Basic Concepts of Loops and Conditional Statements in Python are introduced in the presentation. Also covers How to get input from the Console and includes interactive Problems.
CMIS 102 Hands-On Lab
// Week 4
Overview:
This hands-on lab allows you to follow and experiment with the critical steps of developing a program including the program description, analysis, test plan, design (using both flow chart and pseudocode visualization), and implementation with C code. The example provided uses sequential, selection and repetition statements.
Program Description:
This program will calculate the sum of 10 integers. The program will ask the user to 10 integers. If the sum of the numbers is greater than 1000, a message is printed stating the sum is over 1000. The design step will include both pseudocode and flow chart visualization.
Analysis:
I will use sequential, selection and repetition programming statements.
I will define three integer numbers: count, value, sum. Count will store how many times values are entered to make sure we don’t exceed 10 values. Value will store the input integer and sum will store the running sum.
The sum will be calculated by this formula:
sum = sum + value
For example, if the first value entered was 4 and second was 10:
sum = sum + value = 0 + 4
sum = 4 + 10 = 14
Values and sum can be input and calculated within a repetition loop:
while count <10
Input value
sum = sum + value
End while
The additional selection statement will be of this form:
If sum > 1000 then
print "Sum is over 1000"
End If
Test Plan:
To verify this program is working properly the input values could be used for testing:
Test Case
Input
Expected Output
1
value=1
value=1
value=1
value=0
value=1
value=2
value=0
value=1
value=3
value=2
Sum = 12
2
value=100
value=100
value=100
value=100
value=100
value=200
value=200
value=200
value=200
value=200
Sum = 1200
Sum is over 1000.
3
value=-100
value=-100
value=-200
value=0
value=200
value=100
value=0
value=200
value=-300
value=-200
Sum = -400
Pseudocode:
// This program will calculate the sum of 10 integers.
// Declare variables
Declare count, value, sum as Integer
//Initialize Counter, Sum to 0
Set count=0
Set sum = 0
// Loop through 10 integers
While count < 10
Print “Enter an Integer”
Input value
sum = sum + value
count=count+1
End While
// Print results and messages
Print “Sum is “ + sum
If (sum > 1000)
Printf “Sum is over 1000”
End if
Flow Chart:
C Code
The following is the C Code that will compile in execute in the online compilers.
// C code
// This program will calculate the sum of 10 integers.
// Developer: Faculty CMIS102
// Date: Jan 31, 2014
#include <stdio.h>
int main ()
{
/* variable definition: */
int count, value, sum;
/* Initialize count and sum */
count = 0;
sum = 0;
// Loop through to input values
while (count < 10)
{
printf("Enter an Integer\n");
scanf("%d", &value);
sum = sum + value;
count = count + 1;
}
printf("Sum is %d\n " , sum );
if (sum >1000)
printf("Sum is over 1000\n");
return 0;
}
Setting up the code and the input parameters in ideone.com:
Note the input integer.
About
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
Technical Specifications
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
Key Features
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface
• Compatible with MAFI CCR system
• Copatiable with IDM8000 CCR
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
Application
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Dr.Costas Sachpazis
Terzaghi's soil bearing capacity theory, developed by Karl Terzaghi, is a fundamental principle in geotechnical engineering used to determine the bearing capacity of shallow foundations. This theory provides a method to calculate the ultimate bearing capacity of soil, which is the maximum load per unit area that the soil can support without undergoing shear failure. The Calculation HTML Code included.
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)MdTanvirMahtab2
This presentation is about the working procedure of Shahjalal Fertilizer Company Limited (SFCL). A Govt. owned Company of Bangladesh Chemical Industries Corporation under Ministry of Industries.
2. Starter activity
1. Use the internet to get an accurate, programming description of
programming loops
2. Write your answer on a post-it note
3. Repetition – code embedded within a loop repeats until a condition is
reached
Terminology Challenge :
What type of programming construct is this? s......, s........, i........
Iteration
3. Loops....
• Iteration
The act of repeating a process, either:
• Until a condition equates to True, or
• For a certain number of times
4. Learning objects
• What?
• Gain an understanding of different types of programming loops and how
these might be applied
• Understand the difference between count controlled and condition
controlled loops
• Identify how we end loops
• Apply iteration programming constructs to realistic scenarios
• Why?
• All programs include iteration of some form. Iteration is a fundamental
programming concept and needs to be implemented into your NEA.
5. Why include loops in our code?.......
• There may be times where we want to run some code over and over
again
• For example, entering a new password where the two inputs don’t
match.
6. Two types of Loops
Loop type Description
WHILE loop Uses a condition to determine whether or not the statements
should be executed. The statements are repeated until the
result of the condition equates to true.
e.g. while True
while goes <=10
while goes == True
FOR loop
Uses a variable as a counter; the counter is incremented each
time statements are repeated until it reaches a pre-set value.
For loops iterate over a given sequence
e.g. 5 times – for i in range(5)
My_friends = [‘Sarah’, ‘Jane’, ‘Sam’]
For i in range(0, len(My_friends))
7. Infinity ……..or not?
• Infinite loop – (forever) is where the program will loop continuously.
• Complete ‘Activity 1 – Infinite or not’
8. Task 1:
Explore how these loops
run and why some are
infinite and others aren’t
For each, tick the
appropriate column.
10. Investigation
1. Investigate these
examples of iteration by
turning the pseudocode
into Python code.
• Iteration
The act of repeating a
process, either:
• Until a condition evaluates
to True or False, or
• Until a condition has
looped a certain number of
times
:
:
:
for x in range(1, y)
:
‘ ’
( )
12. While loop ……Boolean expression TRUE
while True # means loop forever.
The while statement executes the loop body while the
Expression evaluates to (boolean) "true"
Infinite loop……how do we get
out of the loop?
Deconstruct : Boolean has one of two possible values true and false
13. Task – a loop to check length of a user’s name
while True
player input('Please enter your name: ')
IF player.length >=3 and player.length <=6:
OUTPUT "Accepted"
break
ELSE
OUTPUT "Please enter a username between 3 and 6 characters“
ENDIF
endwhile
len(player)
Using the Boolean
expression - ‘break’ will stop
the loop.
15. Adaption of a Boolean loop
• We could assign the Boolean value to a variable.
• Then, test that variable to see if it is True or False.
16. Alternative condition controlled
Boolean while loop – use of a variable
happy = False
While happy equals False
happy input “Are you happy?”
if answer equals ‘yes’ then
output “You are happy”
happy = True
else if answer equals ‘no’ then
output “You are not happy”
happy = False
Extension:
Add an else statement to capture any other answer and set happy to False
Activity –
1. recreate this pseudocode as Python code
2. Alter the Boolean and replace with ‘yes’, ‘no’
17. Code
Infinite loop
Having a variable means
we can end the loop
Or keep it looping
Define the while
loop variable
18. Adapted – a loop to check length of a user’s
name
tries = True
while tries == True
player input('Please enter your name: ')
IF player.length >=3 and player.length <=6:
output "Accepted"
tries = False
ELSE
output "Please enter a username between 3 and 6 characters“
tries = True
end if
endwhile
19. Summary – glossary
• While loops are known as condition controlled iteration command
• A condition-controlled loop is used when it is not known how many
times iteration will need to occur.
20. Analysis – alternative while loop – count
controlled
• Analyse the two algorithms – re-create them in Python
• Why does one loop infinitely and the other one doesn’t?
Counter controlled condition
This program will increment
by 1 after every output
and eventually the condition
will evaluate to False and the
loop will end
Using < means the loop will
automatically end when it
reaches 9
Infinite
21. Task – adapt your ‘happy’ Code
Set the variable to 10
which means the
condition no longer
equates to TRUE – it
equates to FALSE
Define the while
loop variable
10
<10:
happy +1
Increment happy
0
Declare the variable & assign the value 0
22. Activity
• Using a while loop, create an entry login program that re-loops 4
times if the username entered is not equal to ‘1234’
• If the correct answer e..g ‘1234’ is entered, you need to break out of
the loop. To do this, choose a condition that is not less than or equal
to 4
24. Activity - Adapt the pseudocode
• Create a program that:
• Gets username
• Gets password1
• Gets password2
• Checks to see if they match
• And exits if they do
• Re-loops if they don’t
• Only re-loops 4 times
26. For loops – glossary
• Also a count controlled loop
• A count-controlled loop is used when the number of iterations to
occur is already known.
27. Another count controlled loop
A For loop iterates over a sequence of numbers
using the "range" function.
x =1, 2, 3, 4, 5
Once the program reaches the end of the range, the loop
ends.
is used when the number of iterations to occur is already known.
Stepper variable
30. Next for loop...
• We don’t need a variable
for the start value
Extension:
• Add an if statement to jut
return the output if the
stepper contains 5.
01 for x = 1 to 21
02 OUTPUT ‘Number output is : x)
31. Plenary
What are the two types of Loops?
Loop type Description
WHILE loop Repeats a statement or group of statements while a given
condition is TRUE.
FOR loop
Executes a sequence of statements multiple times, until the
statement has been satisfied.
For loops iterate over a given sequence
32. Password check - The pseudocode for this
algorithm might look like this:
password = "blank“
WHILE password does not equal "ilovecomputing" THEN
INPUT password
OUTPUT "Password incorrect“
Recreate this as code
Extension – adapt the program – use a while loop to re-loop if password 1
and password2 are not the same
33. Exploring for loops’’
i Output i End of loop
1 1 False
3 3
5 5
9 9
11 11
13 13
15 15
17 17
19 19 True
i is 21 next – loop ends at 20
Review the code and complete the trace table.
34. Nested for loop can be used to iterate a
certain number of times
Extension:
1. Change the variables to something more meaningful
2. Explain what the purpose is of the nested loop.
35. Dice game
Import random
Create a dice game with
7 rounds
2 players roll per round
e.g. for player in range(1,3):
Dice roll is generated using random.randint(1,6)
e.g. dice_roll = random.randint(1,6)
print(‘You rolled……………. ‘, dice_roll)
Add up the dice roll score
e.g.
if player ==1:
p1score = p1score+dice_roll
else:
p2score = p2score+dice_roll
36. Dice game
Review the code:
What does the code
execute?
Extension:
Adapt this code so that
it has one while loop
and one for loop.
37. ART WORK
• A4 sheet
• Difference between count controlled and condition controlled lops
• Examples if infinite loops
• Example of the same loops but no longer infinite
39. Revision
Define Selection
A condition is used to decide which instruction to execute next.
Score +=1 is only run if the IF condition is equates to true.
Define Iteration
A set of instructions that is repeated until a condition is met
e.g. The code in the repeat loop will be run several times (until
Position = 100).
40. Revision continued
String concatenation
• Joining together two or more values – could be strings, integers,
variables.
Casting
• Converting from one data type to another e.g. str(score) or
int(input())
41. Revision continued
Count controlled loop
For loop
• Repeat the code inside of them for a fixed number of times. The
number of times the code repeats will depend on the initial value,
end value and the step count.
• E.g. for i in range (0, 10, 2)
42. Revision continued
Condition controlled loop
• while loop
• Ctrolled by a condition at the start of a loop. Kepps going while the condition
wquates to true . Never run the ode inside og the loop if the condition is initially
false you get an infinite condition is always true.
e.g. tries = 4
While tries > 0: #this initially equates to true
if ans = ‘correct’:
tries = 0 #the condition will NOT equate to true if tries = 0
else:
tries = tries – 1 # the condition will equate to true if tries = 3,2,1
44. PREDICT
Review this code and predict what will happen
answer = input("Is computer science your favourite subject? [Y/N] ")
agree = answer == "Y" #the logical statement answer == "Y" is evaluated, and the result of this is
assigned to the agree variable.
while not agree:
print("wrong answer")
answer = input("Is computer science your favourite subject? [Y/N] ")
agree = answer == "Y"
print("You're right; computer science is the best")