Section (3) If - For
Section (3) If - For
Lab Manual
If statement in C++
The single if statement is used to execute the code if a condition is true. It is also called one-way selection
statement.
Syntax
if(condition)
{
Statement(s);
}
Example of if statement
#include <iostream>
using namespace std;
int main()
{
int a,b ;
a=50;
b=50;
if ( a == b)
cout << " a is equal to b"<<end1;
return 0;
}
To execute only one statement in if or else (as in previous example), you can skip
curly brackets { }. If you want to execute multiple statements in if, then enclose the
statements within curly brackets { }
C++ Programming
Lab Manual
Syntax
if(condition) {
Statement(s1);
}
else {
Statement(s2);
}
Example of if-else statement
#include <iostream>
using namespace std;
int main(){
int num=66;
if( num < 50 ){
//This would run if above condition is true
cout<<"num is less than 50";
}
else {
//This would run if above condition is false
cout<<"num is greater than or equal 50";
}
return 0;
if(condition_1) {
Statement1(s);
if(condition_2) {
Statement2(s);
}
}
Node: Statement1 would execute if the condition_1 is true. Statement2 would only execute if
both the conditions( condition_1 and condition_2) are true.
C++ Programming
Lab Manual
if(condition_1) {
/*if condition_1 is true execute this*/
statement(s1);
}
else if(condition_2) {
/* execute this if condition_1 is not met and
* condition_2 is met
*/
statement(s);
}
else if(condition_3) {
/* execute this if condition_1 & condition_2 are
* not met and condition_3 is met
*/
statement(s);
}
.
.
.
else {
/* if none of the condition is true
* then these statements gets executed
*/
statement(s);
}
C++ Programming
Lab Manual
C++ switch...case
switch (n)
case constant1:
break;
case constant2:
break;
default:
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter an integer: ";
cin >> n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
C++ Programming
Lab Manual
D= b2-4ac
#include <iostream>
#include <cmath>
using namespace std;
int main() {
if (D > 0) {
x1 = (-b + sqrt(D)) / (2 * a); //sqrt() library function is used to find the square root of a number
x2 = (-b - sqrt(D)) / (2 * a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (D == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b ) / (2 * a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
return 0;
}
C++ Programming
Lab Manual
int main()
{
float n1, n2, n3;
return 0;
Example: C++ Program to read the numbers from 1 to 7 and display their
correspondence day of week using if statement.
#include <iostream>
using namespace std;
int main()
{
int Day_Number;
cin >> Day_Number;
if (Day_Number == 1) {
cout << " saturday ";
}
else if(Day_Number == 2){
cout << " sunday ";
}
else if (Day_Number == 3) {
cout << " monday ";
C++ Programming
Lab Manual
}
else if (Day_Number == 4) {
cout << " Tuerday ";
}
else if (Day_Number == 5) {
cout << " wednesday ";
}
else if (Day_Number == 6) {
cout << " thursday ";
}
else if (Day_Number == 7) {
cout << " friday ";
}
else{
cout << "error";
}
return 0;
}
Example: C++ Program to read the numbers from 1 to 7 and display their
correspondence day of week using case statement.
#include <iostream>
using namespace std;
int main()
{
int Day_Number;
cout << "inter the number of day";
cin>> Day_Number;
switch (Day_Number) {
case 1:cout << " saturday ";
break;
case 2: cout << " sunday ";
break;
case 3: cout << " monday ";
break;
case 4: cout << " Tuerday ";
break;
case 5: cout << " wednesday ";
break;
case 6: cout << " thursday ";
break;
case 7: cout << " friday ";
break;
default: cout << "error";
}
}
C++ Programming
Lab Manual
Task
Example:C++ Program to read student's mark as integer then print the
equivalent grade depends on the following table:
0≤Mark<60 60≤Mark<65 65≤Mark<75 75≤Mark<85 85≤Mark<100
Fail OK Good Very Good Excellent
C++ loops
Loops are used in programming to repeat a specific block until some end condition is met.
There are three type of loops in C++ programming:
1. for loop
2. while loop
3. do...while loop
C++ for Loop Syntax
// code
3. If the test expression is false, for loop is terminated. But if the test expression is true, codes
inside body of for loop is executed and update expression is updated.
4. Again, the test expression is evaluated and this process repeats until the test expression is
false.
#include <iostream>
using namespace std;
int main()
{
int i, n, factorial = 1;
while (testExpression)
// codes
}
C++ Programming
Lab Manual
If the test expression is true, codes inside the body of while loop is evaluated.
Then, the test expression is evaluated again. This process goes on until the test expression is false.
#include <iostream>
using namespace std;
int main()
{
int number, i = 1, factorial = 1;
The do...while loop is a variant of the while loop with one important difference. The body of do...while loop
is executed once before the test expression is checked.
do {
// codes;
while (testExpression);
The codes inside the body of loop is executed at least once. Then, only the test expression
is checked.
If the test expression is true, the body of loop is executed. This process continues until the
test expression becomes false.
#include <iostream>
using namespace std;
int main()
{
float number, sum = 0.0;
do {
cout<<"Enter a number: ";
cin>>number;
sum += number;
}
while(number != 0.0);
int main()
{
int n, sum = 0;
Task