SlideShare une entreprise Scribd logo
1  sur  71
Télécharger pour lire hors ligne
ACM init() 
Day 3: November 5, 2014
Any questions? 
• Getting user input 
• Control flow: if, else, elsif, unless 
• Making comparisons: ==, !=, <, >, <=, >= 
• Boolean operators: &&, ||, !
Back to Mad Libs 
...
Simple Calculator 
Let's look at the code 
in Koding so you 
can actually see it.
Can we improve this? 
• This calculator is pretty inconvenient 
• User must be prompted often 
• Unfriendly interface 
• Only one calculation can be performed 
• Can this be improved?
Of course we can 
We can use 'loops' to make a much better calculator
Why is this better? 
• We can do more than one calculation 
• The user can type in a legitimate mathematical 
equation 
• There is less confusion and clutter in the interface
Loops 
• Loops are a great tool if you want to repeat an 
action 
• We are going to go over three types of loops: 
'while', 'until', and 'for'
While Loop 
A while loop checks to see if a certain condition is true, and 
while it is, the loop keeps running. As soon as the condition 
stops being true, the loop stops. 
while statement is true 
do something 
end
While example 
x = 1 
Output:
While example 
x = 1 
Output:
While example 
x = 1 
Output: 
1
While example 
x = 2 
Output: 
1
While example 
x = 2 
Output: 
1
While example 
x = 2 
Output: 
1 
2
While example 
x = 3 
Output: 
1 
2
While example 
x = 3 
Output: 
1 
2
While example 
x = 3 
Output: 
1 
2 
3
While example 
x = 4 
Output: 
1 
2 
3
While example 
x = 4 
Output: 
1 
2 
3
While example 
x = 4 
Output: 
1 
2 
3
A more fun example 
Let's go to koding.com and run it to see what it does!
Danger! 
We always have a condition that will make the loop end. 
What if we forget to update the condition? 
We might create something called an infinite loop. This loop 
will keep running forever, which is really bad.
Infinite Loop Output 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
AHHHHHHHHHHHHHHHHHHHHH 
...
Until Loop 
An 'until' loop will run until a set condition is met. Once the 
condition is met the loop stops running. 
until condition 
do something 
end
Until example 
x = 3 
Output:
Until example 
x = 3 
Output:
Until example 
x = 3 
Output: 
3
Until example 
x = 2 
Output: 
3
Until example 
x = 2 
Output: 
3
Until example 
x = 2 
Output: 
3 
2
Until example 
x = 1 
Output: 
3 
2
Until example 
x = 1 
Output: 
3 
2
Until example 
x = 1 
Output: 
3 
2 
1
Until example 
x = 0 
Output: 
3 
2 
1
Until example 
x = 0 
Output: 
3 
2 
1
Until example 
x = 0 
Output: 
3 
2 
1
What will this print out?
Answer 
0 
1 
2 
3 
4
A Brief Sidenote 
You might have seen that in loops I was using += and -= 
These are just shorthand.
For Loop 
A 'for' loop is used when we know how many times we'll be 
looping when we start. The loop will run however many times 
we tell it to. 
for variable in x...y 
do something 
end
For example 
x = 1 
Output:
For example 
x = 1 
Output: 
1
For example 
x = 2 
Output: 
1
For example 
x = 2 
Output: 
1 
2
For example 
x = 3 
Output: 
1 
2
For example 
x = 3 
Output: 
1 
2 
3
For example 
x = 4 
Output: 
1 
2 
3
For example 
x = 4 
Output: 
1 
2 
3
More about for loops 
• Notice that we typed in 'for num in 1...4' and 1, 2, 3 
was printed out 
• 4 was not printed out 
• That is because we typed in 1...4 
• How do we make the same loop print out 1, 2, 3, 4?
Inclusive and Exclusive 
Ranges 
• There are two different ways to write for loops in 
Ruby 
• If we type in 'for num in 1...4' it will print out 1, 2, 3 
(Exclusive) 
• If we type in 'for num in 1..4' it will print out 1, 2, 3, 4 
(Inclusive) 
• Notice the different numbers of '.'
What will this print out?
Answer 
1 
2 
3 
4 
5 
6 
7 
8 
9 
This is exclusive because 10 is not printed out.
What will this print out?
Answer 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
This is inclusive because 10 is printed out.
Looping Tools 
• Some loops we write are fairly complex and include 
multiple conditional statements 
• What if we want to skip an iteration of the loop 
depending on a condition? 
• There is a command to do this: the 'next' command 
• If a 'next' is reached Ruby will immediately 
advance to the next iteration of the loop
What will this print out?
Answer 
1 
3 
5 
7 
9 
This loop skipped if x was even and printed out x if it was odd.
Any questions on 
loops?
Arrays 
• An array is a new data type 
• In simplest terms, an array is a 
list 
• You can have an array of any 
type of variable: string, 
numbers, booleans, etc 
• Arrays work great for storing 
longs lists of variables!
Accessing Array Elements 
• Once we make our array we want to be able to use 
each individual element. To do this we can look at 
the index of each array element. 
• Each element of the array is assigned a number 
from 0...array.length-1 
• The first element gets index 0, the next gets index 
1, the one after that gets index 2, and so on.
Array indices 
What if we have the array {15, 27, 21, 13, 44, 17}? 
my_array = [15, 27, 21, 13, 44, 17]
Accessing Array Elements 
How do we actually get to a specific element of an array? Let's 
try to get the third number of the following array: 
The third number's index is 2. Therefore, to get the third 
element of this array we would type in 'my_array[2]'
General Array Access 
There is a general formula for accessing array elements: 
array_name[element# - 1] 
For example, if we have an array called 'string_array' and we 
want to print out the fourth element we would type: 
Will print out 'cheese'
Intro to Sorting 
What if we get a large array of numbers and we want to put 
them in ascending order? 
Ruby has a method for that! 
The '.sort!' method will rearrange elements of an array so they 
are in order!
Sort example 
But wait! These are numbers! What if we want to sort an array 
of strings?
Sorting String Arrays 
Calling '.sort!' on an array of strings will also work! The array 
will be sorted alphabetically.
What we did today 
• While Loops (Be careful about infinite loops!) 
• Until Loops 
• For Loops 
• Next 
• Arrays 
• Simple Sorting
Any questions before we 
get to the homework?
Homework: Fizzbuzz 
Write a program that prints the numbers from 1 to 100. For 
multiples of three print “Fizz” instead of the number and for the 
multiples of five print “Buzz”. For numbers which are multiples 
of both three and five print “FizzBuzz”. 
The next slide shows what the output should look like for the 
first 26 numbers. 
Remember, if you have any questions you can ask on the 
Facebook page!
Init() Lesson 3

Contenu connexe

Tendances

Py4inf 05-iterations
Py4inf 05-iterationsPy4inf 05-iterations
Py4inf 05-iterationskaran saini
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)karan saini
 
