0% found this document useful (0 votes)
46 views12 pages

Decision Making With Statement

The document discusses different forms of if statements in C programming: 1. Simple if statements execute code if a condition is true. 2. if-else statements execute one block of code if the condition is true and another block if false. 3. Nested if-else statements allow multiple conditions to be checked in sequence. 4. else-if ladders provide multiple alternative conditions that are checked in order until a true condition is found.

Uploaded by

mehedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views12 pages

Decision Making With Statement

The document discusses different forms of if statements in C programming: 1. Simple if statements execute code if a condition is true. 2. if-else statements execute one block of code if the condition is true and another block if false. 3. Nested if-else statements allow multiple conditions to be checked in sequence. 4. else-if ladders provide multiple alternative conditions that are checked in order until a true condition is found.

Uploaded by

mehedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Decision making with if statement

The if statement may be implemented in different forms depending on the complexity of


conditions to be tested. The different forms are,

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

The general form of a nested if...else statement is,


if( expression )
{
if( expression1 )
{
statement block1;
}
else
{
statement block2;
}
}
else
{
statement block3;
}
if expression is false then statement-block3 will be executed, otherwise the execution
continues and enters inside the first if to perform the check for the next if block, where
if expression 1 is true the statement-block1 is executed otherwise statement-block2 is
executed.
Example:
#include <stdio.h>

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

1. In if statement, a single statement can be included without enclosing it into curly


braces { ... }
2. int a = 5;
3. if(a > 4)
printf("success");

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");

In above example, hello will be printed.

5.Conditional Operators [ ?: ] : Ternary Operator Statement in C

1. They are also called as Ternary Operator .

2. They also called as ?: operator

3. Ternary Operators takes on 3 Arguments

Syntax :

expression 1 ? expression 2 : expression 3

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

2. If result of expression1 is TRUE then expression2 is Executed

3. Expression1 is said to be TRUE if its result is NON-ZERO

4. If result of expression1 is FALSE then expression3 is Executed

5. Expression1 is said to be FALSE if its result is ZERO

Live Example : Check whether Number is Odd or Even

#include<stdio.h>

int main()
{
int num;

printf("Enter the Number : ");


scanf("%d",&num);

flag = ((num%2==0)?1:0);

if(flag==0)
printf("\nEven");
else
printf("\nOdd");
}

More Simply we can write this program as –

#include<stdio.h>

int main()
{
int num;

printf("Enter the Number : ");


scanf("%d",&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;

printf("User, type in some intger value...\n");


scanf("%d", &flag);

if (flag == b) /* if flag is zero or false */


(!flag); /* set flag to 1 */

if (flag == a) /* if flag is 1 or true */


(flag); /* flag is True */

return 0;
}

You might also like