Tag Archives: C

Flow Control Statements

(Click the below link for a Microsoft Word version of this blog-post)

flow_control_statement

(Click the below link for a pdf version of this blog-post)

flow_control_statement

 

if_jpeg_segment

Figure 1: I drew the above segment of a Flowchart Algorithm in Microsoft Word.

In programming, statements such as:

if

, which introduce a condition, are known as:

flow-control statements

.

One way to conceive of Computer Algorithms, is to represent them as Flowcharts.  The:

if

statement alters or controls the flow of the algorithm.

In the above depicted example[1], if the condition tested by the:

if

statement should be found to be true, then the logical execution of the algorithm will flow down the left-hand side of the page.

In the above depicted example, if the condition tested by the:

if

statement should be found to be true, then the logical execution of the algorithm will flow down the left-hand side of the page.

In the above depicted example, if the condition tested by the:

if

statement should be found to be false, then the logical execution of the algorithm will flow down the right-hand side of the page.

When we introduce a logical split into our algorithm, then this is termed:

‘branching’

.

The true and false tributaries of the depicted flow-chart algorithm are termed:

‘branches’

.

branching

Figure 2:  The algorithm branches.  We can instruct the computer to do different tasks depending upon whether the logical condition tested by the if statement be found true or false.

if_jpeg_segment

Figure 3:  In this segment of a Flowchart Algorithm, we can see that it branches after we test a logical condition with an if statement.  In the depiction, above, we can observe the true branch of the Algorithm, and the false branch of the Algorithm.  I drew the above illustration in Microsoft Paint.

algo_second_version_final

Figure 4: I drew this flow-chart algorithm in Microsoft Word.

In Figure 4, we have a flow-chart algorithm that describes a program that takes an integer – either a 1 or a 0 – inputted by the user, and which outputs a string contingent upon what the user has inputted.

The above algorithm solves a computational problem.  The computational problem that the above algorithm solves may be stated as:

 How can we test a litteral inputed by a user so as to see if it should equate to Boolean True or Boolean False?

Mutual Exclusion:

The:

true

 and:

 false

branches of this algorithm are termed:

‘mutually exclusive’

.

A logical test is performed, and if that which is tested be true then that excludes the possibility of its being false.

A logical test is performed, and if that which is tested be false then that excludes the possibility of its being true.

If the true branch of the algorithm be executed, then the false branch of the algorithm will not be executed.

If the false branch of the algorithm be executed, then the true branch of the algorithm will not be executed.

Branching in Python:

We shall now write a program in Python that corresponds to the algorithm depicted in Figure 4.

python_branch_program_final

Figure 5:  This is the python program that corresponds to the algorithm in Figure 4.

output_0_false

Figure 6:  This is what is outputted by the Python program depicted in Figure 5 should the user input the value, 0.

output_1_true

Figure 7:  This is what is outputted by the Python program depicted in Figure 5 should the user input the value, 1.

output_2_invalid_boolean_input

Figure 8:  This is what is outputted by the Python program depicted in Figure 5 should the user input a literal that is not a 1 or a 0.

More on Branching in Algorithms in General:

As we can see from the algorithm depicted on Page 5, the:

true

and:

false

branches of the algorithm converge or attain a confluence prior to the:

“Goodbye!”

string’s being outputted.

The:

“Goodbye!”

string will be outputted regardless of the result of the logical condition tested by the:

if

statement.

Back to If Statements in Python:

One quintessential piece of Python syntax is the colon.  The colon is used to declare that what follows will be an indented code block.

colon_black_pen

Figure 9:  In Python, if statements are always terminated by colons.  In Python, the colon always declares that the preceding code block will be indented.  The code block that follows the colon that terminates the if statement is indented[2].  I drew the above image with pens.

Back to Branching in Algorithms in General:

Trees are not the only things that branch.  Rivers also branch into tributaries.  Rivers also flow downwards[3], and so it is an excellent analogy so as to conceive of branching in algorithms.

branching_river

Figure 10:  Another way to conceive of branching in flow-chart algorithms: the flow-chart algorithm branches into true and false code blocks after a logical condition is tested, before re-attaining a confluence prior to “Goodbye” being printed.  The two tributaries of the flow-chart algorithm merge together again prior to “Goodbye” being printed.  Regardless of whether the true code block or the false code block be executed, “Goodbye” will nonetheless be printed.

What is the Purpose of Writing an Algorithm prior to Writing a Program?

An algorithm is imperative[4] knowledge.  It tells one how to do something.  In computing, an algorithm tells one how to solve a computational problem.

In computing, an algorithm is a series of commands that solves a computational problem.

There are two approaches to programming:

Seat-of-the-Pants Method:

With this method, the programmer just dives into writing the program.  However, the programmer still composes an algorithm, only this time, the algorithm is mental.  At each stage of his writing a program, the programmer still must imagine what he must command the computer to do for it to solve a computational problem.  The programmer just does not take the time to write this series of commands or algorithm down.

Write-the-Algorithm-First Method:

With this method, the programmer solves the computational problem first prior to his commencing writing the program.  He does this by writing an algorithm.

The advantage of writing an algorithm is that it does not limit the programmer to a solution in a single language such as Python.  Should the programmer take the time to write out the algorithm first, then it will allow him to easily compose a program that corresponds to that algorithm not only in Python, but in whatever programming language that he should so choose.

Writing a Program that Corresponds to our Algorithm in C:

In Figure 4, we wrote an algorithm that solved a computational problem.  The computational problem that was solved by the algorithm depicted in Figure 4 can be stated as:

How can we test a litteral inputed by a user so as to see if it should equate to Boolean True or Boolean False?

With the above-stated computational problem solved, we can now easily write a program that corresponds to the algorithm, not only in Python syntax, but in C syntax, as well.

c_program_true_false_code_final

Figure 11:  The C program that corresponds to the algorithm depicted in Figure 4.

 

output_0_false_c

Figure 12:  What the C program depicted in Figure 11 outputs should the user input a 0.

output_1_true_c

Figure 13:  What the C program depicted in Figure 11 outputs should the user input a 1.

output_2_invalid_boolean_input_c

Figure 14:  What the C program depicted in Figure 11 outputs should the user input a literal that is neither a 0 or a 1.

Glossary:

confluence

  • noun. the junction of two rivers, especially rivers of approximately equal width.
    • an act or process of merging: a major confluence of the world’s financial markets.

<ORIGIN> late Middle English: from late Latin confluentia, from Latin confluere ‘flow together’ (see CONFLUENT).[5]

<ETYMOLOGY>  From the Latin 1st-declension feminine noun, ‘conflŭentĭa, conflŭentĭae,’ which means ‘a flowing together.’[6]  From the Latin preposition, ‘cum,’ which means ‘together;’ and the Latin 3rd-conjugation verb, ‘fluō, fluere, fluxī, fluxum,’ which means ‘to flow;’ and the Latin 1st-declension nominal suffix, ‘-tia, -tiae,’ which denotes a state of being.  A confluence, therefore, etymologically, is ‘a flowing together.’

As regards algorithms, by way of an analogy, a confluence can be said to describe the merging of two or more branches of a flow-chart algorithm.

 

 

 

confluent

  • adjective. flowing together or merging.

<ORIGIN> late 15th century: from Latin confluent- ‘flowing together’, from confluere, from con- ‘together’ + fluere ‘to flow’.[7]

<ETYMOLOGY>  From the Latin 3rd-declension masculine noun, ‘cōnfluēns, cōnfluēntis,’ ‘ which means ‘confluence.’ ‘flowing together.’  From the Latin preposition, ‘cum,’ which means ‘together;’ and the Latin present active participle, ‘fluēns, fluēntis,’ which means ‘flowing.’

 

As regards algorithms, by way of an analogy, two or more branches of an algorithm can be said to be confluent when they merge together.

 

 

imperative

  • adjective.
  1. of vital importance;crucial:  immediate action was imperative | [with clauseit is imperative that standards are maintained.
  2. giving an authoritative command; peremptory:the bell pealed again, a final imperative call.
  • [GRAMMAR] denoting the mood of a verb that expresses a command or exhortation, as income here!
  • noun.
  1. an essential or urgent thing:free movement of labour was an economic imperative.
  • a factor or influence making something necessary:the biological imperatives which guide male and female behaviour.
  1. [GRAMMAR] a verb or phrase in the imperative mood.
  • (the imperative) the imperative mood.

<DERIVATIVES> imperatival adjective.  imperatively adverb.  imperativeness noun.

<ORIGIN> late Middle English (as a grammatical term): from Late Latin imperativus (literally ‘specially ordered’, translating Greek prostatikē enklisis ‘imperative mood’), from imperare ‘to command’, from in- ‘towards’ + parare ‘make ready’[8].

 

<ETYMOLOGY>  from the Latin 1st-and-2nd-declension adjective, ‘impĕrātīva, impĕrātīvus, impĕrātīvum,’ which means ‘pertaining to the command;’ ‘of the command.’  From the Latin 1st-conjugation verb, ‘imperō, imperāre, imperāvī, imperātum,’ which means ‘to command,’ ‘to order,’ and the Latin 1st-and-2nd-declension adjectival suffix ‘-īva, -īvus, -īvum,’ which means ‘of,’ ‘concerning,’ ‘pertaining to.’  From the Latin prefix ‘in-’ which expresses the concept of ‘unto,’ ‘toward,’ and the Latin 1st-conjugation verb, ‘parō, parāre, parāvī, parātum,’ which means ‘to make ready,’ ‘to prepare.’  The etymological sense, therefore, of the English adjective, ‘imperative’ is: ‘concerning the command;’ ‘pertaining to the command;’ ‘of the command;’ ‘concerning the order;’ ‘pertaining to the order;’ ‘of the order;’ ‘concerning the making ready of;’ ‘pertaining to the making ready of;’ ‘of the making ready of;’ etc.

 

As regards algorithms, ‘imperative’ denotes the type of knowledge expressed by a series of commands, as opposed to declarative knowledge.

 


[1] i.e. the example depicted in Figure 1.

[2]  In Python style, an indent is worth 4 spaces.

[3]  As does a flow-chart algorithm.

[4]  From the Latin 1st-conjugation verb, ‘imperō, imperāre, imperāvī, imperātum,’ which means: ‘to command,’ ‘to order.’  Cp.  Latin English Lexicon: Optimized for the Kindle, Thomas McCarthy, (Perilingua Language Tools: 2013) Version 2.1  Loc 46105.

[5]  Oxford University Press.  Oxford Dictionary of English (Electronic Edition). Oxford.  2010.  Loc 146068.

[6]  Cp.  Latin English Lexicon: Optimized for the Kindle, Thomas McCarthy, (Perilingua Language Tools: 2013) Version 2.1  Loc 23064.

[7]  Oxford University Press.  Oxford Dictionary of English (Electronic Edition). Oxford.  2010.  Loc 146082.

[8]   ibid.  Loc 345790