String handling session 5
String handling session 5String handling session 5
String handling session 5Raja Sekhar
 
How invariants help writing loops
How invariants help writing loopsHow invariants help writing loops
How invariants help writing loopsnextbuild
 
String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,8neutron8
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPKamal Acharya
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)Anwar Hasan Shuvo
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanKavita Ganesan
 
Java string handling
Java string handlingJava string handling
Java string handlingSalman Khan
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi Kant Sahu
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arraysphanleson
 

Tendances (20)

Py4inf 05-iterations
Py4inf 05-iterationsPy4inf 05-iterations
Py4inf 05-iterations
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
 
String handling session 5
String handling session 5String handling session 5
String handling session 5
 
Java Strings
Java StringsJava Strings
Java Strings
 
How invariants help writing loops
How invariants help writing loopsHow invariants help writing loops
How invariants help writing loops
 
String in java
String in javaString in java
String in java
 
Java String
Java StringJava String
Java String
 
String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,String Matching with Finite Automata,Aho corasick,
String Matching with Finite Automata,Aho corasick,
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
Strings
StringsStrings
Strings
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Python Loop
Python LoopPython Loop
Python Loop
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
finite automata
 finite automata finite automata
finite automata
 
adnoto
adnotoadnoto
adnoto
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
Python for loop
Python for loopPython for loop
Python for loop
 

En vedette (11)

ACM init() Day 6
ACM init() Day 6ACM init() Day 6
ACM init() Day 6
 
UCLA Geek Week - Claim Your Domain
UCLA Geek Week - Claim Your DomainUCLA Geek Week - Claim Your Domain
UCLA Geek Week - Claim Your Domain
 
UCLA ACM Spring 2015 general meeting
UCLA ACM Spring 2015 general meetingUCLA ACM Spring 2015 general meeting
UCLA ACM Spring 2015 general meeting
 
