Introduction to Computer Programming - part 5

in #education7 years ago

enter image description here



INTRODUCTION TO PROGRAMMING

I am collating my study notes as part of my education into Computer Programming. In an attempt to increase and improve my own learning, and to help others also (learn by teaching) I will be sharing my notes, as neatly presented as possible, for others to follow along with if they so wish. This is Lesson 5's notes.


EARLIER LESSONS

Lesson 1 Notes
Lesson 2 Notes
Lesson 3 Notes
Lesson 4 Notes


enter image description here


INTRODUCTION TO COMPUTER PROGRAMMING – STUDY NOTES

LESSON 5


CONTROL FLOW

Control Flow = creating an order in which statements will be executed in a program.

We touched briefly on Control Flow in lesson 4 where we discussed Conditional Statements. In this lesson we are looking at Loops.

Loops = a loop has a condition, and so long as that condition is true, the code will continue to run. When it become false then the code will end.

LINK: Control Flow


While Loop

initialise
while (true){
    do this
    increment/decrement
}
more code

The above example prints out a line (I will be good in class) 50 times. The code checks that X is less than or equal to 50, and so long as that is true then the statement gets printed. When the code becomes false (x > 50) then the code will exit the loop.


Do-while Loop

initialise
do {
    do this
    increment/decrement
}
while (true)
more code

The difference between the Do-While Loop and the While Loop is that the Do-While Loop will be executed at least once, whereas the While-Loop contains the possibility that the code wont get run at all.

enter image description here

The example is similar code to the above, but has been written as a Do-While Loop, and had X changed to 100. This makes the While statement false. This example is to illustrate that the code still prints out once (see line at bottom of screenshot that says '100: I will be good in class'.)


For Loop

for (initialisation; condition; increment/decrement){
    statement;
}
more code

The For Loop puts everything into the brackets and determines if it's true before running the statement.

enter image description here


Jump Statements

Break = used to immediately jump out of a loop block.

enter image description here

The above example runs the loop, and then forces the loop to stop when X==10. At that point it is now outside the loop code.

Continue = skips the rest of a loop and jumps back to the top of the loop.

enter image description here

In the above example the If Statement is true if the number is odd. (X%2 == 1) is true if the number is odd, and this causes the Continue Statement to run, causing the odd number not to print (instead the loop continues back to the top), and the loop starts again from the last (non-printed) number. In other words, this loop will print the even numbers, and skip the odd numbers.


RECAP OF EARLIER LESSONS

In Lesson 1 we briefly looked at:

  • What is a programming language?
  • Language Types – especially HIGH Level and LOW Level.
  • Compiled vs Interpreted Languages
  • Data Types: Strongly Typed vs Weakly Typed Languages.
  • Data Types: The most common data types (characters, integers, floating-point, fixed-point, boolean, reference).

In Lesson 2 we looked briefly looked at:

  • Variables
  • Constants

These both flow on from Data Types and work with these.

In Lesson 3 we looked briefly at:

  • Operators

Used to allow operations (such as multiplication) to be performed on variables or constants.

In Lesson 4 we looked briefly at:

  • Conditional Statements

If, If-Else, If-Then Statements, which are used to control the flow of a program.


enter image description here


FURTHER DATA

cpp.sh - C++ shell website
Scratchpad - Shift + F4 on Firefox - Javascript shell
Dev C++ - A free, portable, fast and simple C/C++ IDE
Code::Blocks - A free C, C++ and Fortran IDE
Ideone - an online compiler and debugging tool which allows youto compile source code and execute it online in more than 60 programming languages.


My Posts

Introduction From a Newbie Programmer and Intro - part 2
Introduction to Computer Programming - part 1 (Lesson 1)
Introduction to Computer Programming - part 2 (Lesson 2)
Introduction to Computer Programming - part 3 (Lesson 3)
Introduction to Computer Programming - part 4 (Lesson 4)


Images from unsplash.com, except code screenshots, which are my own.

I welcome new followers, and thank you for your upvotes and comments.

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by humate from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

hi i am studying programming as well, but i am a super newbie .... so can you please help me with writing functions please?

We're both super-newbies. I am sharing my notes as I study them. Study along with me and maybe you will learn about functions soon. After this introduction course I will be sharing my notes on a C++ course.

Functions are made up of the material I am sharing (Control Flow Statements etc), but the knowledge on how to write them efficiently needs to be learned. I am still learning that.

Congratulations @humate! You have received a personal award!

1 Year on Steemit
Click on the badge to view your own Board of Honor on SteemitBoard.

By upvoting this notification, you can help all Steemit users. Learn how here!