Conditional Statements in C programming language
In this tutorial, we learn about Conditional Statements. Conditional Statements in C programming are used to make decisions based on the conditions.If you put some condition for a block of statements, the execution flow may change based on the result evaluated by the condition. Conditional statements execute sequentially when there is no condition around the statements.
Conditional Statements in C programming language:-
Conditional Statements helps you to make decisions based on certain condition.The conditional Statements in C are the decision making statement they are the branching statement.
These are conditional Statements in C Language :-
1) if statement.2) if....else statement.
3) if_else ladder.
4) Nested if...else statement.
5) switch statement.
1) if...else statement:-
Syntax:-if(Condition)
{
// Statements;
---
----
}
Working of if...else statement:-
If the condition is evaluated to false, statement inside the body of if are executed.
If the condition is evaluated to true, Statement inside the body of if are not executed.
Ex.
#include<studio.h>
int main()
{
int X=20;
int Y=22;
if(X<Y)
{
printf("X is less than Y.");
}
return 0;
}
OUTPUT:
X is less than Y.
2) if... else statement:-
In if statement may have an optional else block.Syntax:-
if(Condition)
{
Statement;
------
}
else {
Statement;
}
Working of if...else statement:
If the condition is evaluated to true.
Statements inside the body of if are executed.
Statements inside the body of else are skipped from execution.
If the condition is evaluated to false else block is executed.
Statements inside the body of if are skipped from execution.
Ex. Consider odd & even
#include<stdio.h>
int main( )
{
int num;
printf(" Enter an integer :");
scanf("%d", &num);
if(num%2 = =0)
{
printf("%d is an even number ",num);
}
else{
printf("%d is an odd number",num);
}
return 0;
}
OUTPUT :
Enter an integer : 8
8 is an even number
When the user enter 8 , the test expression num%2° = =0 is evaluated to true. Hence the statement inside the body of if loop is executed and print output 8 is an even number.
3) if...else ladder :-
The if...else statement is useful to check multiple condition within the program.If none of the condition evaluates to be true then the last else block is executed.
Syntax :-
if (Condition)
{
// Statement 1;
}
else if(Condition 2)
{
//Statement 2;
}
else if (Condition 3)
{
//Statement 3;
}
else
{
//Statement 4;
}
Ex.
#include<studio.h>
int main()
{
int i=30;
if( i>=0 && i<=5 )
{
printf("i is between 0 and 10");
}
else if (i>=11 && i<=15 )
{
printf("i is between 11 and 25");
}
else if ( i>=16 && i<=29)
{
printf ("i is between 16 and 29");
}
else
{
printf( "i is greater than 29");
}
OUTPUT:
i is greater than 29
4) Nested if...else statement
Nested if statement that is the target another if statement Nested if statements means an if statement inside another if statement means we can place the if statement inside the another if statement.Syntax:-
if ( Condition )
{
// block of statements;
if( condition 2)
{
// Block of statements;
}
}
Ex.
#include<stdio.h>
int main ( )
{
if ( i= = 10 )
{
if ( i<15 )
printf ("i is smaller than 15");
if ( i<12 )
printf (" i is smaller than 12");
else
printf (" i is greater than 15");
}
return 0;
}
5). Switch statement
The switch statement allow us to execute one code block among many alternatives.Syntax:-
switch ( Condition )
{
Case constant 1;
//Statement;
break;
Case constant 2;
//Statement;
break;
----
----
default:
// default statement;
}
How it works?
- The expression is evaluated onces and compared with the values of each case label.If there is match, the different / corresponding statement after the matching label are executed.
If there is not match, the default statement is executed.
Ex.
#include<studio.h>
int main ( )
{
int a,b,c, choice;
while (choice!=3)
{
printf ("\n 1. Addition");
printf ("\n 2. Subtraction");
printf(" \n Enter your choice");
scanf("%d", & choice);
switch (choice)
{
case 1:
printf("Enter 2 numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("%d",c);
break;
case 3:
printf("Enter 2numbers");
scanf("%d%d",c);
c=a+b;
printf("%d",c);
break;
default:
printf("wrong choice");
}
}
}
In this tutorial we learn about what is loop statement , how loop statement works in the program , in the case you want to repeated a some statements in the program for condition you use loop statement
Looping Statements:-
What is Loop in C?
Looping Statements in C execute the sequence of statements many times until the stated condition becomes false. A loop in C consists of two parts, a body of a loop and a control statement.
Definition of Loop:-
in loop ,a program executes the sequence/block of statements many times until the stated condition become false.There are three types of loops available in C :-
While:-
In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed.
in while loop in c first check the condition if condition is true control goes to inside the while loop, execute the loop body statement. But condition is false the control goes to outside the body.
Syntax:-
while (Condition)
{
Loop body; // Statement;
increment or decrement (++ , --);
}
If while loop condition never false then loop become infinite loop.
Ex.
Program for reverse Integer:
in while loop in c first check the condition if condition is true control goes to inside the while loop, execute the loop body statement. But condition is false the control goes to outside the body.
Syntax:-
while (Condition)
{
Loop body; // Statement;
increment or decrement (++ , --);
}
If while loop condition never false then loop become infinite loop.
Ex.
Program for reverse Integer:
#include<studio.h>
{
int i=10;
while(i<=10)
{
printf("%d \n",i);
i++;
}
return 0;
}
for loop:-
In a for loop, the initial value is performed only once, then the condition tests and compares the counter to a fixed value after each iteration, stopping the for loop when false is returned.
for is a keyword.in c programming language for loop is used to repeat a block of statements for a given number of times until the given condition is false.
Syntax:-
The syntax of the for loop:-
for(initialization; test condition; increment/decrement operator)
{
//Statement 1;
//Statement 2;
//Statement 3;
//Statement n;
}
in for loop has the expressions separated by the semi-colons (denote by " ; " symbol ).
The flow of control ike
Initialization:-
in for loop start with the initialization statement. The initialization part or section is expected only once at the beginning at and initialization of counters variable is done first or set counter value.Test Condition:-
The value of the counter variable tested against the test Condition.Test Condition is true.it will execute the statement inside the loop, if the condition is false the for loop will be skips or terminate.
Increment / Decrement:-
In this expression executed after the end of each iteration. This operation helps to increase or decrease the counter variable as per requirement.In for loop condition is always true in such case the loop will ran infinite times.
Flowchart:-
Ex.
#include<studio.h>
int main()
{
For(i=0; i>0; i++)
{
printf("Saurabh1999.blogspot.com");
}
Ex.
#include<studio.h>
int main()
{
int i=10;
For(i=10; i>0; i--)
{
printf("%d \n", i);
}
return 0;
}
OUTPUT:-
10
9
8
7
6
5
4
3
2
1
do.... while loop :-
In a do...while loop, the condition is always executed after the body of a loop. It is also called an exit-controlled loop.
The do... while loop is similar to the while loop with one main difference. The body of do... while loop is expected at least once.Irrespective or test condition.
Syntax:-
do
{
// Loop body;
//Expressions;
}
while (expression);
Flowchart :-
Ex.
#include<studio.h>
int main()
{
int i=0;
do
{
printf ("Hi Friends");
i++;
}
while (i<1);
return 0;
}
Recommended Articles