Decision Making With Statement
Decision Making With Statement
1. Simple if statement
2. if....else statement
3. Nested if....else statement
4. Using else if statement
1.Simple if statement
The general form of a simple if statement is,
if(expression)
{
statement inside;
}
statement outside;
If the expression returns true, then the statement-inside will be executed,
otherwise statement-inside is skipped and only the statement-outside is executed.
Example:
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 13;
if (x > y )
{
printf("x is greater than y");
}
}
x is greater than y
IF Condituoinal
2.if...else statement
Example:
The general form of a simple if...else statement is,
if(expression)
{
statement block1;
}
else
{
statement block2;
}
If the expression is true, the statement-block1 is executed, else statement-block1 is
skipped and statement-block2 is executed.
Example:
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}
y is greater than x
3.Nested if....else statement
void main( )
{
int a, b, c;
printf("Enter 3 numbers...");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
}
}
4.else if ladder
The general form of else-if ladder is,
if(expression1)
{
statement block1;
}
else if(expression2)
{
statement block2;
}
else if(expression3 )
{
statement block3;
}
else
default statement;
The expression is tested from the top(of the ladder) downwards. As soon as
a true condition is found, the statement associated with it is executed.
Example :
#include <stdio.h>
void main( )
{
int a;
printf("Enter a number...");
scanf("%d", &a);
if(a%5 == 0 && a%8 == 0)
{
printf("Divisible by both 5 and 8");
}
else if(a%8 == 0)
{
printf("Divisible by 8");
}
else if(a%5 == 0)
{
printf("Divisible by 5");
}
else
{
printf("Divisible by none");
}
}
FlowChart :
Points to Remember
No curly braces are required in the above case, but if we have more than one statement
inside ifcondition, then we must enclose them inside curly braces.
4. == must be used for comparison in the expression of if condition, if you use = the
expression will always return true, because it performs assignment not comparison.
5. Other than 0(zero), all other values are considered as true.
6. if(27)
printf("hello");
Syntax :
where
expression1 is Condition
expression2 is Statement Followed if Condition is True
expression2 is Statement Followed if Condition is False
Conditional Statement in C Programming Lanuage
Meaning of Syntax :
1. Expression1 is nothing but Boolean Condition i.e it results into either TRUE or FALSE
#include<stdio.h>
int main()
{
int num;
flag = ((num%2==0)?1:0);
if(flag==0)
printf("\nEven");
else
printf("\nOdd");
}
#include<stdio.h>
int main()
{
int num;
(num%2==0)?printf("Even"):printf("Odd");
6.Boolean
Standard C (since C99) provides a boolean type, called _Bool . By
including the header stdbool.h , one can use the more intuitive
name bool and the constants true and false . ... Objective-C also has a
separate Boolean data type BOOL , with possible values being YES or
NO , equivalents of true and false respectively.
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int flag;
bool a=true, b=false;
return 0;
}