The Learn Programming AcademyThe Learn Programming Academy
  • Home
  • Courses
  • Meet The Team
  • Blog
  • About Us
  • Contact
  • Home
  • Courses
  • Meet The Team
  • Blog
  • About Us
  • Contact

Programming

  • Home
  • Blog
  • Programming
  • Sequence, Selection, and Iteration – The Building Blocks of Programming Languages

Sequence, Selection, and Iteration – The Building Blocks of Programming Languages

  • Posted by Frank J. Mitropoulos
  • Categories Programming
  • Date August 31, 2018
  • Comments 33 comments

There are many, many programming languages available that allow us to program computers to solve all kinds of problems. There are scripting languages, systems languages, web programming languages, dynamic languages, object-oriented languages, functional languages, and the list goes on and on. 

But did you know that all programming languages have 3 elements in common? Three very simple elements that give us the power to implement solutions to extremely complex problems. 

These 3 elements are: 

  •  Sequence 
  • Selection 
  • Iteration 

Sure, many programming languages have many other complex features. Some are ‘easy’ to learn and others more difficult to learn. 

In this post I’d like to talk about each one of these elements and build a very simple C++ program that uses all of them. C++ is one of those languages that is considered very difficult to learn because it is very complex. 

Let’s talk about each of these elements individually and we’ll write a simple C++ program along the way that uses all of them. We’ll keep this example simple and I’m sure you will be able to follow along. 

If you’d like to follow along by typing or copying/pasting the code below, you can do so without installing any software at all. Simply point your favorite browser to https://www.onlinegdb.com/  and select C++17 from the dropdown list at the upper right.

Then delete the text in the online editor window and type or copy/paste the code we’ll write along the way. 

When you are ready to run the program, simply click on the green Run button at the top of the screen. 

If you see any errors, then double check that you entered the code exactly as shown and try it again. Once the program runs, you will be able to enter data and see output at the bottom of the screen.

So, what are these 3 elements all about?

It’s actually very simple. In order to solve problems with any programming language, we write code that tells the computer what operations to execute and in what order. The order must be very specific – remember the computer is not very smart – it simply follows our instructions. These operations make up what is called an algorithm. Just a fancy word that describes a set of operations that solves a specific problem. You can think of this very much like a cooking recipe. If you follow the recipe exactly, you will end up with the produce of that recipe.

Sequence, Selection, and Iteration are the basic elements that we use to tell the computer what to do. The code will definitely look different depending on the programming language we use, but the algorithm will be the same.

So let’s describe these elements:

  • Sequence– the order we want the computer to execute the instructions we provide as programmers. For example, do this first, then do this, then do that, and so forth.
  • Selection– selecting which path of an algorithm to execute depending on some criteria. For example, if you passed a class in school, then we execute the operations that clap and cheer and play a song. But if you didn’t pass the class, then maybe we would say, “Better luck next time, hang in there!”
  • Iteration– looping or repeating. Many times, we want to be able to repeat a set of operations a specific number of times or until some condition occurs.

That’s it, these 3 super simple elements give us the ability to write programs that solve problems. When we put them together we can create programs that are very complex such as operating systems, game engines, compilers, anything! In fact, with just Sequence, Selection, and Iteration we can implement any algorithm.

Read that again! Any algorithm! That’s a very powerful place to be!!

Alright, let’s write some C++ code together.

Sequence 

Let’s start with Sequence. Most programming languages simply execute instructions one after another as they are read – much like reading a recipe or a book. 

Here’s a simple C++ program that prompts the user to enter their age and then reads what they type in on the keyboard into a variable and then displays “Bye.” to the display console. 

#include <iostream> 
using namespace std;   
  
int main() {     
   int age {0};  
       
   cout << "Enter your age: ";
   cin >> age;     
   cout << "Bye.";  
}

All programming language have structure and syntax that we have to follow so that the program is legal, but that’s not important right now. So don’t worry about the details of the code, let’s focus on the big picture . The code on lines 7, 8, and 9 are what we are interested in. What’s important is that you can see the order or Sequence of the operations:

  1. We prompt the user to enter their age
  2. We read the integer they entered and store it in a variable named age
  3. We display “Bye.“

Run this program several times and enter different ages. Make sure you enter a whole number since that’s what the program expects.

Pretty boring right? Yes, but the computer is doing exactly what we are telling it to do.

That was pretty easy. But if we were only limited to Sequence our programs would be very limited indeed. We have to have a way to make decisions based on certain conditions.

