Skip to Main Content or Page Contents
Branching if ( ) if ( ) ... else
Looping while ( ) for ( ) switch ( )
Unless you already understand the basics of “branching and looping” you should read The previous tutorial Controlling Program Flow in Plain English.
You have arrived at the point where programming becomes more challenging and if you have a programmers mind" far more interesting, so enjoy.
This is the stage where you have to start thinking, how can I code a particular aspect of my project.
This is the point where you start to develop your personel style.
Up to this tutorial your programs have been sequential or linear statements being executed one after the other. Now you are going to be introduced to constructs that alter that linear progression.
The new functions that are in this tutorial allow either branching or looping.
You will note that a pair braces { } are used with these constructs.
The { denotes the start of the first statement within the construct.
The } denotes the end of the last statement in the construct.
Tip: Take particular note of how braces { } are used.
Tip: Note where ; is NOT used in these constructs.
Tip: As you read my tutorials, books, course material etc., copy a few samples of each construct showing the use of braces and where ; is used or not used, for future reference. Pay particular note of the if ( ) ... else construct, and where constructs are nested. That it where a construct is placed within the braces of another construct. You can have several layers of nesting
Branching statements allow different sections of code to be executed, or not executed depending on some condition being either true or false.
The following constructs contain branching.
From the previous tutorial, “Do you require sugar” is an example
.
if, if ... else, while, switch, and for constructs.
All these construct contain a test. Think of test as a question. Example “Is the number less than 3”
All tests contain a Relational Operators such as equal to, less than, or greater than. These Relationa Operators are listed below.
Operator | Description | Example |
---|---|---|
== | Tests if the values of two operands are equal or not. If yes, then the condition becomes true. | (A == B) is not true. |
!= | Tests if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. | (A != B) is true. |
> | Tests if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. | (A > B) is not true. |
< | Tests if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. | (A < B) is true. |
>= | Tests if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. | (A >= B) is not true. |
<= | Tests if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. | (A <= B) is true. |
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then −
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | !(A && B) is true. |
Looping constructs are used to repeat a section of code a number of times, depending on some condition being either true or false.
To take the previous lesson a little further, if somebody wanted 3 teaspoon full of sugar the you could repeat adding 1 spoonful of sugar 3 times.
Iteration is the correct term to use for this looping process
The looping constructs are while, for constructs.
Again these contructs contain a test and a a comparison / conditional operator
This table compare how the different contructs compare in what they do. At the moment you may not understand this comparison, refer back to this table as you gain experience.
The actual test is always carried out, and depending on the test result, the following section(s) code will be or will not be exectuted, this is listed in the Times executed column.
C Tutorial 4. Branch & Loop Introduction
C Tutorial 5. Branch & Loop Code
This site is hosted on Hostgator