Conditional Statements and Conditional Operator | C Weekly
Welcome back,
Today we are gonna talk about very interesting topic in programming - "Conditional Statements".
As we know that a computer program is a set of statements, in previous blog discusses some types of statements in C. Now take a look on Conditional statements.
if (test-expression)
{
True block of statements
}
Else
{
False block of statements
}
Statements;
if -else is the extended version of if statement. The statement inside the body of if executes when the condition is true otherwise the statement inside the else is execute.
if (test - expression 1) {
statement1;
} else if (test - expression 2) {
Statement2;
} else if (test - expression 3) {
Statement3;
} else if (test - expression n) {
Statement n;
} else {
default;
}
Statement x;
Let us see the actual working with the help of a program.#include
int main()
{
int marks=83;
if(marks>75){
printf("First class");
}
else if(marks>65){
printf("Second class");
}
else if(marks>55){
printf("Third class");
}
else{
print("Fourth class");
}
return 0;
}
Output:
First class
if (test - expression 1) {
statement1;
if (test - expression 2)
Statement2;
else
Statement 3;
}
else {
if (test - expression 3)
Statement3;
else
default;
}
(expression 1 ) ? (expression 2) : (expression 3)
In conditional operator first the expression 1 is evaluated if the result of expression 1 is true than the expression followed by '?' will execute (i.e. expression 2) , but if the result of expression 1 was false than the expression followed by ':' will execute(i.e. expression 3). This operator is very helpful to make your program compact.
For example:
#include
int main() {
int y;
int x = 2;
y = (x >= 6) ? 6 : x;/* This is equivalent to: if (x >= 5) y = 5; else y = x; */
printf("y =%d ",y);
return 0;}
Output :
y =2
Today we are gonna talk about very interesting topic in programming - "Conditional Statements".
As we know that a computer program is a set of statements, in previous blog discusses some types of statements in C. Now take a look on Conditional statements.
Conditional Statements:
Conditional statements are those statements which are based on some condition. For example you have some lines of code that you want to run only if a condition satisfy and you do not want that code to be run if the condition is not satisfy. But by default a C program execute in sequential manner that means one statement after another is execute as they were written. Consider the following example.
Statement 1;
Statement 2;
Statement 3;
Statement 4;
They are further divided into following categories-
Statement 1;
Statement 2;
Statement 3;
Statement 4;
First
the compiler execute statement 1 then statement 2 and so on. If you put
some condition for a block of statements the flow of execution might change
based on the result evaluated by the condition. This process is referred to as
decision making in 'C.' The decision-making statements are also called as
control statements or conditional statements.
1. Selective statements
2. Iterative statements
Selective Statements:
These statements are used for selecting some statements to execute. They are following-
- if statement
- if - else statement
- if -else if - else statement
- Nested if- else
- switch statement
- goto statement
if statement:
It is one of the powerful conditional statement. If statement is always used with a condition. The statement inside the body of the if is only executed when the condition is true. The condition is evaluated first before executing any statement inside the body of If. The syntax for if statement is as follows:
Following program illustrates the use of if construct in 'C' programming:
if - else statement:
if -else is the extended version of if statement. The statement inside the body of if executes when the condition is true otherwise the statement inside the else is execute.
Following programs illustrate the use of the if-else construct:
We will initialize a variable with some value and write a program to determine if the value is less than ten or greater than ten.
Let's start.
#include
int main()
{
int num =19;
if( num <10)
{
printf("The value is less than 10");
}
else
{
printf("The value is greater than 10");
}
return 0;
}
Output:
The value is greater than 10
Output:
if - else if - else statement:
when you have to check multiple conditions then if - else if - else statements are very useful.
Let us see the actual working with the help of a program.
Output:
Nested if-else statement:
You can either use if -else if -else statements or Nested if -else statements for multiple conditions.
when the if - else statements are placed inside the body of another if- else then it is called Nested if else statements. The syntax of Nested if else is following:
Conditional Operator:
C language provide a conditional operator or it is called as ternary operator also, Because it works with three operands. It is known as conditional operator because it provides the functionality of if - else statement. It consist of '?' and ':' , the syntax of this operator is:
In conditional operator first the expression 1 is evaluated if the result of expression 1 is true than the expression followed by '?' will execute (i.e. expression 2) , but if the result of expression 1 was false than the expression followed by ':' will execute(i.e. expression 3). This operator is very helpful to make your program compact.
For example:
Output :
We will discuss about switch and goto statements in our next blog. I hope this was helpful to you.
Comments
Post a Comment