ACM init() Day 5
ACM init() Day 5ACM init() Day 5
ACM init() Day 5
 
Init() Lesson 2
Init() Lesson 2Init() Lesson 2
Init() Lesson 2
 
ACM init()- Day 4
ACM init()- Day 4ACM init()- Day 4
ACM init()- Day 4
 
ACM Teach - Hackathon Tips and Tricks - Spring 2014
ACM Teach - Hackathon Tips and Tricks - Spring 2014ACM Teach - Hackathon Tips and Tricks - Spring 2014
ACM Teach - Hackathon Tips and Tricks - Spring 2014
 
ACM General Meeting - Spring 2014
ACM General Meeting - Spring 2014ACM General Meeting - Spring 2014
ACM General Meeting - Spring 2014
 
Building a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground UpBuilding a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground Up
 
An Introduction to Sensible Typography
An Introduction to Sensible TypographyAn Introduction to Sensible Typography
An Introduction to Sensible Typography
 
Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)
 

Similaire à Init() Lesson 3 (20)

ACM init() Day 3
ACM init() Day 3ACM init() Day 3
ACM init() Day 3
 
Notes2
Notes2Notes2
Notes2
 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_Statement
 
Lesson flow charts 1
Lesson flow charts 1Lesson flow charts 1
Lesson flow charts 1
 
Init() day2
Init() day2Init() day2
Init() day2
 
C# loops
C# loopsC# loops
C# loops
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
 
While loop
While loopWhile loop
While loop
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
 
Mastering Python lesson 3a
Mastering Python lesson 3aMastering Python lesson 3a
Mastering Python lesson 3a
 
CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1CPAP.com Introduction to Coding: Part 1
CPAP.com Introduction to Coding: Part 1
 
ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1
 
Anatomy of the WordPress Loop
Anatomy of the WordPress LoopAnatomy of the WordPress Loop
Anatomy of the WordPress Loop
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
module 2.pptx
module 2.pptxmodule 2.pptx
module 2.pptx
 
Spreadsheets for developers
Spreadsheets for developersSpreadsheets for developers
Spreadsheets for developers
 
Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building Blocks
 
JAVA.pptx
JAVA.pptxJAVA.pptx
JAVA.pptx
 

Dernier

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 

Dernier (20)

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 

