Join Free! Sign In UPLOAD
Science Science Math Math History History Social Studies Social Studies Language Arts Language Arts Educational Songs Kids Educational Songs
More
History Algebra Earth Science Geography Health PE Fractions Elementary Science Music Programming Languages Pre Calculus Chemistry Biology More
Open main menu TeacherTube.com
  • Join Free! Sign In UPLOAD
  • Close main menu

Remove Ads

Learn C Language - Explanation of While Loop in C Programming

Elementary / Language Arts / Speaking And Listening

newtum

Feb 18, 2020

121 views

Speaking And Listening

Bachelors

In this chapter, we will learn about the loop. Many programming languages support three types of loops. While loop, do while loop and for loop, in this chapter, we will focus more on the while loop. So if you want to write a simple program in C, like printing a variable, it's pretty simple. We have done this in the last chapters many times, right? What you will do is you will create a main statement, you will initialize the variable. Let's say it's I equal to one. You will use printf statement to display the value. So this is the program. It's output will be I equal to one. Now suppose you want to print one to 9 like this. What will you do? Will you write the printf statement 9 times? No, it's too much work. What you will do is you will write a printf statement one time and you will execute it 9 times. Let's see how this is done in C language. Let's open our source code again. What we will do is we will put the printf statement inside while like this in the vial statement, we will write a condition that I is less than ten. Remember, we have used this condition in F and L statements. Here, we are telling our program to run print value statement till 9. But the value of I is only one, so we need to tell the program to increment the value. So what we will do is we will ask our program to increment the value of I after printf is executed like this. Here, we have initialize the variable I equal to one, this is called initialization. Next, we have given the condition and after that, we have given the increment. Now, the only thing is print test statement. Instead of printf, we can write any logic that we want. So what we understand is for loop execution, three things are very important. First is the initialization. Second is the condition and third is increment. Just try to understand the program. First, computer reaches the part of initialization that is R equal to one next, it reaches the vile condition. In wild condition, it checks whether eyes less than ten, but currently the value of I is one. So one is definitely less than ten. The result is true. So it goes to the printf statement, and it prints the value of I that is one. In the next statement, it says R equals two, I plus one. So the value of I becomes two. Now what happens is it goes to the end of the bracket and again it jumps to the condition. In the condition, it checks where the eye is less than ten or not, but the value of I is to so again the condition is true. Since the condition is true, the system enters the loop again, and it prints the value of I that is to. Now it enters the increment code. In increment, value of R becomes three. Now it again enters the condition. And in the condition, system checks that where the eye is less than ten or not. But again, the condition is true because three is less than ten. So what happens is it again enters the loop. Now, suppose we have done the printing till 9. After printing the value of 9, system goes to increment and the value of I becomes ten. Now it again checks the wild condition. Condition is I less than ten or not. But ten is definitely not less than ten. So the condition breaks and systems skips the while loop and goes to the next line. Here, the while loop ends.

Remove Ads

Remove Ads

Related categories

Elementary
Language Arts
Reading
Common Core
Handwriting
Speech
Study Skills
Writing
Elementary > Language Arts > Speaking And Listening
Online Spelling Test
Online Spelling Test
SPED730 Teacher Interview
SPED730 Teacher Interview
How does an AI communication tool work
How does an AI communication tool w...
Phonic Sounds
Phonic Sounds
First Interaction with English Expert and English learner at EngVarta - English Learning App
First Interaction with English Expe...
Grover Lewis- SPCH 100- ITR#2- 3/24/2021
Grover Lewis- SPCH 100- ITR#2- 3/24...
Public Speaking Intro
Public Speaking Intro
My School Description
My School Description
Fear of Public Speaking - Steps for Effective Communication for Students (Of All Ages)
Fear of Public Speaking - Steps for...
Day 12 Lecture
Day 12 Lecture
Elementary > Language Arts
Protons and Neutrons
Protons and Neutrons
I Voted: Making a Choice Makes a Difference
I Voted: Making a Choice Makes a Di...
Greenie Grows a Garden
Greenie Grows a Garden
Electrons
Electrons
Benji, the Bad Day, and Me
Benji, the Bad Day, and Me
Beautiful Shades of Brown
Beautiful Shades of Brown
Context Clues Detectives
Context Clues Detectives
Online Spelling Test
Online Spelling Test
The Best Christmas Pageant Ever - Character Traits
The Best Christmas Pageant Ever - C...
The Best Christmas Pageant Ever - Character Traits
The Best Christmas Pageant Ever - C...
Elementary
parachutes
parachutes
Tic Tac Toe Subtraction Game
Tic Tac Toe Subtraction Game
Balloons Over Broadway
Balloons Over Broadway
Lessons with Pam - How to Catch a Turkey_Read, Make, Do
Lessons with Pam - How to Catch a T...
Lessons with Pam - Counting in the Ocean_Read, Make, Do
Lessons with Pam - Counting in the ...
Ecosystems
Ecosystems
MLA Format
MLA Format
PPTtoTeams
PPTtoTeams
Lessons with Pam - Llama Llama Loves to Read_Read, Make, Do
Lessons with Pam - Llama Llama Love...
Penny's Reading Nook - Veterans - Heroes in Our Neighborhood_Read, Make, Do
Penny's Reading Nook - Veterans - H...
  • Contact Us
  • Help and FAQ
  • Privacy Policy
  • Terms
  • California - Do Not Sell My Personal Information
  • California - CCPA Notice
© 2023 TeacherTube. All Rights Reserved.