C105: Condition
A conditional statement is a statement that has a condition. the condition returns true or false when evaluated. That means it is comparison statement or a logical statement. For example a > 6 where it returns true if value of a more than 6 otherwise it returns false.
Example for logical statement is TRUE && TRUE is TRUE
Comparison
Logical
Selection statement
if statement
if statement evaluates the condition to determine whether to execute the code block or not to. following is a syntax for the if condition.
example
this example will display the message because the condition 6 > 3 is TRUE.
The second code will not display the message because the condition is FALSE, so the code block is not entered.
More on conditional statement will be discussed in part 2.
Example for logical statement is TRUE && TRUE is TRUE
Comparison
Symbol | Means |
---|---|
> | more than if first operand is more than the second operand, it will return true. Otherwise it will return false 5 > 1 is TRUE 5 > 6 is FALSE |
< | less than if first operand is less than the second operand it will return true. Or else it will return false. 3 < 5 is TRUE 5 < 1 is FALSE |
>= | more than or equal if first operand is more than or equal to second operand, it will return true. Otherwise it will return false 4>=2 TRUE 4>= 6 FALSE 5 > 5 is FALSE but 5 >= 5 is TRUE |
<= | less than or equal if first operand is less than or equal to second operand, it will return true. Otherwise it will return false 4<=2 FALSE 4<= 6 TRUE 5 < 5 is FALSE but 5 <= 5 is TRUE |
== | equal if both the operands are equal it returns true, Otherwise it returns false. 3 == 3 is TRUE 2 == 3 is FALSE |
!= | not equal if both the operands are not equal it returns true, Otherwise it returns false. 3 != 4 is TRUE 2 != 2 is FALSE |
Logical
Symbol | Means |
---|---|
&& | AND if both operands are TRUE it returns TRUE. Otherwise it returns FALSE (5 > 2) && (10 > 6) is TRUE (5 < 2) && (10 > 6) is FALSE (5 > 2) && (10 < 6) is FALSE (5 < 2) && (10 < 6) is FALSE |
|| | OR if both operands are FALSE it returns FALSE. Otherwise it returns TRUE (5 > 2) && (10 > 6) is TRUE (5 < 2) && (10 > 6) is TRUE (5 > 2) && (10 < 6) is TRUE (5 < 2) && (10 < 6) is FALSE |
! | NOT if the operand is FALSE it returns TRUE, and if the operand is TRUE it returns FALSE. !(5>2) is FALSE !(5<2) is TRUE |
Selection statement
if statement
if statement evaluates the condition to determine whether to execute the code block or not to. following is a syntax for the if condition.
if(condition){
//code block
}
//code block
}
example
if(6 > 3){
printf("6 is more than 3");
}
printf("6 is more than 3");
}
this example will display the message because the condition 6 > 3 is TRUE.
if(6 < 3){
printf("6 is more than 3");
}
printf("6 is more than 3");
}
The second code will not display the message because the condition is FALSE, so the code block is not entered.
More on conditional statement will be discussed in part 2.
Comments
Post a Comment