0% found this document useful (0 votes)
45 views

Computer Programming Lecture4

The document discusses different selection statements in C++ including if, if else, nested if, and switch statements. Examples are provided for each type of statement. If statements execute code if a condition is true, if else adds optional else blocks. Nested if uses multiple if statements. Switch compares a value to multiple case labels and executes the matching code block. The document provides examples of using each statement to check conditions, read input, and make decisions in programs.

Uploaded by

『68』 TeaM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Computer Programming Lecture4

The document discusses different selection statements in C++ including if, if else, nested if, and switch statements. Examples are provided for each type of statement. If statements execute code if a condition is true, if else adds optional else blocks. Nested if uses multiple if statements. Switch compares a value to multiple case labels and executes the matching code block. The document provides examples of using each statement to check conditions, read input, and make decisions in programs.

Uploaded by

『68』 TeaM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Assistant lecturer

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.

Assistant lecturer Safa Hussain

1
Example:Write a C++ program to read any two numbers and print the
largest value of it:

The Single Block If Statement Structure :

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:

Assistant lecturer Safa Hussain

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

Assistant lecturer Safa Hussain

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;
}

Example:Write a C++ program to read a student degree, and check if it’s


degree greater than or equal to 50, then print pass, otherwise print fail:

#include<iostream>
using namespace std;
int main( )
{
int degree;
cin >> degree;
if (degree >= 50 )
cout << "pass";
else
cout << "fail";
return 0;
}

Assistant lecturer Safa Hussain

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 ;

Assistant lecturer Safa Hussain

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;
}

Example:Write C++ program to compute the value ofz according to the


following equations:

Assistant lecturer Safa Hussain

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;
}

The Switch Selection Statement


switch ( selector )
{
case label1 :
statement1 ;
break;
case label2 :
statement2 ;
break;
case label3 :
statement3 ;
break;
:
case label-n :
statement-n ;
break;
default :
statement-e ;
break;
}

Assistant lecturer Safa Hussain

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;
}

Assistant lecturer Safa Hussain

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;
}

Assistant lecturer Safa Hussain

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;
}

Assistant lecturer Safa Hussain

12
Homework:
1- Write a c++ program to find a large number between three integer
number?

2- Write a c++ program that performs the arithmetic operation (+,-,*,/)


Determined by a user input.

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)

Assistant lecturer Safa Hussain

13

You might also like