0% found this document useful (0 votes)
33 views9 pages

C++ Programming: Academic Year 2018-2019 Lecturer Zanco A. Taha (MSC.)

Here are two programs using if and switch statements to implement the given tasks: IF program: #include <iostream> #include <string> using namespace std; int main() { string code; cout << "Enter mobile code: "; cin >> code; if(code == "0750") { cout << "Korek" << endl; } else if(code == "0770") { cout << "Asia" << endl; } else if(code == "0780") { cout << "Zain" << endl; } else { cout << "Unknown" << endl; } return

Uploaded by

Rahel Sabr
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)
33 views9 pages

C++ Programming: Academic Year 2018-2019 Lecturer Zanco A. Taha (MSC.)

Here are two programs using if and switch statements to implement the given tasks: IF program: #include <iostream> #include <string> using namespace std; int main() { string code; cout << "Enter mobile code: "; cin >> code; if(code == "0750") { cout << "Korek" << endl; } else if(code == "0770") { cout << "Asia" << endl; } else if(code == "0780") { cout << "Zain" << endl; } else { cout << "Unknown" << endl; } return

Uploaded by

Rahel Sabr
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/ 9

Ministry of Higher Education Computer Department

Noble Institute 1st Stage

C++ Programming
Academic Year
2018-2019

Lecturer
Zanco A. Taha (MSc.)
Outlines

• Conditional structures: if and switch

2
Conditional structure: if and else
The if keyword is used to execute a statement or block only if a condition is
fulfilled. Its form is:
if (condition) statement
For example, the following code fragment prints x is 100 only if the value stored
in the x variable is indeed 100:
if (x = = 100)
cout << "x is 100";
If we want more than a single statement to be executed in case that the condition is
true we can specify a block using braces { }:
if (x == 100)
{
cout << "x is ";
cout << x;
}
We can additionally specify what we want to happen if the condition is not
fulfilled by using the keyword else. Its
form used in conjunction with if is:
if (condition) statement1 else statement2
Conditional structure: if and else
We can additionally specify what we want to happen if the condition is not
fulfilled by using the keyword else. Its form used in conjunction with if is:
if (condition) statement1 else statement2
Ex:
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";

The if + else structures can be concatenated with the intention of verifying a range
of values. The following example shows its use telling if the value currently stored
in x is positive, negative or none of them (i.e. zero):
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
If Example
#include <iostream>
using namespace std;
int main () {
int a, b, c;
int max;
Cout<<“Enter Three numbers to find max:\n”;
Cin>>a>>b>>c;
If (a > b){
if (a > c)
max=a;
else
max=c}
Else{
if (b>c)
max=b;
else
max=c}
cout<<“\nMax number is”<<max;
return 0; }
The selective structure: switch
It compares an expressions with a set of cases for equality.
switch (expression)
{
case constant1:
group of statements 1;
break;
case constant2:
group of statements 2;
break;
.
.
.
default:
default group of statements
}
The selective structure: switch
Switch Example
#include <iostream>
using namespace std;
int main () {
int x;
Cin>>x;
Switch (x){
case 1:
cout<<“X is One”;
break;
case 2:
cout<<“X is One”;
break;
case 3:
cout<<“X is One”;
break;
default:
cout<<“X is One”;
}
return 0; }
Homework

Write two programs using IF and Switch statements to


implement the followings:

Input Output
0750 Korek
0770 Asia
0780 Zain
Others Unknown

Notes:Use String data type for reading codes.

You might also like