Skip to Main Content or Page Contents

How to Branch and Loop

C Programming Tutorial - Using if, if ( ) ... else, do ... while, while, for, switch

Results per page:

Match: any search words all search words

 Branch & Loop Page Contents

Introduction

Function Quick Comparison

 Branching    if ( )   if ( ) ... else

Relationa-Operators

 Looping   while ( )  for ( )  switch ( )

Divide by zero error

Semicolon ;

braces { }

Introduction

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

Top of Page

Branching

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.

Top of Page

Relational Operators

Relationa Operators
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.

Logical Operators

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

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

Top of Page


True or False

In C the value of true is nonzero, and false 0.

Function Quick Comparison

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.

Construct Times executed A section of code executed
if 0 or 1 Section 1 Not executed or executed
if ... else 1 Section 1 or Section 2 executed
switch ... case 0 or 1 Not executed or 1 or more section(s) executed.
A default option available
while 0,1 or more Section 1 Not executed, executed once, or executed more times
do ... while 1 or more Section 1 executed at least once or more times
for 0,1 or more Section 1 Not executed or executed 1 or more times

Top of Page

 

 


© tutorials4u.com
HTML Tutorial by John McGuinn.