Init() Lesson 3

  • 1. ACM init() Day 3: November 5, 2014
  • 2. Any questions? • Getting user input • Control flow: if, else, elsif, unless • Making comparisons: ==, !=, <, >, <=, >= • Boolean operators: &&, ||, !
  • 3. Back to Mad Libs ...
  • 4. Simple Calculator Let's look at the code in Koding so you can actually see it.
  • 5. Can we improve this? • This calculator is pretty inconvenient • User must be prompted often • Unfriendly interface • Only one calculation can be performed • Can this be improved?
  • 6. Of course we can We can use 'loops' to make a much better calculator
  • 7. Why is this better? • We can do more than one calculation • The user can type in a legitimate mathematical equation • There is less confusion and clutter in the interface
  • 8. Loops • Loops are a great tool if you want to repeat an action • We are going to go over three types of loops: 'while', 'until', and 'for'
  • 9. While Loop A while loop checks to see if a certain condition is true, and while it is, the loop keeps running. As soon as the condition stops being true, the loop stops. while statement is true do something end
  • 10. While example x = 1 Output:
  • 11. While example x = 1 Output:
  • 12. While example x = 1 Output: 1
  • 13. While example x = 2 Output: 1
  • 14. While example x = 2 Output: 1
  • 15. While example x = 2 Output: 1 2
  • 16. While example x = 3 Output: 1 2
  • 17. While example x = 3 Output: 1 2
  • 18. While example x = 3 Output: 1 2 3
  • 19. While example x = 4 Output: 1 2 3
  • 20. While example x = 4 Output: 1 2 3
  • 21. While example x = 4 Output: 1 2 3
  • 22. A more fun example Let's go to koding.com and run it to see what it does!
  • 23. Danger! We always have a condition that will make the loop end. What if we forget to update the condition? We might create something called an infinite loop. This loop will keep running forever, which is really bad.
  • 24. Infinite Loop Output AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH AHHHHHHHHHHHHHHHHHHHHH ...
  • 25. Until Loop An 'until' loop will run until a set condition is met. Once the condition is met the loop stops running. until condition do something end
  • 26. Until example x = 3 Output:
  • 27. Until example x = 3 Output:
  • 28. Until example x = 3 Output: 3
  • 29. Until example x = 2 Output: 3
  • 30. Until example x = 2 Output: 3
  • 31. Until example x = 2 Output: 3 2
  • 32. Until example x = 1 Output: 3 2
  • 33. Until example x = 1 Output: 3 2
  • 34. Until example x = 1 Output: 3 2 1
  • 35. Until example x = 0 Output: 3 2 1
  • 36. Until example x = 0 Output: 3 2 1
  • 37. Until example x = 0 Output: 3 2 1
  • 38. What will this print out?
  • 39. Answer 0 1 2 3 4
  • 40. A Brief Sidenote You might have seen that in loops I was using += and -= These are just shorthand.
  • 41. For Loop A 'for' loop is used when we know how many times we'll be looping when we start. The loop will run however many times we tell it to. for variable in x...y do something end
  • 42. For example x = 1 Output:
  • 43. For example x = 1 Output: 1
  • 44. For example x = 2 Output: 1
  • 45. For example x = 2 Output: 1 2
  • 46. For example x = 3 Output: 1 2
  • 47. For example x = 3 Output: 1 2 3
  • 48. For example x = 4 Output: 1 2 3
  • 49. For example x = 4 Output: 1 2 3
  • 50. More about for loops • Notice that we typed in 'for num in 1...4' and 1, 2, 3 was printed out • 4 was not printed out • That is because we typed in 1...4 • How do we make the same loop print out 1, 2, 3, 4?
  • 51. Inclusive and Exclusive Ranges • There are two different ways to write for loops in Ruby • If we type in 'for num in 1...4' it will print out 1, 2, 3 (Exclusive) • If we type in 'for num in 1..4' it will print out 1, 2, 3, 4 (Inclusive) • Notice the different numbers of '.'
  • 52. What will this print out?
  • 53. Answer 1 2 3 4 5 6 7 8 9 This is exclusive because 10 is not printed out.
  • 54. What will this print out?
  • 55. Answer 1 2 3 4 5 6 7 8 9 10 This is inclusive because 10 is printed out.
  • 56. Looping Tools • Some loops we write are fairly complex and include multiple conditional statements • What if we want to skip an iteration of the loop depending on a condition? • There is a command to do this: the 'next' command • If a 'next' is reached Ruby will immediately advance to the next iteration of the loop
  • 57. What will this print out?
  • 58. Answer 1 3 5 7 9 This loop skipped if x was even and printed out x if it was odd.
  • 60. Arrays • An array is a new data type • In simplest terms, an array is a list • You can have an array of any type of variable: string, numbers, booleans, etc • Arrays work great for storing longs lists of variables!
  • 61. Accessing Array Elements • Once we make our array we want to be able to use each individual element. To do this we can look at the index of each array element. • Each element of the array is assigned a number from 0...array.length-1 • The first element gets index 0, the next gets index 1, the one after that gets index 2, and so on.
  • 62. Array indices What if we have the array {15, 27, 21, 13, 44, 17}? my_array = [15, 27, 21, 13, 44, 17]
  • 63. Accessing Array Elements How do we actually get to a specific element of an array? Let's try to get the third number of the following array: The third number's index is 2. Therefore, to get the third element of this array we would type in 'my_array[2]'
  • 64. General Array Access There is a general formula for accessing array elements: array_name[element# - 1] For example, if we have an array called 'string_array' and we want to print out the fourth element we would type: Will print out 'cheese'
  • 65. Intro to Sorting What if we get a large array of numbers and we want to put them in ascending order? Ruby has a method for that! The '.sort!' method will rearrange elements of an array so they are in order!
  • 66. Sort example But wait! These are numbers! What if we want to sort an array of strings?
  • 67. Sorting String Arrays Calling '.sort!' on an array of strings will also work! The array will be sorted alphabetically.
  • 68. What we did today • While Loops (Be careful about infinite loops!) • Until Loops • For Loops • Next • Arrays • Simple Sorting
  • 69. Any questions before we get to the homework?
  • 70. Homework: Fizzbuzz Write a program that prints the numbers from 1 to 100. For multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”. The next slide shows what the output should look like for the first 26 numbers. Remember, if you have any questions you can ask on the Facebook page!