Sometimes we want to do a thing if conditions being met. For example sometimes, we want to watch comedy videos. On you tube or any other platform. But only if we are free. If we are busy then we’ll do our work. Hence, come condition applied. Similarly, we’ll order a cake if its someone’s birthday. In the same way there are decision making instructions in c. So in this article we are going to study concerning CONDITIONAL INSTRUCTIONS IN C LANGUAGE. So continuing further, about CONDITIONAL INSTRUCTIONS IN C LANGUAGE. These are of following types. Also we have already studied about C preprocessors.
TYPES OF CONDITIONAL INSTRUCTIONS :
- IF-else statement
- Switch statement
If-else statement:
The syntax of an if-else statement in C look like this:
If( condition to be checked) { statement if condition true;} else{ statement if condition false;}
for example:
int a= 23; if( a>18) { printf(" you can drive"); }
*else block is not necessary but optional.
Further let’s know about some relational operators and logical operators use in c language.
RELATIONAL OPERATORS IN C:
Relational operator are use to evaluate condition (true or false) inside the if statements. Some of them are: equals to (==) . Greater than( >). Greater than equals to( >=). Less than or equal to (<=). Not equal to (!=).
*Note- ‘=’ is use for an assignment. That means it use for assigning the values. Whereas, ‘==’ is use for equality check. Also, In c, a non-zero value is considered as true.
LOGICAL OPERATORS IN C :
&&, || and ! are the three logical operators. These are read as ” and” “or” “not”. They are use to provide logic to our programs.
USE OF LOGICAL OPERATORS:
- &&( AND) is true when both the conditions are true.
- ||(OR) is true when at least one of the two conditions are true.
- ! (NOT) returns true if given False. And False if given True.
Else-If clause:
Instead of using multiple if statements, we can use else-if. Along with if. Thus forming an if-else ladder.
SYNATX: If{ //statement;} else if { //statement; } else { //statement;}