Sei sulla pagina 1di 4

getch();

Decision Making- If statement in C++ (OOPs)


It is used whenever any condition is to be implemented by the user it
means some conditions like:
1. If the price of the item is more than 5000 then discount is 10%.
2. If the students marks are greater than 60% then he should be in 1st
division.
For all these conditions a decision making statement called if
statement is used.
if statement is that statement which executes the expression given
with if.
It will only execute if its true.
But if the expression given with if statement is not true then this
statement does not contribute to the result i.e. output.
Here keyword if is used.
/* Program to calculate simple interest having varying time period
using IF statement*/
#include<iostream.h>
#include<conio.h>
main()
{ float princ,time,rate=2.5,si;
cout<<"\n Enter Principle and Time period of loan";
cin>>princ>>time;
if(time>=5)
{
rate=5.5;
si=(princ*rate*time)/100.0;
}
si=(princ*rate*time)/100.0;
cout<<"\n Simple interst of the loan is: "<<si<<" with Rate of
interest of: "<<rate;
getch();
}
* Program to enter student details and allow grade according to its
performance*/
#include<iostream.h>
#include<conio.h>
main()
{
int roll;

If else statement in C++ (OOPs)


This statement is also a conditional statement. It starts
with keyword if followed by an expression followed by
first statement and second statement is given
after else clause.
Format:
if(expression)
statement1;
else
statement2;
if the expression is true then statement1 will be executed.
If the expression is false then statement2 will be executed.
Similarly, if there are more statement before else then a
block is to be formed and similarly if there are many
statements after the else clause then its separate block is
formed.
Such statement is called Compound if else statement.

/* Program to enter two values and find which is greater*/


#include<iostream.h>
#include<conio.h>
main()
{
int n1,n2;
cout<<"\n Enter any two number you want to
check";
cin>>n1>>n2;
if(n1>n2)
{
cout<<"\n "<<n1<<" is greater than
"<<n2;
}
else
{
cout<<"\n "<<n2<<" is greater than "<<n1;
}
getch();
}

int s1,s2,s3,s4,s5;
float per;

/*Program to enter item details and print its bill with complete
details according to the discount offered to the customer */
#include<iostream.h>

char name[20];
#include<conio.h>
cout<<"\n Enter student's Name and Roll number";
main()
cin>>name>>roll;
{
cout<<"\n Enter student marks in Physics, Chemistry, Maths,
English, Physical";

int itcd;

cin>>s1>>s2>>s3>>s4>>s5;

float pr,qty,d,dp,amt,tamt;

per=(s1+s2+s3+s4+s5)/5.0;

char itnm[10];

cout<<"\n Name: "<<name;

cout<<"\n Enter Item's Name and Code";

cout<<"\n Roll Number: "<<roll;

cin>>itnm>>itcd;

cout<<"\n Percentage: "<<per;


if(per>=80.0)
{

cout<<"\n Enter Item's Quantity(in Kg) and


Price";
cin>>qty>>pr;
amt=pr*qty;

cout<<"\n Student is Above Average";


}

if(amt>=500)
{

d=12.5;
dp=amt*d/100;
tamt=amt-dp;
cout<<"\n
Code\tName\tQty.\tPrice\tAmount\tDis%\tT.Amount";
cout<<"\n
"<<itcd<<"\t"<<itnm<<"\t"<<qty<<"\t"<<pr<<"\t"<<am
t<<"\t"<<d<<"\t"<<tamt;
}
else
{
d=7.5;
dp=amt*d/100;
tamt=amt-dp;
cout<<"\n
Code\tName\tQty.\tPrice\tAmount\tDis%\tT.Amount";
cout<<"\n
"<<itcd<<"\t"<<itnm<<"\t"<<qty<<"\t"<<pr<<"\t"<<am
t<<"\t"<<d<<"\t"<<tamt;
}
getch();
}

Decision making- Nested If else


This statement can be used to select any one statement or a block
which contains a set of instructions. Only that statement will execute
which is true according to the data entered and rest other conditions
will not executed.
FORMAT:
if(expression1)
Statement1;
else if(expression2)
statement2;
else if(expression3)
statement3;

/*Program to enter students name, roll number and its percentage and
allot his division*/
#include<stdio.h>
#include<conio.h>
void main()
{
int roll;
char nm[40];
float percent;
printf("\n enter students name and roll number");
scanf("%s%d",nm,&roll);
printf("\n enter percentage of the student");
scanf("%f",&percent);
if(percent>=60.0)
printf("\n student obtain first division");
else if(percent>=50.0 || percent<60.0)
printf("\n student obtain second division");
else if(percent>=35.0 || percent<50.0)
printf("\n student obtain third division");
else
printf("\n student is fail");
getch();
}

Switch Statement in C++ (OOPs


This statement is also used for decision making. Here key word is
switch followed by control variable and followed by its block.
In the block of switch statement multiple cases are used and each ends
with break statement this is also called as early exit loop.
Out of all the cases only that case will run whose value matches with
the control variable.

/* Program to make a calculator using switch statement and apply such


condition that it will terminate by the users wish */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int add,sub,mul,mod,a,b,n;
float div;
cout<<"\n Enter any two values";
cin>>a>>b;
do
{
cout<<"\n Enter any operation:";
cout<<"\n 1: Addition\n 2: Subtraction\n 3: Multiplication\n 4:
Division\n 5: Remainder\n 6:Exit\n";
cin>>n;
switch(n)
{
case 1:
{
add=a+b;
cout<<"\n\n Addition of "<<a<<" and "<<b<<" is "<<add;
break;
}
case 2:
{
sub=a-b;
cout<<"\n\n Subtraction of "<<a<<" and "<<b<<" is "<<sub;
break;
}
case 3:
{
mul=a*b;
cout<<"\n\n Multiplication of "<<a<<" and "<<b<<" is "<<mul;
break;
}
}
case 4:
{
div=float(a)/b;
cout<<"\n\n Division of "<<a<<" and "<<b<<" is "<<div;
break;
}
case 5:
{
mod=a%b;
cout<<"\n\n Remainder of "<<a<<" and "<<b<<" is "<<mod;
break;
}
case 6:
exit(0);
default:
cout<<"\n Enter correct operation";
}
}
while(n=6);
getch();
}

