decision making statements

Decision Making Statements

http://install4install.com

Decision Making Statements are the statements that imposes one or more conditions on the expression and if the conditions evaluates to be true then specific actions are followed,if not true then some other actions are followed.They are called as decision making as it leads to a particular conclusion/decision.In C,true values are treated as non-zero i.e.1 and false values are treated as 0.The various types of decision making statements are :

  • if statement
  • if-else statement
  • nested if statement
  • switch

if statement

The if statement consists of boolean expression followed by group of statements.If the expression evaluates to be true then block of code inside if is executed otherwise nothing happens.

Syntax

if(boolean_expression)

{

//lines_of_code ;

}

Example

Say we need to find whether a child is eligible for admission in school on basis of age.If his age is greater than/equal to 5 but less than 6 then he is eligible for admission otherwise not.To specify this in C programming language we can use if statement.

Program

#include <stdio.h>

int main() 
{
int age;
//printf("Enter the age of child");
scanf("%d",&age);
if((age>=5) && (age < 6))
{
printf("Chid is eligible for admission");
}
return 0;
}

Run

 

if-else statement

In if-else statement if the condition in if part evaluates to be true then code of lines in if block is executed otherwise code of lines in else part gets executed.

Syntax

if(boolean_expression)

{

code 1;

}

else

{

code 2;

}

Example

Say we need to find larger of two numbers and print the same.The bigger no. can be found out by using if-else statement.

Program

#include <stdio.h>

int main() 
{
	int a;
	int b;
	//printf("Enter two numbers :");
 	scanf("%d",&a);
 	scanf("%d",&b);
	if(a > b)
	{
		printf("%d is bigger",a);
	}
	else
	{
		printf("%d is bigger",b);
	}
	return 0;
}

Run

 

Nested if statement

In nested if statement,one or more if statements are inside another if statement.

Syntax

if(boolean_expression_1)

{

 if(expression 2)

{

lines_of_code

}

if(expression 3)

{..}

}

Program

#include <stdio.h>

int main()
{
int marks;
//printf("Enter the marks of child");
scanf("%d",&marks);
if((marks >=50) && (marks <=100))
{
	if((marks >=50) && (marks <=60))
	{
	printf("Child needs improvement");
	}
	if((marks >60) && (marks <=70))
	{
		printf("Child can do better");
	}
	if((marks >70) && (marks <=80))
	{
		printf("Good Attempt");
	}
	if((marks >80) && (marks <=100))
	{
		printf("Excellent");	
	}

}
else
{
	printf("Failed");
}
	return 0;
}

Run

 

switch statement

The functionality of switch statement is same as of if-else statement.It uses cases for different conditions and code of lines are executed according to the case (whichever is true).Each case has a break statement so that control gets transferred (if case is valid)to out of switch block and other false cases are not executed.It has a default case where code of lines are executed if none of the cases satisfy the mentioned conditions.The expression of switch is the variable on which conditions are based.Constant expressions of cases must match the data type of expression mentioned in switch.

Syntax

switch(expression)

{

case constant-expression 1:

set of statements;

break;

case constant-expression 2:

set of statements;

break;

..

default:

statements;

….

}

Example

#include <stdio.h>

int main() 
{
	char alphabet;
	//printf("Enter the alphabet:");
	scanf("%c",&alphabet);
	switch(alphabet)
	{
		case 'a':
		case 'A':
		printf("%c is a vowel",alphabet);
		break;
			
		case 'e':
		case 'E':
			printf("%c is a vowel",alphabet);
		break;
			
		case 'i':
		case 'I':
			printf("%c is a vowel",alphabet);
		break;
		
		case 'o':
		case 'O':
			printf("%c is a vowel",alphabet);
		break;
		
		case 'u':
		case 'U':
			printf("%c is a vowel",alphabet);
		break;
		default:
		printf("%c is not a vowel",alphabet);
		
	}

	return 0;
}

Run

 


Short URL: http://tinyit.cc/298821