Explain the working of IF condition block with one example.
Answers
Answer:
The IF statement works by checking the expression to see whether a condition is met and returns a value based on the output obtained. For example, based on the criteria, it returns one predetermined value if the condition is found to be true and a different predefined value if the statement is found to be false.
Hope it helps you.. :)
Explanation:
Conditional Statements in C programming are used to make decisions based on the conditions. Conditional statements execute sequentially when there is no condition around the statements. If you put some condition for a block of statements, the execution flow may change based on the result evaluated by the condition. This process is called decision making in 'C.'
In 'C' programming conditional statements are possible with the help of the following two constructs:
1. If statement
2. If-else statement
It is also called as branching as a program decides which statement to execute based on the result of the evaluated condition.
In this tutorial, you will learn-
What is a Conditional Statement?
If statement
Relational Operators
The If-Else statement
Conditional Expressions
Nested If-else Statements
Nested Else-if statements
If statement
It is one of the powerful conditional statement. If statement is responsible for modifying the flow of execution of a program. If statement is always used with a condition. The condition is evaluated first before executing any statement inside the body of If. The syntax for if statement is as follows:
if (condition)
instruction;
The condition evaluates to either true or false. True is always a non-zero value, and false is a value that contains zero. Instructions can be a single instruction or a code block enclosed by curly braces { }.
Following program illustrates the use of if construct in 'C' programming:
#include<stdio.h>
int main()
{
int num1=1;
int num2=2;
if(num1<num2) //test-condition
{
printf("num1 is smaller than num2");
}
return 0;
}
Output:
num1 is smaller than num2
The above program illustrates the use of if construct to check equality of two numbers.
In the above program, we have initialized two variables with num1, num2 with value as 1, 2 respectively.
Then, we have used if with a test-expression to check which number is the smallest and which number is the largest. We have used a relational expression in if construct. Since the value of num1 is smaller than num2, the condition will evaluate to true.
Thus it will print the statement inside the block of If. After that, the control will go outside of the block and program will be terminated with a successful result.
Relational Operators
C has six relational operators that can be used to formulate a Boolean expression for making a decision and testing conditions, which returns true or false :
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
Notice that the equal test (==) is different from the assignment operator (=) because it is one of the most common problems that a programmer faces by mixing them up.
For example:
int x = 41;
x =x+ 1;
if (x == 42) {
printf("You succeed!");}
Output :
You succeed
Keep in mind that a condition that evaluates to a non-zero value is considered as true.
For example:
int present = 1;
if (present)
printf("There is someone present in the classroom \n");
Output :
There is someone present in the classroom