Author Archives: Mauricio Cooper

Mastery 22

When to use what type of repetition in a program

A repetition statement (or the looping statements or loops) enables the program to repeat an action as long as a particular set condition remains true. Its form goes as follow, “As long as you are at school, I will continue to pay your allowances”. This means that the action of paying you allowances will be done over and over again on condition that you go to school, but if you finish or drop out of school, you will have to fend for yourself. There are three types of repetition (loop) statements. They include the while,do.....while and for loop statements

while Repetition Statements

The while loop statements allows the program to perform a particular action while a set condition is true. In the case of our going to school example, the while statement can be stated as “while you go to school, I will pay your allowances”. Now this statement gives you the ability to determine how much you will earn while you go to school.
 

do..while statement

The do....while loop allows for executing an action at least before a condition is tested. To enable the loop automatically execute its statement at least once, the while condition is put at the end instead of the beginning. The loop executes its body before the condition is tested. So whether condition is true or false, the loop will execute at least once. 
 

for loop statement

The for loop is another form of repetition statements in C++. The for loop provides for the beginning of the condition, the limit of the condition and updates after every execution. The for loop takes the following form; for ( starting point; limiting condition; update ) { Code to execute while the condition is true }
The stating point can include an initialized control variable either declared internally or using an already existing variable. Any expression however that evaluates to a value can also be used. 
 
Credit:

1017 22

Mastery 21

Use of recursion for repetitives algorithms

What is recursion? The simple answer is, it’s when a function calls itself. But how does this happen? Why would this happen, and what are its uses?

When we talk about recursion, we are really talking about creating a loop. Let’s start by looking at a basic loop.

 



For those who don’t yet know, this basic loop displays the sentence, “The number is: ” followed by the value of ‘i’. Like this.


Inside the ‘for loop’ declaration we have the integer variable ‘i’ and have its starting value of 0. So the first time the sentence is displayed it reads, “The number is: 0”. The part of the ‘for loop’ declaration that is ‘i++’ tells the program that each time the loop repeats, the value of ‘i’ should be increased by 1. So, the next time the sentence is displayed it reads, “The number is: 1”.
This cycle will continue to repeat for as long as the value of ‘i’ is less than 10. The last sentence displayed would read, “The number is: 9”. As you can see the basic ‘for loop’ has three parts to its declaration, a starting value, what must remain true in order to continue repeating, and a modifying expression. Everything that is contained within the {braces} is what the program performs. Cout stands for console out, and prints words or characters to the screen.
So what does this have to do with recursion? Remember recursion is a loop. What if I did not want to just print a message to the screen? A loop can be used to perform other tasks as well.

Credit:

http://www.cplusplus.com/articles/D2N36Up4/

1017 21

Mastery 20

Use of loops with “for”

for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax:

The syntax of a for loop in C++ is:

Here is the flow of control in a for loop:

  • The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

  • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

  • After the body of the for loop executes, the flow of control jumps back up to theincrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

  • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

Flow Diagram:

Mastery 20

Example:

When the above code is compiled and executed, it produces the following result:

Credits:

http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

1017 20

Mastery 19

Use of loops with “while”

while loop statement repeatedly executes a target statement as long as a given condition is true.

Syntax:

The syntax of a while loop in C++ is:

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.

When the condition becomes false, program control passes to the line immediately following the loop.

Flow Diagram:

Mastery 19

Here, key point of the while loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example:

When the above code is compiled and executed, it produces the following result:

Credits:

http://www.tutorialspoint.com/cplusplus/cpp_while_loop.htm

1017 18 

Mastery 18

Nesting of conditional statements

It is always legal to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).

Syntax:

The syntax for a nested if statement is as follows:

 

You can nest else if…else in the similar way as you have nested if statement.

Example:

When the above code is compiled and executed, it produces the following result:

Credits:

http://www.tutorialspoint.com/cplusplus/cpp_nested_if.htm

1017 18

WSQ17

I’m just starting using it but I think that it would be a powerful tool for my entire career.

My last image in this course, this is for all the ninja banana lovers:

Final WSQ SciLab

1017 17

WSQ 16

Link to WSQ16 code

1017 16 ##CCourses

Quiz #11

Me, after the C++ final exam 

Link to code Q2

Link to code Q1

Quiz #10

ECOS

Classroom mates, if you haven’t done this, you must be out of your mind. It’s just as simple as making cereal.