Warning: The magic method Slickr_Flickr_Plugin::__wakeup() must have public visibility in /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php on line 152
‘#Mastery19’ Articles at TC101 Fall 2015, Page 2
Introduction to Programming Python and C++

Tag Archives: #Mastery19

#Masteries 19 and 20

Mastery 19. Use of loops with “while”… Mastery 20. Use of loops with “for”… And here my video:

Mastery Number 19

Basic use of While loops, but it does work.

Masteries 19 & 20: loops with while and for

Hi again there! Here is my video for #Mastery19 & #Mastery20:

Mastery 19 & Mastery 20

Hello everybody, this post is to explain the basic use of while loops and for loops. While is a loop that will be performed if the conditions inside its definition is true. Yes, you are right… again boolean value. If the condition inside the while returns the value true then the code under the while […]

#MASTERY19

Mastery 19

On this mastery I’m going to show you what “while” loop in C++ is and how to use it.

The while loop consists of something happens WHILE a condition is given. For example: WHILE x is greater or equal than 0, something will happen, and this process will continue to happen WHILE this condition is true.

For the purpose of these mastery, we will use a function and a while loop to continue printing a character while the condition is true. 

We want to print n amount of hashtags (#) because we really LOVE hashtags. So the user will input the amount of hashtags he wants to print and the program should be able to print that amount of hashtags. So, do as follows:

  1. Write our “basic code” and leave some space to write a function as we learn on masteries 11 and 12.
  2. Write a function of type CHAR (because we will use characters) that prints a # when you return its value.
  3. Inside int main (), declare the variable, and ask the user for it.
  4. Now, we will use the WHILE loop. So until now, your code should look something like this:

Now, for the WHILE loop, we want to ask the user for an x amount of times the character # will print, so we need this:

The code inside the while loop will continue to happen until x = 0. So your code should work like this:

And that’s about it, you now know how to use while loop with a function, congrats!

Loops with While

     A “loop” is how we call a process that repeats itself as long as some condition is true. As soon as this condition becomes false, the process continues as the loop is being left behind.


     We state this condition by the usage of the word “while” followed by the condition that needs to be true in order to initialize the loop.


     For example:


     As you can see, the condition is followed by “:”. This is the only way for the computer to know which is the condition that needs to be true in order to continue with the loop.  The condition will always be the one between the word “while” and “:”.


      The evaluation “!=” means “ is not ” and “==” means “is”.

      The previous conditions which is “x != 0” means “x is not 0”. But if we had “x==0” it would mean “x is 0”.

      We can also use “<” (smaller than), “>” (greater than), “<=” (lower or equal to)and “>=” (greater or equal to).



     The actions being done inside the loop will be held in the following lines. These need to be intended to be considered as part of the loop.


      What this program does is that is asks the user for a number, this number is stored in two diferent variables which are “x” and “x2”. The first one will remain the same and the second one will be altered during the loop.


      If the value given by the user is not 0, then the program will enter the loop.

In this loop, “x2” will be altered until it becomes “0”. The number of times this variable is being changed will be determined by the variable “count”.


     The next line is not intended, which means is not part of the loop. Therefore, this action will take place once “x2” is 0 (which is the condition that breaks the loop).

      
       In the end, what this program does is to determine how far from 0 is the number the user gave. After that, it prints the equation that need to be done to this number in order to turn it into 0.

       Another example more complicated than the one stated before ca be this one:

Loops with While

      In this program, the computer chooses a random number between 0 and 100 and it asks the user to guess it. The user has an unlimited number of tries to guess the number.


      The loop initializes when the number given by the user is not the same as chosen by the computer and it finishes once the user guesses the number. During the loop, there is a variable called “count” that will count how many times the loop is repeated; which is the same number of tries the user had.

Masteries 19 & 20

Masteries 19 & 20

#Masteries 15 & 19

Hi! This was my first youtube video for Ken’s class called Masteries TC1017 While loop & If

What should you work on?

Week #12 and more partial exams for you.

For this week's readings:
C++ (TC1017) should either be looking at support for your project, ImageMagick C++ libraries are a good start.
Python (TC1014) should be finishing chapter 11 (Dictionaries).