Selection 

Now let’s modify this program so that it can decide whether a person can vote! We’ll assume that a person must be 18 years or older to vote. 

So, let’s see how Selection works. 

We’ll add logic so that if the age entered is 18 or greater then the program will display, “You can vote.” and if the age entered is less than 18 then the program will display, “Sorry, you can’t vote.”. 

See the selection, we are selecting which operations to execute depending on what the value of age is. Pretty simple and useful too! 

Let’s do it. 

#include <iostream> 
using namespace std;   
  
int main() {     
   int age {0};
           
   cout << "Enter your age: ";
   cin >> age;      
  
   if (age >= 18)
      cout << "You can vote." << endl;     
   else
      cout << "Sorry, you can't vote yet." << endl;      
  
   cout << "Bye.";    
}

Notice that on line 10 we added an if statement. If the expression in the parentheses is true then we display, “You can vote.“ 

But what if the person’s age is not greater than 18? That’s where the else on line 12 comes in. In this case we display, “Sorry, you can’t vote yet.“. We made a yes/no or true/false decision and we selected the operations to execute based on that decision. 

That was pretty easy. Notice how we combined Sequence and Selection to make our program more useful. 

Iteration 

But, suppose there are 3 voters and we would like to ask each of them what their age is. We could copy and paste the code 3 times, right? Sure, but if we had 1000 voters the program would grow very large and become very hard to follow and change. That’s where Iteration comes in. We can use a loop to iterate 3 times. Programming languages all allow Iteration and many provide multiple ways to iterate. Let’s do it using a very simple for loop in C++. 

So let’s rewrite the code one final time. 

#include <iostream> 
using namespace std;   
  
int main() {     
   int age {0};
  
   for (int count = 1; count <= 3; count++) {  
            
      cout << "Enter your age: ";
      cin >> age;     
         
      if (age >= 18)          
         cout << "You can vote." << endl;        
      else          
        cout << "Sorry, you can't vote yet."<< endl;               
   }       
   
   cout << "Bye.";    
}

Notice that our original code is still there, we haven’t changed it. But we now added a new operation on line 7 that tells the program to count from 1 to 3 and execute everything from lines 9-15 that many times. 

So, now we will be asked to enter the ages for 3 voters and each time we’ll know whether a voter can vote or note. Pretty cool! 

Now our program is using Sequence to provide the order in which we execute our instructions. Selection to allow decisions to be made and Iteration to loop or repeat our instructions as many times as we need. 

Congratulations!

You have just learned the 3 elements that all programming languages must support and you wrote a simple program in one of the most complex programming languages used today, C++.

We can combine these 3 simple elements to implement any algorithm – now that’s power!

Of course, there is much more to programming. As our problems grow larger and more complex, we need other elements to help us deal with the complexity. Some of these elements are classes, methods, functions, threads, and many others.

But remember, the core elements of programming are the simple, Sequence, Selection, and Iteration elements that we learned about – that’s what it’s all about!

Ready to learn more?  Take a look at my Beginning C++ Programming – From Beginner to Beyond course.   It’s got close to 40 hours of video training and is aimed to make you a competent C++ programmer.  It’s one of the most popular courses of it’s kind on Udemy!

  • Share:
