C – While Loops

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/c-while-loops/
PINTEREST
PINTEREST
INSTAGRAM

C Programming Tutorial Index

  1. Introduction To Programming
  2. C Setup Development Environment
  3. C Hello World and Compile First Application
  4. C Syntax and Basics
  5. C Variables
  6. C Numeric Variable Types (Integer, Long, Float)
  7. C String Variables
  8. C Constants and Define Processor
  9. C Operators
  10. C If Else and Switch Case Conditionals
  11. C While Loops
  12. C For Loops
  13. C Arrays
  14. C Functions
  15. C Structure
  16. C Union
  17. C File Operations (Open, Read, Write)
  18. C Pointers
  19. C Type Casting
  20. C Header Files and Include

Loops are used to loop and iterate through sequential data or similar serial output. Sequential data will be generally an array. For example to search in a array for a specific value while loop can be used. We will look 3 main loop mechanism provided by C programming language.

While

While runs repeatedly if given condition is true. Here is the syntax of while .

In while syntax condition can be a boolean  value like true or false.

In this example we will count up to 10. For each step the a<10 will be evaluated. a=a+1 will run in each step and a will increment one by one. When a reached 10 the a<10 will be false and the while loop end. In each step the value of the a will be printed with printf function.

Here our condition is a<10 which is met for the first 10 steps but in each step we increment a and print it to the console with printf("%d\n",a)

Infinite While Loop

While loop is the simplest and easiest way to create infinite loop. Infinite loop will run forever if the application is not stopped externally. We will put 1 into condition of the while which is equivalent of true boolean in C.

LEARN MORE  Python Script Change Mac Address Periodically

OR we will use 1=1 which will evaluate boolean true

OR we will use 1>0 which is always true

Do While

Do While mechanism is very similar to While but the difference is that While code block is executed after condition check. In Do While mechanism code block is executed first and then condition is checked. If condition is not met which is not true the Do While mechanism ends. Here is the syntax.

In the example below we print some message to the console and then check the number.

C Programming Tutorial Index

  1. Introduction To Programming
  2. C Setup Development Environment
  3. C Hello World and Compile First Application
  4. C Syntax and Basics
  5. C Variables
  6. C Numeric Variable Types (Integer, Long, Float)
  7. C String Variables
  8. C Constants and Define Processor
  9. C Operators
  10. C If Else and Switch Case Conditionals
  11. C While Loops
  12. C For Loops
  13. C Arrays
  14. C Functions
  15. C Structure
  16. C Union
  17. C File Operations (Open, Read, Write)
  18. C Pointers
  19. C Type Casting
  20. C Header Files and Include
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/c-while-loops/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)