Computer Programming Lecture4
Computer Programming Lecture4
Safa Hussain
Selection(conditional)statement
Conditional expressions are mainly used for decision making. C++ provides
multiple selection structures:
1)if statement
2)if…….else statements
3)Nested if statements
4)switch statement
If statement
The IF statement is used to express conditional expression. If the given
condition is true then it will execute the statements; otherwise it will execute
the optional statements.
1
Example:Write a C++ program to read any two numbers and print the
largest value of it:
The block IF statement are enclosed in ({) and (}) to group declaration and
statements into a compound statement or a block. These blocks are always
considered as a single statement. The structure is:
2
Example:Write a C++ program to read a number and check if it’s positive,
if it’s so print it, decrement it by 2:
#include<iostream>
using namespace std;
int main( )
{
int num;
cin>> num;
if(num>=0)
{
cout<<"positive"<<num<<endl;
num=num-2;
cout<<num;
}
return 0;
}
Example:Write a program that adds five (5) to the average student rate(45-
50)
#include<iostream>
using namespace std;
int main( )
{
float av;
cin>> av;
if(av>=45&&av<50)
{
av=av+5;
cout<<"success"<<av<<endl;
}return 0;
}
Assistant lecturer Safa Hussain
3
Example:write a c++ program to input integer number then print if it's consist
of 2 digit or not.
#include<iostream>
using namespace std;
int main( )
{
int x;
cin>>x;
if(x>9&&x<100)
cout<<"yes"<<x;
if(x<10||x>99)
cout<<"no"<<x;
return 0;
}
If……else statement
4
Example:Write a program that tests if the number is positive or negative
#include<iostream>
using namespace std;
int main( )
{
int a;
cin >> a;
if(a>=0)
cout<<"positive";
else
cout << "negative";
return 0;
}
#include<iostream>
using namespace std;
int main( )
{
int degree;
cin >> degree;
if (degree >= 50 )
cout << "pass";
else
cout << "fail";
return 0;
}
5
Example:write a program that tests if the number is even or odd number and
adds it 4 to even number only.
#include<iostream>
using namespace std;
int main( )
{
int x;
cin>>x;
if(x%2==0)
{
x=x+4;
cout<<"even"<<endl<<x;
}
else
cout<<"odd"<<endl<<x;
return 0;
}
Else if Statements:
if ( expression or condition 1 )
statement1 ;
else
if ( expression or condition 2 )
statement2 ;
else
if ( expression or condition 3 )
statement3 ;
:
.
.
Else
if ( expression or condition n )
statement-n ;
else
statement-e ;
6
Example:write a c++ program to input one character from the keyboard then
print type of character.
#include<iostream>
using namespace std;
int main( )
{
char x;
cin>>x;
if(x>='A'&&x<='Z')
cout<<"upper case"<<endl<<x;
else
if(x>='a'&&x<='z')
cout<<"lower case"<<endl<<x;
else
if(x>='0'&&x<='9')
cout<<"digital"<<endl<<x;
else
cout<<"special"<<endl<<x;
return 0;
}
Example:Write a c++ program to input the average for one student ,then print
grade of student.
#include<iostream>
using namespace std;
int main( )
{
float av;
cin>>av;
if(av>=50&&av<=59)
cout<<"your grade is pass"<<endl;
else
if(av>=60&&av<=69)
cout<<"your grade is medium"<<endl;
else
if(av>=70&&av<=79)
cout<<"your grade is good"<<endl;
else
if(av>=80&&av<=89)
cout<<"your grade is verygood"<<endl;
else
if(av>=90&&av<=100)
cout<<"your grade is excelent"<<endl;
else
cout<<"your grade is fail"<<endl;
return 0;
}
Assistant lecturer Safa Hussain
7
Example:write a c++ program to input one character and print the next
character according to the following table
#include<iostream>
using namespace std;
int main( )
{
char x;
cin>>x;
if(x>='A'&&x<='Y'||x>='a'&&x<='y'||x>'0'&&x<'8')
x++;
else
if(x=='Z')
x='A';
else
if(x=='z')
x='a';
else
if(x=='9')
x='0';
else
x='*';
cout<<x;
return 0;
}
8
#include<iostream>
#include<math.h>
using namespace std;
int main( )
{
float x,z;
cout << "Enter X value \n";
cin>>x;
if(x<0)
z=x+5;
else
if(x==0)
z=sin(x)+4;
else
z=sqrt(x);
cout << "z is " <<z;
return 0;
}
9
Example:Write C++ program to read integer number, and print the name of
the day in a week:
#include<iostream>
using namespace std;
int main( )
{
int day;
cout << "Enter the number of the day \n";
cin >> day;
switch (day)
{
case 1:
cout << "Sunday";
break;
case 2:
cout << "Monday";
break;
case 3:
cout << "Tuesday";
break;
case 4:
cout << "Wednesday";
break;
case 5:
cout << "Thursday";
break;
case 6:
cout << "Friday";
break;
case 7:
cout << "Saturday";
break;
default:
cout << "Invalid day number";
break;
}
return 0;
}
10
Example:Write C++ program to read two integer numbers, and read the
operation to perform on these numbers:
#include<iostream>
using namespace std;
int main( )
{
int a, b;
char x;
cout << "Enter two numbers \n";
cin >> a >> b;
cout << "+ for addition \n";
cout << "- for subtraction \n";
cout << "* for multiplication \n";
cout << "/ for division \n";
cout << "enter your choice \n";
cin >> x;
switch ( x )
{
case '+':
cout << a + b;
break;
case '-':
cout << a - b;
break;
case '*':
cout << a * b;
break;
case '/':
cout << a / b;
break;
default:
break;
}
return 0;
}
11
Example:Write a program to see if the entered character is a vowel or not.
#include<iostream>
using namespace std;
int main( )
{
char x;
cin>>x;
switch(x)
{
case'a':
cout<<"vowel";
break;
case'i':
cout<<"vowel";
break;
case'e':
cout<<"vowel";
break;
case'o':
cout<<"vowel";
break;
case'u':
cout<<"vowel";
break;
default:
cout<<"invaild vowel";
}
return 0;
}
12
Homework:
1- Write a c++ program to find a large number between three integer
number?
3- Input integer number which consist of 3 digits and find out the
maxiumum digit.
4- Write a C++ program to read a number, and print the day of the
week:
5- Write a c++ program to read the month number then print the name of
the month(switch-case)
6- Write a c++ program to input the average for one student ,then print
grade of student(switch-case)
13