While & Do-while in C++ (OOPs)

r=n%10;

These are used for repeating some portion of the program either
by specified number of times or until a particular condition is true.
1. while statement
2. do while statement
3. for loop

n=n/10;
sum=sum+r;

while loop
This loop is that in which we can execute a set of statements again and
again starting from given starting value of the variable up-to that value
of the variable which is given in the termination condition.
While loop starts with the the keyword while followed by expression
and a block is used there after it includes set of statements. Its format
is:
while(expression)
{
Statement1;
Statement2;
Statement3;
}
In this while loop starting value of the variable is initialized above the
loop. Termination condition is given with the while keyword and
increment and decrement of the variable takes place inside the loop.

}
cout<<"\n Sum of digits of number "<<n1<<" is "<<sum;
getch();
}

Do while looping
This loop is also an inbuilt structure in the language and it is just like for
loop and while loop. But this loop is different from while loop because
while loop checks the terminating condition at the top but this loop checks
the condition at the bottom. This loop is a free entry loop and unlike while
loop this loop will execute at least once even if the condition is false.
Some basic difference is:
/* Program to enter limit and find sum of series upto that limit */

/* Program to enter limit by the user and find sum upto limit */

#include<iostream.h>
#include<conio.h>
main()
{
int n,sum=0,r,n1;
cout<<"\n Enter any number whose sum is to be found";
cin>>n;
n1=n;
while(n>0)
{
r=n%10;
n=n/10;
sum=sum+r;
}
cout<<"\n Sum of digits of number "<<n1<<" is "<<sum;
getch();

#include<iostream.h>
#include<conio.h>
main()
{
int n,sum=0,r,n1;
cout<<"\n Enter any number to find its digit sum";
cin>>n;

}
n1=n;
/* Program to enter a number and find sum of its digits*/
do
#include<iostream.h>
{
#include<conio.h>
r=n%10;
main()
n=n/10;
{
sum=sum+r;
int n,sum=0,r,n1;
}
cout<<"\n Enter any number whose sum is to be found";
while(n>0);
cin>>n;
cout<<"\n Sum of digits of "<<n1<<" is "<<sum;
n1=n;
getch();
while(n>0)
}
{

for loop in C++ (OOPs)


for loop is also structure control loop as program will run or execute
if the initial condition is true otherwise it will terminate and program
will run until some condition is fulfilled.
FORMAT:
for(expression1;expression2;expression3)
{
Statement1;
Statement2;
}
for loop is used in those problems where the number of times the
body of the loop is to be executed is known in advance.
Here expression1 is used to give initial value to the loop control
variable.
Here expression2 represents some test condition which should be true
before executing the body of the loop
Here expression3 changes the value of the loop control variable i.e.
increment or decrement.
/* Program to find factorial of number using for loop*/
#include<iostream.h>
#include<conio.h>
main()
{
int n,i,fact=1;
cout<<"\n Enter any number whose factorial is to be find";
cin>>n;
for(i=n;i>0;i--)
{
fact=fact*i;
}
cout<<"\n Factorial of "<<n<<" is "<<fact;
getch();
}

Potrebbero piacerti anche