Lecture 7 Decision Making Statements
Lecture 7 Decision Making Statements
Making
Statements
C++
Lecture 7
if statement
• If condition is false then those commands are ignored and are not
executed.
if(boolean_expression)
{
// statement(s) will execute if the
boolean expression is true
}
else{
……………….
}
#include <iostream>
using namespace std;
int main ()
{
int a = 10;
if( a < 20 )
{cout << "a is less than 20;" << endl;
}
cout << "value of a is : " << a << endl; return 0;
}
if statement #include<iostream.h>
#include<conio.h>
void main(void)
{ clrscr();
int a;
cout<<"Enter value";
cin>>a;
if(a>=10)
{
cout<<“BSCS";
}
else
{
cout<<“MCS";
}
getch();
}
• int main ()
• { int a = 100;
• if( a == 10 )
• {
• cout << "Value of a is 10" << endl; }
• else if( a == 20 )
• { // if else if condition is true
• cout << "Value of a is 20" << endl;
• } else if( a == 30 )
• {
• cout << "Value of a is 30" << endl; }
• else
• cout << "Value of a is not matching" << endl;
• }
• cout << "Exact value of a is : " << a << endl;
• return 0;}
Operation of if else statement
Operation of if else statement
if(boolean_expression 1)
{ // Executes when the boolean expression 1 is true }
else if( boolean_expression 2)
{ // Executes when the boolean expression 2 is true }
else if( boolean_expression 3)
{ // Executes when the boolean expression 3 is true }
else { // executes when the none of the above condition is
true. }
Write a program in C++ to find if an
integer is even or odd or neither (0)
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
if ((num % 2) == 0)
{
cout << "The number is even." << endl; }
else if ((num % 2) != 0)
{ cout << "The number is odd." << endl; }
else
{
cout << "The number is 0 and it is neither even nor odd." << endl; } }
Write a program that takes the value
from the user between 1 to 99999 and
determine the following cases.
If the value is less than 100 and greater then equal to 1 the it will
be two digit number. otherwise If the value is less than 1000 and
greater then equal to 100 the it will be three digit number.
otherwise If the value is less than 10000 and greater then equal to
1000 the it will be four digit number. otherwise If the value is less
than 100000 and greater then equal to 10000 the it will be five
digit number. otherwise the number does not lie between the
range.
int main(){
int num;
cout<<"Enter an integer number between 1 & 99999: ";
cin>>num;
if(num <100 && num>=1)
{ cout<<"Its a two digit number"; }
else if(num <1000 && num>=100)
{ cout<<"Its a three digit number"; }
else if(num <10000 && num>=1000)
{ cout<<"Its a four digit number"; }
else if(num <100000 && num>=10000)
{ cout<<"Its a five digit number"; }
else
{ cout<<"number is not between 1 & 99999"; }
}
Nested if statement
#include<iostream.h>
#include<conio.h>
void main(void)
Output
{ clrscr();
int a, b, c; Enter values for A, B and C 5 5 8
cout<<"Enter values for A, B and C "; B and C are not Equal
cin>>a>>b>>c;
if(a==b) Enter values for A, B and C 5 3 3
{
if(b==c)
A and B are not Equal
{
cout<<"A. B and C are Equal"; Enter values for A, B and C 4 4 4
} A. B and C are Equal
else
{
cout<<"B and C are not Equal"; Enter values for A, B and C 8 6 8
} A and B are not Equal
}
else
{
cout<<"A and B are not Equal";
}
getch();
#include<iostream.h>
void main()
{ int a=45, b= 89, c=78;
if (a>b)
{ if (a>c)
cout << "\n\nA is Greatest";
else
cout << "\n\nC is Greatest";
}
else { if (b>c)
cout << "\n\nB is Greatest";
else
cout << "\n\nC is Greatest";
}
}
Counting words and characters with different technique
#include<iostream.h>
#include<conio.h>
void main(void)
Output
{ hello how are you
int chcount=0; Words = 4
int wdcount=1;
Characters = 14
char ch;
clrscr();
cout<<“enter string”;
cin>>ch;
while((ch=getche()) !='\r')
{ if(ch==' ')
wdcount++;
else
chcount++;
}
cout<<"\nWords = "<<wdcount;
cout<<"\nCharacters = "<<chcount;
getch();
}
Counting words and characters with different technique
#include <iostream>
using namespace std;
int main() {
int chcount = 0;
Output
int wdcount = 1;
hello how are you
char ch;
Words = 4
cout << "Enter string: "; Characters = 14
while (true) {
ch = cin.get();
if (ch == '\n') break; // Break loop on Enter key (newline)
if (ch == ' ')
wdcount++;
else
chcount++;
}
cout << "\nWords = " << wdcount;
cout << "\nCharacters = " << chcount;
return 0;
}
The switch statement
• The switch statement is similar to the if else or else if construct but is
more flexible.
• If decision tree is large, and all the decisions depend on the value of the
same variable, then it is better to use switch statement instead of series of
if else or else if statements.
switch(expression)
{
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
The switch statement
#include<iostream.h>
#include<conio.h>
void main(void)
{ clrscr();
char ch;
cout<<“Enter character”;
cin>>ch;
switch(ch)
{
case ‘b':
cout<<“BSCS”<<endl;
break;
case ‘m':
cout<<“MCS”<<endl;
break;
default:
cout<<“BSCS and MCS";
}
int day = 3;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
default:
cout << "Looking forward to the Weekend";
}
The break statement
• The break keyword causes the entire switch statement
to exit. Control goes to the first statement following
the end of the switch statement.
• If break statement is not used then the control passes
down to the next case statement and the statements that
we do not want to execute, starts executing.
• If the value of the switch variable doesn’t match any of
the case constants then control passes to the end of the
switch without doing anything
#include <iostream>
using namespace std;
int main(){
int num=5;
switch(num+2) {
case 1:
cout<<"Case1: Value is: "<<num<<endl;
case 2:
cout<<"Case2: Value is: "<<num<<endl;
case 3:
cout<<"Case3: Value is: "<<num<<endl;
default:
cout<<"Default: Value is: "<<num<<endl;
}}
Find the output of the following code.
#include <iostream> Output:
using namespace std; Case2
int main(){ Case3
int i=2; Case4
switch(i) Default
{
case 1: cout<<"Case1 "<<endl;
case 2: cout<<"Case2 "<<endl;
case 3: cout<<"Case3 "<<endl;
case 4: cout<<"Case4 "<<endl;
default: cout<<"Default "<<endl;
}
return 0;
}
Find the output of the following code.
int main() {
char choice;
switch (choice) { Now here if we enter the choice
Y then there should be a break
case 'N':
statement to work the program
cout << "No";
accurately.
break;
case 'D':
cout << "Don't know";
break;
case 'Y':
cout << "Yes";
default:
cout << "N/A";
}
}
Example
int day = 2;
switch (________)
{
________1:
cout << "Saturday";
break;
________2:
cout << "Sunday";
________;
}
Defining Label and use
of switch, break and
goto statements
#include <iostream>
using namespace std;
int main()
{ char ch='b';
switch(ch)
{
case 'd': cout<<"Case1 "; break;
case 'b': cout<<"Case2 "; break;
case 'x': cout<<"Case3 "; break;
case 'y': cout<<"Case4 "; break;
default: cout<<"Default ";
} return 0;
}
switch(ch)
◼ Example {
#include<iostream.h> case '+':
cout<<a<<" + "<<b<<" = "<<a+b<<endl;
#include<conio.h>
break;
void main(void) // main start case '-':
{ clrscr(); cout<<a<<" - "<<b<<" = "<<a-b<<endl;
int a, b; break;
char ch; case 'x':
cout<<"Enter 1st value "; cout<<a<<" x "<<b<<" = "<<a*b<<endl;
cin>>a; break;
case '/':
cout<<"Enter 2nd value ";
cout<<a<<" / "<<b<<" = "<<a/b<<endl;
cin>>b; break;
again: default:
cout<<"+, -, x, / "; cout<<"Wrong Choice\n";
ch=getche(); goto again;
cout<<endl; }
getch();
} // main end
switch(ch)
EXAMPLE
Using multiple cases in switch
{
case '1':
statement case 'A':
case 'a':
cout<<a<<" + "<<b<<" = "<<a+b<<endl;
#include<iostream.h> break;
#include<conio.h> case '2':
case 'S':
void main(void)
case 's':
{ clrscr(); cout<<a<<" - "<<b<<" = "<<a-b<<endl;
int a, b; break;
case '3':
char ch; case 'M':
cout<<"Enter 1st value "; case 'm':
cout<<a<<" x "<<b<<" = "<<a*b<<endl;
cin>>a;
break;
cout<<"Enter 2nd value "; case '4':
cin>>b; case 'D':
case 'd':
cout<<"1. Add\n"; cout<<a<<" / "<<b<<" = "<<a/b<<endl;
cout<<"2. Subtract\n"; break;
cout<<"3. Multiply\n"; default:
cout<<"Wrong Choice\n";
cout<<"4. Divide\n"; goto again;
again: }
getch();
cout<<"Enter your choice (1-4) "; }
ch=getche();
The Conditional Operator
int x=3;
int a=12
while(x<=a)
{
x=x+4;
a++;
}
1. How many times this loop will be executed.
2. What will be the value of x be after the execution?
3. What will be the value of a be after the execution?
What is wrong with this code?
While(c<=5)
{
p=p*c;
c=c+1;
What is wrong with this code?
for(k=50,k=>1,--k);
cout<<k<<endl;
•;
int count=0;
do
{
cout<<“C++”;
} while(count++<=3)
What will be the value of variable s
after the following loop terminates.
int s=0;
int n =0;
do
{ n++;
s=+n;
if(s>=4) continue;
} while(n<5)
11
12
13
int i,j; 14
for(i=1;i<=3;i++) 21
{ for(j=1;j<=4;j++) 22
{ 23
cout<<”\n”<<i<<”\t“<<j;
24
}
}
31
32
33
34
int i,k; +
for(i=1;i<6;i++) ++
for(k=0;k<i;k++)
cout<<“+”; +++
cout<<endl; ++++
+++++
1
1 2
1 2 3
1 2 3 4
DRY RUN THE FOLLOWING
CODES
• You have take the value from the user and determine the grade of the
user.
• If the value range from 70 to 80 then mark A, if the value if between 60
to 70 then mark b.
• If the value is between 50 to 60 then mark 6 otherwise mark fail.