You are on page 1of 13

Decisions

In programming situations , we want the instructions to be executed sequentially. When we want a set of instructions to be executed in one situation, and an entirely different set of instructions to be executed in another situation. This kind of situation is dealth in C programs using a decision control instruction. Decision control can be implemented in C using: The if statement The if-else statement

THE IF STATEMENT :
Like most languages , C uses the keyword if to impliment the decision control instruction . The general form of if statement looks like this: if ( this condition is true) execute this statement; The keyword if tells the compiler that what to follow , this is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses( ). If the condition , whatever it is, is true,then the statement is executed. If the condition is not true ; the program skips it.

RELATIONAL
THIS EXPRESSION

OPERATORS
Is true if

x==y
x!=y x<y x>y x<=y x>=y

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

Note: = is used for assigning purpose. == is use for comparision of two quantities.
Q: If you purchase items 15 or more you get a discount of 10%. Write a programme to calculate total expense..

Output:

Multiple statements using if : If in a program we want more than one statement to be executed, if the condition following if is satisfied. If multiple statements are to be executed then they must be placed within a pair of braces.

Output:

If-else statement :
The if statement by itself will execute a single statement, or a group of statements, when the condition is true and does nothing when the condition is false. We can execute one group of statements when the condition is true and another when the condition is false. This is the purpose of else statement.

Output 1:

Output 2:

FEW POINTS

The group of statement after if upto else (excluding else) is called an if block.Similarly, the statements after the else form the else block. Else is written exactly below if . The if block and else block are always indented. If there is one statement to be executed in if block and else block then we can drop pair of braces.

Nested if-elses : To write entire if-else statement in the body of if statement or else statement is called nesting of ifs.

You might also like