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

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/feed-rss2.php on line 8
‘Mastery #19’ Articles at TC101 Fall 2015 https://kenscourses.com/tc101fall2015 Introduction to Programming Python and C++ Thu, 26 Nov 2015 01:50:47 +0000 en hourly 1 https://creativecommons.org/licenses/by/4.0/ Loops with “While” https://kenscourses.com/tc101fall2015/2015/loops-with-while-5/ Thu, 26 Nov 2015 01:50:47 +0000 http://juansalvadorfernandez.wordpress.com/?p=169 Continue reading Loops with “While”]]> Looping will be very important skill for this course, from my experience more that 75% of the activities you will do during the course will require you to loop a piece of code.

There are different ways to make loops, but right now we will review the one called “While”.

The while loop basically takes a condition, and as long as this condition remains true the loop will continue, meaning that the instructions WITHIN the while will keep repeating themselves.

For instance if we write: while(i>0): print(“hit yourself ”), the condition for the while to happen is i>0, so each time the loop repeats itself their two values will be compared to see if the condition is true or false.

The piece written above has two ENOURMOUS mistakes, number one being that “i” has no assigned value, we can solve this by writing “i=1” before the while. The remaining problem is that, since there is nothing that makes the value of “i” change, it will always be one, meaning that it will always be bigger than zero and that the while will repeat itself endlessly, eating up your RAM and killing your computer, and that is a big no-no.

One very simple way to solve this matter is to write “i= i- 0.1” inside the while, this way the value for i will change every time the while repeats itself, in this case turning into a smaller number, eventually reaching the value of zero.

Now this is very important, once the condition the while has is no longer met, in this case i>0, the while will stop.

For the program we wrote i=1, and each run of the while we subtract 0.1, turning it into 0.9, 0.8, 0.7 and so on till we reach zero, this means that the while will repeat itself ten times, performing the action inside it ten times.

The outcome of our little program will be: “hit yourself hit yourself hit yourself hit yourself hit yourself hit yourself hit yourself hit yourself hit yourself hit yourself”

Of course this is just the basics, things will get scarier as we continue, but be patient and try to understand this as best as you can, since the while itself does not change, EVER, just the things outside it and inside it.

]]>
https://creativecommons.org/licenses/by/4.0/
Using Loops FOR and WHILE https://kenscourses.com/tc101fall2015/2015/using-loops-for-and-while/ Sat, 31 Oct 2015 02:02:56 +0000 http://myfreakingcrazythoughts.wordpress.com/?p=155 Continue Reading →]]> Loops

Loops are used to repeat a block of code. Being able to have your program repeatedly execute a block of code is one of the most basic but useful tasks in programming — many programs or websites that produce extremely complex output (such as a message board) are really only executing a single task many times.

FOR – for loops are the most useful type. The syntax for a for loop is:

For (variable initialization; condition; variable update) {
  Code to execute while the condition is true
}

The variable initialization allows you to either declare a variable and give it a value or give a value to an already existing variable. Second, the condition tells the program that while the conditional expression is true the loop should continue to repeat itself.

 

WHILE – WHILE loops are very simple. The basic structure is

while (condition) {Code to execute while the condition is true} the true represents a Boolean expression. It can be any combination of Boolean statements that are legal.) Notice that a while loop is the same as a for loop without the initialization and update sections. However, an empty condition is not legal for a while loop as it is with a for loop.

Example:

#include <iostream>

Using namespace std; // So we can see cout and endl

int main()
{ 
  int x = 0;  // Don't forget to declare variables
  
  while ( x < 10 ) { // While x is less than 10 
    cout<< x <<endl;
    x++;             // Update x so the condition can be met eventually
  }
  cin.get();
}

Here is an example of a program which has loops (factorial calculator):

/* Source code to find factorial of a number. */
#include <iostream>
using namespace std;
int main()
{
  int x , factorial=1;

    cout << "Please enter a non-negative number:  " << endl;
    cin >> x;

    for(int a=1; a<=x; a++)//HERE IS THE "FOR" LOOP
    {
        factorial=factorial*a;
    }

cout << "The factorial of your number is " << factorial << endl;
cout << "Would you like to play again?   ";
string answer;
cin >> answer;

do
{
    int x , factorial=1;

    cout << "Please enter a non-negative number:  " << endl;
    cin >> x;

    for(int a=1;a<=x;a++)//HERE IS THE "FOR" LOOP
    {
        factorial=factorial*a;
    }

    cout << "The factorial of your number is " << factorial << endl;
    cout << "Would you like to play again  ? ";
    cin >> answer;

}while (answer =="yes");//HERE IS THE "WHILE" LOOP

if (answer== "no")
{
  cout << "AS YOU WISH MASTER, MAY THE FORCE BE WITH YOU! " << endl;
}

return 0;
}

Thank’s to http://www.cprogramming.com/ for the support!

-The Admin.

]]>
https://creativecommons.org/licenses/by/4.0/