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

6 Lecture

The document discusses C++ if, else if, and else statements. It provides examples of using if statements to check if a number is positive or negative, and using else if statements to check for multiple conditions. It also shows an example of drawing different shapes like a square and X letter using if/else conditions in nested for loops to print asterisks in certain positions and spaces in others. The document suggests writing C++ code to draw a triangle shape and print the letter A as additional examples.

Uploaded by

brwa rahim
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)
44 views

6 Lecture

The document discusses C++ if, else if, and else statements. It provides examples of using if statements to check if a number is positive or negative, and using else if statements to check for multiple conditions. It also shows an example of drawing different shapes like a square and X letter using if/else conditions in nested for loops to print asterisks in certain positions and spaces in others. The document suggests writing C++ code to draw a triangle shape and print the letter A as additional examples.

Uploaded by

brwa rahim
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/ 7

Object Oriented Programming

Third Semester (2nd Stage)


C++ PROGRAMMING LANGUAGE
Lecture: Areen J.fadhil
If…else if…else

C++ if Statement

if (testExpression)

// statements

How if Statement work?


Example If …else statement:
// Program to print positive number entered by the user

#include <iostream>
using namespace std;
void main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
else{
cout << "This statement is always executed.";
return 0;
}
}

Example 2:
// Program to check whether an integer is positive or negative
#include <iostream>
using namespace std;
void main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout<< "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0;
}

Example : if … else if…else inter by user

#include <iostream>
Using namespace std;
Void main(){
int number;
Cout <<”enter the number=”;
Cin>> number;
If (number >0){
Cout<<” you entered the positive number”<<number<<endl;
}
else if (number <0){
cout<<: you entered the negative number”<<number<<endl;
}
else{
Cout<<”you entered the zero”;
}
}
Same Example for drawing shapes:
// C++ program for Square Shape using 10 stars:

#include<iostream>
using namespace std;
void main()
{
for (int i = 1; i <= 10; i++)
{
for (int j = 1; j <= 10; j++)
{
if (i == 1 || i == 10) {

}
cout << "*";
else if (j == 1 || j == 10) // (||) means OR
//(&&) means AND

cout << "*";


else
cout << " ";
}
cout << endl;

}
}
// C++ program for X letter using 5 stars:
#include<iostream>
using namespace std;
void main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
if (i == j || j == 6-i)
cout << "*";

else
cout << " ";
}
cout << endl;

}
}

//write C++ code for Draw the Tringle shape.


//write C++ code for print A Latter.

You might also like