Decision Control Structure
Decision Control Structure
Decisions ! Decisions !
if(this condition is true) execute this statement. The keyword if tells the compiler that what
follows, is a decision control instruction. The condition is specified using Relational Operators
Expression x==y
x != y x<y x>y x <= y x >= y
Is true if x is equal to y
x is not equal to y x is less than y x is greater than y x is less than or equal to y x is greater than or equal to y
Forms of if
1) if(condition) do this; 2) if(condition) { do this; and this; } 3) if(condition) do this; else do this; 4) if(condition) { do this; and this; } else { do this; and this; }
Forms of if..(contd)
5) if(condition) do this; else { if(condition) do this; else { do this; and this; } } 6) if(condition) { if(condition) do this; else { do this; and this; } } else do this;
The first two are composed of double symbols. Dont use single symbol | and &. These are bitwise operators. The first two operators, && and ||, allow two or more conditions to be combined in an if statement.
The conditional operators ? and : are sometimes called ternary operators since they take three arguments. General Form:
expression 1 ? expression 2 : expression 3
Example
int x,y; scanf(%d,&x); y=(x > 5 ? 3 : 4)
(i == 1 ? printf(Hi) : printf(hello));
The limitation of the conditional operator is that after the ? Or after the : only one C Statement can occur.