Professional Documents
Culture Documents
06-Sixth Lecture
06-Sixth Lecture
Mosul University
Mechatronics Engineering
Programming
1st Class
Instructor : Osamah Abdulwahid Taha
Lecture (6)
• if (condition)
{
statement set 1;
}
else
{
statement set 2;
}
1
1/7/2018
if(x%2==0)
cout<<"The number is even";
else
cout<<"the number is odd";
}
• What if we add :
cout<<“Hi”; after cout<<“even”<<endl;
What will happen??
Will the compiler show an Error?
Try it yourself.
2
1/7/2018
Example :
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter number value : \n";
cin>>num;
3
1/7/2018
if(num == 1)
cout<<“one”;
else if(num == 2)
cout<<“two”;
else if(num == 3)
cout<<“three”;
else
cout<<“not”;
return 0;
}
Logical Operators
• And in C++ we write it &&
1 1 1
1 0 0
0 1 0
0 0 0
• Or in C++ we write it ||
1 1 1
1 0 1
0 1 1
0 0 0
1 0
4
1/7/2018
Example :
int main() {
cout<<(true && true)<<endl;
cout<<(true && false)<<endl;
cout<<(true || false)<<endl;
cout<<(false || false)<<endl;
cout<<(!true)<<endl;
cout<<(!false)<<endl;
cout<<(!(true && false))<<endl;
return 0;
}
Output :
1
0
1
0
0
1
1
5
1/7/2018
6
1/7/2018
Nested if Statements
• To test more than one condition, an if statement can be nested inside another if statement.
if (condition) {
……
……
If(condition) {
…..
…..
}
…..
……
}
7
1/7/2018
8
1/7/2018
if
if
if
else
else
else