While loop in C language, the ‘ask first’ of Control Flow

While loop in C language, the ‘ask first’ of Control Flow

‘While loop' in C let's you control the loops and cycles that a program executes. At the beginning of the loop, a condition is checked whenever it's true; if it's so, a code will be executed over and over again until the condition becomes false. This is helpful for counters, but it has various applications and purposes. Have you ever wondered why a program, computer, or device could run forever? It's because of the While instruction.

https://www.techzorro.com/en/blog/control-flow-c/

Sample of While loop in C

The While loop statement is a repetitive if statement. This means it continues asking the question in the condition and executing the tasks until the condition is false. Unlike the do statement, the While statement asks first before doing anything.

while (valueADC<512) {
  speedMotor++;
  speedFan++;
}

Syntax of While

While

While loop in C consists of 3 parts: the word While itself at the start, the condition (between parenthesis) and the statement with a semicolon at the end. The condition is the question(s) asked and the statement is the action taken after the result of the questions is known.

while (condition) statement;

The previous line of code is used if the statement can be written in only 1 line of code. If it requires more lines, then it can be written the following way:

while (condition) {
  statement 1;
  statement 2;
  statement 3;
  statement 4;
  //etc
}

Relational Operators with While loop in C

How could the result from the condition be found out? You have to ask the correct question. The following relational operators will return a true (1) or false (0) depending in the condition.

  • Equal ==.
  • Not equal !=
  • Greater than >
  • Less than <
  • Greater than or equal >=
  • Less than or equal <=

For example:

while (A==B) statement //while A is equal to B, the statement will be executed.
while (A>B) statement //while A is greater than B, the statement will be executed.

Example of While loop in C

The following examples are done for 8-bit microcontrollers and its programmed in MPlab X IDE.

Example 1

This code is usually used for debugging and depuration. It's useful to make a program stop after a certain test or branch. If an error occurs, it might be good idea to stop an entire program.

//Accelerometer
if (Accel_Init(SCALE_2G,ODR_800)) {
  TextUART(Start);
  I2C_Lcd_Out_C(2,1,Start);
  UART1_Write(Tab);
} 
else {
  TextUART(Error);
  I2C_Lcd_Out_C(2,1,Error);
  while(1); //program gets stucked here (on purpose!!).
  }

Example 2

Do you remember that in the first paragraph, I wrote that programs and devices can run forever? Use the While with a 1 in the condition and the program will always run because the condition will be always true.

void main() {
  Config();
  while(1){
    //code goes here
  }
}

Example 3

In this code, the function reads content and sends a string of characters to a LCD screen. Notice it sends only one character at a time, passes to the next position in the string (for example, 3th character to the 4th character) and the process is repeated again.

void TextUART(const char *puntero) {
  while (*puntero!=0) { //!*puntero
       UART1_Write(*puntero);
       puntero++;
  }
}

techZorro's Index of Content

Keep Reading!

One Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.