Lokang 

C++ and MySQL

if

In C++, conditional statements allow you to control the flow of your program based on certain conditions. The most common conditional statement is the if statement, which allows your program to execute a block of code only if a specified condition is true. This is crucial for making decisions in your programs.

The if Statement

The basic structure of an if statement in C++ is as follows:

if (condition) {
   // Code to execute if the condition is true
}
  • condition: This is a boolean expression that is evaluated to either true or false.
  • If the condition is true, the code inside the {} brackets is executed.
  • If the condition is false, the code inside the {} brackets is skipped.

Example:

#include <iostream>
using namespace std;
int main() {
   int number = 10;
   if (number > 5) {
       cout << "The number is greater than 5." << endl;
   }
   return 0;
}

Explanation: In this example, since number is 10 and the condition number > 5 is true, the message "The number is greater than 5." will be printed.

 

 

The else Statement

You can extend the if statement by using an else statement to define an alternative block of code to execute if the condition is false.

if (condition) {
   // Code to execute if the condition is true
} else {
   // Code to execute if the condition is false
}

Example:

#include <iostream>
using namespace std;
int main() {
   int number = 3;
   if (number > 5) {
       cout << "The number is greater than 5." << endl;
   } else {
       cout << "The number is 5 or less." << endl;
   }
   return 0;
}

Explanation: In this case, since number is 3 and the condition number > 5 is false, the message "The number is 5 or less." will be printed.

 

 

The else if Statement

The else if statement allows you to check multiple conditions. It is used between an if and an else statement to specify a new condition to test if the previous conditions were false.

if (condition1) {
   // Code to execute if condition1 is true
} else if (condition2) {
   // Code to execute if condition2 is true
} else {
   // Code to execute if none of the above conditions are true
}

Example:

#include <iostream>
using namespace std;
int main() {
   int number = 5;
   if (number > 5) {
       cout << "The number is greater than 5." << endl;
   } else if (number == 5) {
       cout << "The number is exactly 5." << endl;
   } else {
       cout << "The number is less than 5." << endl;
   }
   return 0;
}

Explanation: Here, since number is 5, the condition number == 5 is true, so the message "The number is exactly 5." will be printed.

 

 

Nesting if Statements

You can also nest if statements inside other if statements to create more complex decision-making structures.

Example:

#include <iostream>
using namespace std;
int main() {
   int number = 15;
   if (number > 0) {
       if (number % 2 == 0) {
           cout << "The number is positive and even." << endl;
       } else {
           cout << "The number is positive and odd." << endl;
       }
   } else {
       cout << "The number is negative." << endl;
   }
   return 0;
}

Explanation: In this example, the program first checks if the number is positive. If it is, it then checks whether the number is even or odd, using the modulo operator (%).

Summary

  • The if statement allows you to execute code based on whether a condition is true.
  • The else statement provides an alternative code block if the condition is false.
  • The else if statement lets you check multiple conditions in sequence.
  • Nesting if statements can create more complex logical structures.

Understanding how to control the flow of your program using these conditional statements is essential for creating dynamic and responsive applications.

Practice Problems

  1. Write a program that checks whether a number entered by the user is positive, negative, or zero.
  2. Modify the above program to also check if the number is even or odd.
  3. Create a program that determines if a year entered by the user is a leap year.

This content provides a comprehensive introduction to using if, else, and else if statements in C++, including examples and practice problems to reinforce the concepts.