Day 11: Decision making statements : If , If..Else.., if..Else if ...else if, else..

 Decision-Making Statement: in c lang

The Keywords which are used to check a certain condition and

evaluate the expression to be either accurate or false is called a Decision-Making statement.

Day 11: Decision making statements : If , If..Else.., if..Else if ...else if, else..


Decision-making Statements

Decision-making statements, also known as conditional statements, are statements in programming languages that allow the program to make decisions based on certain conditions.

Types of decision-making statements

------

decision-making statement


1. if

"if" is used to check a Condition, It evaluates and results either

in "true" or in "false". The "if" part of the code is executed only if

the condition becomes true. 

 

Syntax:

if(Condition)

{

Statement(s);

Expression(s);

}

2. if..else

The result of the "if" Statement is either "true" or "false". When 

the result is "true", "if" will be executed. When the result is "false"

else will be performed.

Syntax:

if(Condition)

{

Statement(s);

Expression(s);

}

else 

{

Statement(s);

Exression(s);

}

Note: 

"else" is optional


Recommended for you: Day 10: Math. h header file detailed review and important functions in Math. h.


3. if..else if..else

In Some cases, a number of conditions to evaluate and perform

a different action on each condition.

When "if" is false in a condition it will check another condition 

using "else if". Any number of "else if" can be used to 

check conditions. It should be ended with "else".

Syntax:

if(Cond1)

{

}

else if(Cond2)

{

}

else if(Cond3)

{

}

.

.

.

else

{

}


4. switch case

switch case is similar to if..else if..else only the difference

is switch evaluate multiple cases at a time? if..else if. else can

evaluate any one block of code.

switch case contains a "default" case similar to "else".

"default" is optional in the switch case

Syntax:

switch(CaseNo/Char)

{

case No/Char:

case No/Char:

.

.

default:

}

Note:

In the "switch" case, the Case should be a Constant Number or 

Character


decision making statement


Basic to advance answers and equations on decision-making statements.

//*1}
  
 /* WAP to input any Character from User and check that it is Vowel 
   or Consonant */
#include<stdio.h>

int main()
{
char ch; // 1 byte  -128 to +127
printf("\n Enter any character :");
scanf("%c",&ch);// e
if(ch=='a')
{
printf("\n a is Vowel");
}
else if(ch=='e')
{
printf("\n e is Vowel");
}
else if(ch=='i')
{
printf("\n i is Vowel");
}
else if(ch=='u')
{
printf("\n u is Vowel");
}
else if(ch=='o')
{
printf("\n o is Vowel.");
}
else
{
printf("\n %c is Consonent",ch);
}
return 0;
}

/* Ass: WAP to input gender and age of an Employee. As per the following
   condition check that the Employee is eligible for the insurance or not 
   1. Employee is Male and Age is >35
   2. Employee is Female and Age >25 */


/* WAP to input account type, and inial amount and check that the bank account
   opened by the customer is a valid bank account or not.
   1. if the account type is "saving" then the initial amount should be 
      Minimum Rs. 500
   2. if the account type is "current" then the initial amount should be
      Minimum Rs. 10,000
   3. if the account type is "loan" then the loan amount should be
      Minimum Rs. 1,00,000. */
      
#include<stdio.h>
#include<string.h>

int main()
{
char acctype[20];
long inibal;
printf("\n Enter Account Type [saving/current/loan] :");
scanf("%s",&acctype);
printf("\n Enter Initial Amount in INR :");
scanf("%ld",&inibal);
/* Note:
In String, using equality operator (==) String can not be
compared. the function strcmp() from string.h header can be
used. */
// syntax: strcmp(src_str,des_string)
if(strcmp(acctype,"saving")==0)
{
if(inibal>=500)
{
printf("\n Account is Valid.");
}
else
{
printf("\n Account is Not Valid.");
}
}
else if(strcmp(acctype,"current")==0)
{
if(inibal>=10000)
{
printf("\n Account is Valid.");
}
else
{
printf("\n Account is Not Valid.");
}
}
else if(strcmp(acctype,"loan")==0)
{
if(inibal>=100000)
{
printf("\n Account is valid.");
}
else
{
printf("\n Account is not valid.");
}
}
else
{
printf("\n Account is not Valid.");
}
return 0;
}

/* 
Note:
strcmp() function compares two distinct strings and returns 0 if
they are the same otherwise returns Non zero value */



************************

/* WAP to input any Number and Check that it is Positive, negative
   or Zero */

////#include<stdio.h>
////
////int main()
////{
// int no;
// printf("\n Enter any Number :");
// scanf("%d",&no);//56
//
// if(no>0)
// {
// printf("\n Number is Positive");
// }
//
// if(no<0)
// {
// printf("\n Number is Negative");
// }
//
// if(no==0)
// {
// printf("\n Number is Zero");
// }
//
// return 0;
//}


#include <stdio.h>

int main(){
int no ;
printf("Enter Any Number");
scanf("%d",&no);
if(no>0)
{
printf("Number Is positive");
}
if(no<0){
printf("Number is Negative");
}
if(no==0)
{
printf("Number is 0");
}
// return 0;
}



************************


/* WAP to input Two Numbers and Check that which is Max */

#include<stdio.h>

int main()
{
int a,b;
printf("\n Enter values of a and b :");
scanf("%d%d",&a,&b); //74 56
if(a>b)
{
printf("\n a is Max");
}
else // else is the false part of the code
{
printf("\n b is Max");
// Ass: Check for the same numbers
}
return 0;
}




**********************

/* WAP to input any Number and check that it is Even or Odd */

#include<stdio.h>

int main()
{
int no;
printf("\n Enter any number :");
scanf("%d",&no);
if(no%2==0)
{
printf("\n Number is Even");
}
else
{
printf("\n Number is Odd");
}
return 0;
}



**********************


/* WAP to input any Three Digit Number and check that it is Palindrome
   Number or not */
   
#include<stdio.h>

int main()
{
int no,rem, rev,temp;
printf("\n Enter any three digit Number :");
scanf("%d",&no);//457 --> 45 --> 4
temp = no; 
rem = no%10; //7
rev = rem;
no = no/10;
rem = no%10; //5
rev = rev*10+rem;
no = no/10; 
rem = no%10;  //4
rev = rev*10+rem;
if(temp==rev)
{
printf("\n Number is Palindrome");
}
else
{
printf("\n Number is not Palindrome");
}
return 0; 
}

/* Ass: WAP to input any Three Digit Number and check that it is 
   Armstrong number or not */
   


/* WAP to input Students Percentage and Display Pass or Fail. */

#include<stdio.h>

void main()
{
float avg_marks;
printf("\n Enter Percentage :");
scanf("%f",&avg_marks);
if(avg_marks>=35)
{
printf("\n Congradulations! You are Pass....");
}
else
{
printf("\n Sorry! You are Fail.");
}
}

/* Ass: WAP to input Age of two Childres and Display that who is Elder and Who is Younger */
\\



Summary of Decision-making statements

These decision-making statements in C provide you with the flexibility to control the program's behavior based on specific conditions, allowing you to make informed decisions and perform different actions accordingly.



   
   




Post a Comment

Previous Post Next Post