After learning about the Basic fundamentals of C Programming in the previous tutorial. Now this tutorial will be focussing about the decision control statements(if , else if ) and the looping statements (for, while, do-while) in C Language.
Its also similar to If statements except that it has another else block of statements, which are executed when the condition is false.
It is a further extension to the if-else statement, consisting of even more if-else statements. You can have as many as if-else statements. But after every if there should be an else block also. Thus it should end with an else block compulsarily.
The most popularly used looping statement. It repeats a given block of code for a given number of times as the user needs.
This is another looping statement gaining popularity these days. Its function is same as For loop, only the difference in syntax.
In this looping statement there is a change in working as in others condition was checked at the beginning, but here the condition is checked after the execution. Hence it runs the block of code 1 extra time than other loops even if the condition is false.
Decision Control Statements:
These statements are very much helpful in changing the flow of execution as they decide what statements to execute and when to execute, based on a specific boolean condition. These are also called Condition statements or Branching statements.
- if statement :
Its the most widely used and most easy statement in C, we'll encounter this in every complex or simple C program
Syntax: if(condition)
* It works as, if the condition inside the brackets is true then the set of statements in the if block will get executed otherwise not.
Example:
#include <stdio.h>
int main()
{
int n=20;
if (n==20){
printf("%d is actually 20",n);
}
return 0;
}
* here == is called the comparison operator which is used to compare 2 values.
* If there is only 1 statement inside the if-block, you can omit or skip the braces.
#include <stdio.h>
int main()
{
int n=20;
if (n==20)
printf("%d is actually 20",n);
return 0;
}
- If else statement :
Its also similar to If statements except that it has another else block of statements, which are executed when the condition is false.Example:
#include <stdio.h>
int main()
{
int n=120;
if(n==20)
printf("TRUE");
else
printf("FALSE");
return 0;
}
- If-else-if statement :
It is a further extension to the if-else statement, consisting of even more if-else statements. You can have as many as if-else statements. But after every if there should be an else block also. Thus it should end with an else block compulsarily.Example:
#include <stdio.h>
int main()
{
int n=120;
if(n==20)
printf("A");
else if(n>12 && n<20)
printf("B");
else if(n<0)
printf("Z");
else if(n>100)
printf("X");
else
printf("NOT");
return 0;
}
- If else statement :
If block in another if block, or it can be a if-else or if-else-if block inside another if block.
Program:
#include <stdio.h>
int main()
{
int a=12, b=11, c=10;
if(a>b){
if(a>c)
printf("a is greater");
else{
printf("c is greater");
}
}
else if(b>c){
if(b>c)
printf("b is greater");
else{
printf("c is greater");
}
}
else{
printf("c is greater");
}
return 0;
}
***********************************************************
ALSO WE CAN CREATE AS MANY AS IF ELSE STATEMENTS BUT ONLY POINT TO NOTICE IS EACH IF STATEMENT NEEDS TO HAVE ITS OWN ELSE BLOCK WHILE IN CASE OF IF ELSE.
***********************************************************
Looping Statements:
These are also called iteration statements as they are used to repeat the execution of block of statements on there own. This is very useful concept in C as well as found in other languages also. There are 3 types of looping statements.
Here we need a initialised variable, then comes the loop body where before entering the body the condition is checked , if it is true then the statements are executed. otherwise not. After the execution 1st time, then the control is redirected towards the increment statement where the value of the initialised variable is incremented. And the condition is again checked before entering the block. And when the condition becomes false. Then the loop block is exit and the rest code after the loop is continued.
* The condition provided should be a valid boolean condition.
- For Loop :
The most popularly used looping statement. It repeats a given block of code for a given number of times as the user needs.#include <stdio.h>
int main()
{
int i;
for(i=0 ; i<10 ; i=i+1)
{
printf("Hello\n");
}
return 0;
}
* The for loop consists of 3 main parts included inside the bracket after the keyword 'for'
First is the initialisation i.e, i=0, this is the starting point of the loop. Then comes the Condition part i.e, i<10, this means that the code will get executed for 10 times starting from 0 to 9(see the condition again) and then is the iteration statement which increases the value of the variable so that we can move to the next step. Also i=i+1 can also be written in the form i++ , it also means the same.
OUTPUT
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
- While Loop :
This is another looping statement gaining popularity these days. Its function is same as For loop, only the difference in syntax.Syntax:
initialisation
while(condition)
{
incrementation
}
* Initialisation, condition and incrementation are the same terms used in the for loop example. Only the place where they are set is changed.
* Since in while loop, condition is checked before entering the loop body, it is also known as ENTRY CONTROL LOOP.
Program:
#include <stdio.h>
int main()
{
int count=1;
while (count <= 4)
{
printf("%d ", count);
count++;
}
return 0;
}
OUTPUT
1 2 3 4
- Do While Loop :
In this looping statement there is a change in working as in others condition was checked at the beginning, but here the condition is checked after the execution. Hence it runs the block of code 1 extra time than other loops even if the condition is false.Syntax:
initialisation
do
{
statements
incrementation
}while(condition);
* Since in do-while loop, condition is checked at the end of the loop body, it is also known as EXIT CONTROL LOOP.
Program:
#include <stdio.h>
int main()
{
int i=0;
do
{
printf("executed one more time\n");
}while(i==1);
printf("END");
}
OUTPUT
executed one more time
END