mm
Frank J. Mitropoulos
    Frank J. Mitropoulos, Ph.D. has over 30 years of experience in the computer science and information technology fields. Frank earned B.S., M.S., and Ph.D. degrees in Computer Science and has worked both in industry and academia. Frank has taught hundreds of courses at the university level to thousands of both undergraduate and graduate students in both face-to-face and online modes. Frank has also taught dozens of professional seminars for private corporations such as Motorola, IBM, GE, American Express, Logica, and many others. In addition to his teaching experience, Frank also worked for 12 years in industry doing both application, compiler, and operating systems development as well as  project management. In 2014, Frank was one of 13 professors invited by Apple to Apple Headquarters to discuss teaching strategies using Apple development tools.  In addition to being a full professor, trainer, and mentor. Frank is a consultant in the areas of Mobile Application Development, Game Design, and Machine Learning, and assists many technology companies as a technical interviewer for applicants seeking intermediate and senior level IT positions.  Frank enjoys developing computer games and has published games using gaming frameworks such as LibGDX, Corona, Cocos2DX, SpriteKit, SceneKit, Unity and Unreal. Frank's academic specialty is Aspect-Oriented programming and is a recognized expert in the field with dozens of publications in that area of computer science. Frank brings a wealth of both real-world and academic experience to make your learning experience productive, relevant, and fun!

    Previous post

    Do You Need A College/University Degree To Get A Programming Job?
    August 31, 2018

    Next post

    How Microsoft is Enabling Developers to Build Native iOS and Android Apps
    September 4, 2018

    You may also like

    The Complete JavaScript Course for Developers
    16 January, 2019

    Recently a student in my The Complete JavaScript Course for Developers course asked about the capabilities and limitations of JavaScript. Specifically, can JavaScript collect and handle data from a user in a mobile environment. The answer is an unequivocal “yes” …

    Java 11 has Arrived. Is it time to panic?
    27 September, 2018
    java11
    Kotlin – Java Developers Have a Head Start
    26 September, 2018
    Kotlin – Java Developers Have a Head Start

      33 Comments

    1. mm
      คลิปหลุด vk
      November 21, 2022
      Reply

      Thɑnks for one’s marvelous posting! I actually enjoyeɗ readіng it,
      you’re a greɑt author. I will make sure
      to bookmark your blog and ᴡiⅼl оften come baⅽk from now on. I want to encourage you to ultimately continue your great ᴡork, have a nice evening!

    2. mm
      หนัง av ญี่ปุ่น
      November 22, 2022
      Reply

      Ԝonderful goⲟds from you, man. I have understand your stuff previous to and you’re jսst extremely great.
      I really like what yⲟu have acquired here, really like what you are saying and the way in wһich
      you say it. You make it enjoyable and you still take care of to keep it sensible.
      I can’t wait to read far more from you. This iѕ actually a terrific web site.

    3. mm
      Teknik Industri
      July 21, 2023
      Reply

      What is the significance of selection in programming and how is it implemented?

    4. mm
      xnxx
      November 27, 2023
      Reply

      With haνin so much writtеn content do yoᥙ eѵer run into any problems of
      plagorism or copyright violatіon? My site has ɑ lot of completelʏ unique content I’ve
      either written mysеlf or outѕourced but it appears a lot of іt is popping it up all
      over the internet without my permission. Do you know any techniques tο help prevent content from being ripped off?

      I’d certainly appreciate it.

    5. mm
      doujin
      November 27, 2023
      Reply

      Wе’re a group of volunteers and opening a new scheme in ouг community.
      Your site offered ᥙs with valuable info to work on. You
      have done an imⲣгeѕsive job and our whole community ѡiⅼl be
      grateful to you.

    6. mm
      Hardcover Novel Book Printing
      November 27, 2023
      Reply

      Lock Channel Wiggle Wire
      Manga Illustrated Book Printing
      http://www.ctcglobal.org
      Novel Book Printing
      Steel Wiggle Wire
      Greenhouse Zig Zag Wiggle Wire
      Hardcover Novel Book Printing

    7. mm
      Plastic Pet Food Storage Bin
      November 27, 2023
      Reply

      R&S ZNA67 Vector Network Analyzers
      E5081A ENA Vector Network Analyzers
      Double Layer Drain Basket With Lid
      R&S ZNA50 Vector Network Analyzers
      reminders.chegal.org.ua
      Fruit And Vegetable Storage Container For Fridge
      Plastic Pet Food Storage Bin

    8. mm
      Disperse Blue SY-FGB
      November 28, 2023
      Reply

      http://www.alivecz.com
      Disperse Red F-3BL
      Disperse Red FB 200%
      Migrain Relief Hat
      Health Care
      Ice Gel Pack
      Disperse Blue SY-FGB

    9. mm
      หนังโป้ญี่ปุ่น
      November 28, 2023
      Reply

      Link exchange is nothing else except it is only placing
      the other person’s blog link on your page at suitable place and
      other perѕon will also do same for yoᥙ.

    10. mm
      doujin
      November 28, 2023
      Reply

      I just ⅼike the valuable informatіon you supply on your artіcleѕ.
      I ѡill bookmark ʏour weblog and test once more гight here
      frequently. I am faiгlү certain I will be toⅼd plenty of new stuff rigһt һere!
      Best of luck for the next!

    11. mm
      Precious Metal Brush Motor
      November 28, 2023
      Reply

      Analog Pressure Transmitter
      Hollow Cup DC Brushless Motor
      Carbon Brush Motor
      Calibrated Digital Pressure Gauge
      Low Pressure Switch
      http://www.alajlangroup.com
      Precious Metal Brush Motor

    12. mm
      Hex Head Double Thread Self Drilling Screw Silver Ruspert Coating
      November 28, 2023
      Reply

      Windows
      Folding Study Chair
      Hex Head Metal To Wood Screw Type 17
      duhockorea.net
      Roof Tiles
      Dry Wall Screw Black
      Hex Head Double Thread Self Drilling Screw Silver Ruspert Coating

    13. mm
      Led Headlight Conversion Kits
      November 29, 2023
      Reply

      Super Bright Led Headlight
      Helical Piles Screw Piles Foundation Ground Screw for Solar Panel
      Carbon Steel Stud Dowel Screw With Self Tapping Hanger Bolt For Wood
      Solar Panel Mount Helical Galvanized Ground Screw Pile
      Auto Led Headlight Bulb
      lioasaigon.vn
      Led Headlight Conversion Kits

    14. mm
      High Gain Waterproof Plate Signal Jammer Antenna
      November 29, 2023
      Reply

      Portable UAV Detection Precise Positioning Drone Detector
      Reusable Zip Ties
      http://www.alivecz.com
      Epoxy Coated Ss Banding
      Marker Nylon Cable Ties
      Outdoor Dual Band Plate Signal Jammer Antenna
      High Gain Waterproof Plate Signal Jammer Antenna

    15. mm
      What is an Gusset Bag
      November 29, 2023
      Reply

      Wholesale Cnc Turning Machine Parts
      Classification of computerized flat knitting machines
      Wholesale Precision Turning Parts
      Forging Automobile Parts
      What is Steering Rack
      yamaai.yamanoha.jp
      What is an Gusset Bag

    16. mm
      xxx porn
      November 29, 2023
      Reply

      I am ɑctuaⅼly glaⅾ to read this blog posts which inclսdes
      tons of usеful facts, thanks foг providing such data.

    17. mm
      doujin
      November 29, 2023
      Reply

      Hi tһere Dear, are yoᥙ really viѕiting this
      web page on a regulaг ƅasis, if so afterward you will dеfіnitely obtain good
      knowleԁge.

    18. mm
      xnxx
      November 29, 2023
      Reply

      Its sᥙch as you read mʏ mind! You appear to кnow so
      much approximately this, such as you wrote the e-book in it
      or something. I believe that you simply could do with a few percent to pressure the message hօuse a little bit, but other than that, that is great
      blog. An excellent read. I’ll definitely Ьe back.

    19. mm
      japanporn
      November 29, 2023
      Reply

      Excelⅼent blog here! Also your web site rather a lоt up fast!
      What host are you using? Can I get your affiliate ⅼink to your host?
      I wish my website loaded up as fast as yours lol

    20. mm
      หนังxxx
      November 30, 2023
      Reply

      Ηowdy! Do you know іf tһey make any plugins to help with Search Ꭼngine Optіmiᴢation? I’m tгying to get my blog to rank
      for some targeted keywords but I’m not seeіng very good
      gains. If you know of any please share. Thanks!

    21. mm
      Crane Oil Cooler
      November 30, 2023
      Reply

      Truck Charge Air Cooler
      Intercooler Kit Subaru Wrx Impreza
      10 Inch Car Audio Subwoofer Speaker
      Analysis of common specifications and uses of OPP Film
      licom.co.jp
      Outdoor Camping Inflatable Yurt Tent
      Crane Oil Cooler

    22. mm
      vklive
      December 1, 2023
      Reply

      It’s ɡoing to bе finish of mine day, except before finish I am reading this great post
      to increase my knowledge.

    23. mm
      cytology
      December 1, 2023
      Reply

      Ι do not even know how I stopped up here, but I thⲟught this post was good.
      I don’t гecognize who you are but definitely you’re
      going to a famօus blogցer if уou happеn to are not already.

      Chеers!

    24. mm
      โดจิน
      December 2, 2023
      Reply

      Нi! I’m at worк browsing your blog from my new iphone 3gs!
      Just wanted to say Ι love reading through yоur blog
      and look forward to all your posts! Keep up the superb work!

      • mm
        tim--admin
        December 7, 2023
        Reply

        Hi Gemma,

        Thank you for letting us know you enjoy Tim’s blog! You can also find more great and insightful vlogs on his YouTube channel.

        Have a nice day!

        Regards,
        Jp
        LPA Admin
        on behalf of Tim Buchalka

    25. mm
      หนังxxx
      December 15, 2023
      Reply

      Goоd blog you һave got herе.. It’s difficult to find higһ-quality writing
      like yours these days. I really appreciate individualѕ lіke you!
      Take caгe!!

    26. mm
      porn
      December 15, 2023
      Reply

      Іt’s in fact very complex in this full of activity life to listen news on Television, therefore I just use internet for that reason, and get the newest
      information.

    27. mm
      xxx
      December 16, 2023
      Reply

      It’s а pity you don’t have a donate button! I’d definitely donate to
      this brilliant blog! Ι guess for now i’ll settle for booк-marking and adding your RSS feed to
      my Google account. I look forᴡard to brand new updates and
      will talk about this site with my Ϝacеbook group.
      Chat soon!

    28. mm
      xxx japan
      December 16, 2023
      Reply

      Hi tһere everyone, it’s my first pay a quick visit at
      this site, and pіece of wгiting is aϲtually fruitful for me, keep up ⲣosting these types of content.

    29. mm
      Fan For Freezer
      December 16, 2023
      Reply

      HDPE Construction Safety Nets For Multiple Uses
      HDPE Fall Protection Construction Safety Catch Net
      Blower Fan
      Air Circulation Ventilation Fan
      Dark Green Balcony Windproof Fence Screen
      HDPE Balcony Fence Privacy Screen Anti-UV Wind Cover
      http://www.iretza.eus
      HDPE Balcony Sun Shade Net Fence Privacy Screen Cover
      Ventilator Fan Motor
      Ventilation Exhaust Fan
      Fan For Freezer

    30. mm
      Marine Anchor
      December 17, 2023
      Reply

      R&D Customized Marine Products
      Lithium 24v 100ah
      Boat Anchor
      tukurou.xsrv.jp
      48v 100ah Lithium Ion Battery Pack
      Powerwall Home Battery
      Home Battery Wall
      Lithium Phosphate Battery For Solar
      Boat Seat
      Copper Plated Marine Hardware
      Marine Anchor

    31. mm
      porn
      December 17, 2023
      Reply

      Hеllo are ᥙsing WordPress for your site pⅼatform?

      I’m neᴡ to the blog world but I’m trying to get
      stɑrted and create my own. Do you require
      ɑny coding expeгtise to make your own blog? Any help woᥙld be
      greatly apprecіated!

    32. mm
      Tricone Bits IADC127
      December 17, 2023
      Reply

      316 Stainless Steel Long Swivel Anchor Connector
      Hybrid Bit
      api rock drilling bits iadc127
      http://www.dtmx.pl
      iadc742 tricone bit supplier
      316 Stainless Steel Delta Anchor
      iadc735 9 7/8″ tricone bit
      316 Stainless Steel Anchor Connector
      316 Stainless Steel Hinged Plough Anchor
      316 Stainless Steel Bruce Anchor
      Tricone Bits IADC127

    Leave A Reply Cancel reply

    Your email address will not be published. Required fields are marked *


    Popular Courses

    Java Masterclass 2025: 130+ Hours of Expert Lessons

    Java Masterclass 2025: 130+ Hours of Expert Lessons

    Learn Python Programming Masterclass

    Learn Python Programming Masterclass

    Beginning C++ Programming – From Beginner to Beyond

    Beginning C++ Programming - From Beginner to Beyond

    SQL for Beginners: Learn SQL using MySQL and Database Design

    SQL for Beginners: Learn SQL using MySQL and Database Design

    C Programming For Beginners – Master the C Language

    C Programming For Beginners - Master the C Language

    Data Structures and Algorithms:  Deep Dive Using Java

    Data Structures and Algorithms: Deep Dive Using Java

    • Privacy Policy
    • Terms & Conditions

    logo-eduma-the-best-lms-wordpress-theme

    +61 422 512 549

    [email protected]

    Company

    • About Us
    • Blog
    • Contact
    • Become A Co-Instructor

    Links

    • Courses
    • FAQs
    • Contact Us

    logo-eduma-the-best-lms-wordpress-theme

    +61 422 512 549

    [email protected]

    Company

    • About Us
    • Blog
    • Contact
    • Become A Co-Instructor

    Links

    • Courses
    • FAQs
    • Contact Us

    • Privacy Policy
    • Terms & Conditions

    Would you like to become a Udemy Course Co-Instructor?

    Struggling to find students? Let us publish and promote your course to our students.

    FIND OUT MORE NOW