Hello Viewer! How are you I hope you well? In the previous lesson, we learn If-else Statement, Type of Instruction. in this lesson we'll learn Loop Control Instruction in C language...
The ability to perform a program(set of instruction) repeatedly. Sometimes we need to print/execute any instruction/message one or more time.so in this situation, we use loop suppose we have to create that type of program that has to store details of 10 employee/student and I create a function with name "get_data()" so we have to run this function 10 times.
There are three ways to which we can repeat a part of the program.
1) Using "for" loop
2) Using "while" loop
3) Using "do-while" loop
While loop
We use while loop when we have to repeat something for a fixed number of times. understand with an example suppose we have to calculate gross salaries of two different people or we have to convert temperature from centigrade to Fahrenheit for 10 different cities. so in this type of case "while" loop is suited.
let's understand with program suppose we have to create a program that we have to store roll no. of 10 students then our program is like below.
#include
#include
int main()
{
int roll,i=1;
while(i<=5)
{
printf("\nEnter Your Roll : ");
scanf("%d",&roll);
i=i+1;
printf("Roll is %d",roll);
}
getch();
}
I hope in the above example we understood how to use while loop.in the next post we discussed more program about all the lectures/loops etc.
Syntax:
initialize loop counter; // ( like this int i=1;)
while(what is condition of your program) // (like this "while(i<3 i="">3>
{
do this; //what you want to repeat;
increment loop counter; // ( like this "i=i+1;")
}
Notes:
1) The statement within the while loop would keep getting executed until the condition is true. when the condition becomes false, then control out from the loop.
2) The condition may be tested with the relational or logical operators as like below.
while(i<=10)
while(i>=10 && j<=15)
while(j>6 && (c<7 b="" p="">
3) The statement within the loop may be a single line or block of a statement like below...
Single line
while(i<=10)
i =i+1;
Block
while(i<=10)
{
i =i+1;
}
4) The loop counter can be incremented or decremented by any suitable step value.
For Loop
Most programmers use "for" loop because it provides the facility to write three things in a single line.understand with the same program that we have written in "while" loop and you compare both the program.
#include
#include
int main()
{
int roll,i;
for(i=1;i<=5;i++)
{
printf("\n\nEnter Your Roll : ");
scanf("%d",&roll);
printf("\nRoll is %d",roll);
}
getch();
}
7>
Syntax:
for ( initialization ; condition ; increment ;)
{
-----Any Message----
}
1) When the "for" statement is executed for the 1st time the value of i is set to an initial value 1.
2) Next the condition "i <= 5" is tested. since the value of i is 1 the condition is satisfied and body of the loop is executed for the first time.
3) After the execution the value of i is increment by the statement of " i++ " it checks for the next condition is true/false if the condition is true the statement run and if the condition is false then control exit from the loop.
4) When I reached to the final value then the control exit from the loop.
5) It is important to note that the initialization, testing and incrementation.
Notes:
The "while" and "for" loop also can be nested. understand with the example below...
#include
#include
int main()
{
int i,j,s;
for ( i=0;i<=3;i++ )
{
for ( j=0;j<=3:j++)
{
s = i+j;
printf("\nSum is %d : ", s);
}
}
getch();
}
do-while loop
The do-while loop looks like this...
Syntax:
do
{
this; // Any message/statement
and this ;
and this ;
}while(condition);
There is a minor difference between the "while and do-while" looping.
1) The while testing the condition before executing any statements within the "while" loop.
and "do-while" test the condition after having executed the statements within the loop. This means that "do-while" execute it statement at once, even if the condition is failed for the 1st time. and the "while" will not execute it's statements if the condition fails for the first time. understand with an example below...
"while loop"
#include
#include
int main()
{
while( 4 < 1 )
{
printf("\n Hello Babes");
}
}
the output of this code is nothing/error because the condition of "while" is false so the statement in "while" loop that not execute. similarly, you can see the do-while for the same example...
#include
#include
int main()
{
do
{
printf("\n Hello Babes");
}while ( 4 < 1 );
}
The output of this code is "Hello Babes" because the "do-while" loop executed the loop statement before the condition is satisfied or not.
Notes:
The operators +=, -=, *=, /=, %= are compound assignment operators. they modify the value of the operand to the left of them.
There are three ways to which we can repeat a part of the program.
1) Using "for" loop
2) Using "while" loop
3) Using "do-while" loop
While loop
We use while loop when we have to repeat something for a fixed number of times. understand with an example suppose we have to calculate gross salaries of two different people or we have to convert temperature from centigrade to Fahrenheit for 10 different cities. so in this type of case "while" loop is suited.
let's understand with program suppose we have to create a program that we have to store roll no. of 10 students then our program is like below.
#include
#include
int main()
{
int roll,i=1;
while(i<=5)
{
printf("\nEnter Your Roll : ");
scanf("%d",&roll);
i=i+1;
printf("Roll is %d",roll);
}
getch();
}
I hope in the above example we understood how to use while loop.in the next post we discussed more program about all the lectures/loops etc.
Syntax:
initialize loop counter; // ( like this int i=1;)
while(what is condition of your program) // (like this "while(i<3 i="">3>
{
do this; //what you want to repeat;
increment loop counter; // ( like this "i=i+1;")
}
Notes:
1) The statement within the while loop would keep getting executed until the condition is true. when the condition becomes false, then control out from the loop.
2) The condition may be tested with the relational or logical operators as like below.
while(i<=10)
while(i>=10 && j<=15)
while(j>6 && (c<7 b="" p="">
3) The statement within the loop may be a single line or block of a statement like below...
Single line
while(i<=10)
i =i+1;
Block
while(i<=10)
{
i =i+1;
}
4) The loop counter can be incremented or decremented by any suitable step value.
For Loop
Most programmers use "for" loop because it provides the facility to write three things in a single line.understand with the same program that we have written in "while" loop and you compare both the program.
#include
#include
int main()
{
int roll,i;
for(i=1;i<=5;i++)
{
printf("\n\nEnter Your Roll : ");
scanf("%d",&roll);
printf("\nRoll is %d",roll);
}
getch();
}
7>
Syntax:
for ( initialization ; condition ; increment ;)
{
-----Any Message----
}
1) When the "for" statement is executed for the 1st time the value of i is set to an initial value 1.
2) Next the condition "i <= 5" is tested. since the value of i is 1 the condition is satisfied and body of the loop is executed for the first time.
3) After the execution the value of i is increment by the statement of " i++ " it checks for the next condition is true/false if the condition is true the statement run and if the condition is false then control exit from the loop.
4) When I reached to the final value then the control exit from the loop.
5) It is important to note that the initialization, testing and incrementation.
Notes:
The "while" and "for" loop also can be nested. understand with the example below...
#include
#include
int main()
{
int i,j,s;
for ( i=0;i<=3;i++ )
{
for ( j=0;j<=3:j++)
{
s = i+j;
printf("\nSum is %d : ", s);
}
}
getch();
}
do-while loop
The do-while loop looks like this...
Syntax:
do
{
this; // Any message/statement
and this ;
and this ;
}while(condition);
There is a minor difference between the "while and do-while" looping.
1) The while testing the condition before executing any statements within the "while" loop.
and "do-while" test the condition after having executed the statements within the loop. This means that "do-while" execute it statement at once, even if the condition is failed for the 1st time. and the "while" will not execute it's statements if the condition fails for the first time. understand with an example below...
"while loop"
#include
#include
int main()
{
while( 4 < 1 )
{
printf("\n Hello Babes");
}
}
the output of this code is nothing/error because the condition of "while" is false so the statement in "while" loop that not execute. similarly, you can see the do-while for the same example...
#include
#include
int main()
{
do
{
printf("\n Hello Babes");
}while ( 4 < 1 );
}
The output of this code is "Hello Babes" because the "do-while" loop executed the loop statement before the condition is satisfied or not.
Notes:
The operators +=, -=, *=, /=, %= are compound assignment operators. they modify the value of the operand to the left of them.
In this post are so enough 4 today. I hope you like it.
Thank You 4 reading my blog! if you have any problem or issue with this post please comment/contact.
Important :
In this post, some words/writing style is taken from Let Us C book that is written by Yashavant Kanetkar.