Skip to main content

Control Flow

Booleans in C++

Control Flow Introduction

Control flow in C++ is like giving instructions to a computer to tell it what to do in a certain order. You can make choices (if something is true, do this; otherwise, do that) and repeat actions. This helps you create the flow of your program and make it do what you want.

Introduction to Booleans

Booleans represent either true or false. These are important to decision-making in programming. Let's see,

bool var = true;
bool var2 = false;

cout << var << endl;    
cout << var2 << endl;   
1
0

In C++, the bool keyword is used to represent a boolean. Notice that when we tried to print var, it printed 1, not true.

Why?

Because in programming, true is represented by 1 and false by 0.

Takeaways:

  1. We can't declare a variable's name as true or false, as they both are keywords.
  2. "true" is a string, whereas true is a boolean value, represented by 1.
  3. True doesn't mean true, nor does False mean false.

Quiz


Comparison Operators

We can use the above operators while assigning values to a boolean variable. Let's see how.

bool var1 = (2 >= 3);     // var1 becomes equal to false
bool var2 = (3 >= 2.3);   // var2 becomes equal to true

cout << "value of var1 is " << var1 << endl;
cout << "value of var2 is " << var2 << endl;
value of var1 is 0
value of var2 is 1

Let's break down what is happening in the above code:
bool var1 = (2>=3);

First of all, the expression (2>=3) gets evaluated, and then the result of this comparison is assigned to var1. So what do you think about (2>=3), is it true or false?

Correct, it's false. 2 is not greater than or equal to 3. So, now you know how var1 got assigned with false.

You can play around with these operators.

Task

Write a program, that takes two numbers as inputs, use the above operators and print the results.


Quiz

What is the output of this code?

#include <iostream>
using namespace std;

int main() {

    int number = 7;

    bool result1 = (number != 7);
    bool result2 = (number >= 8);
    bool result3 = (number <= 8);
    
    cout << result1 << " " << result2 << " " << result3 << endl;
  
    return 0;
}

What is the output of this code?

#include <iostream>
using namespace std;

int main() {

    int number = 15;

    bool result1 = (number == 8*2-1);
    bool result2 = (number != 16-4);
    bool result3 = (number < 8);
    
    cout << result1 << " " << result2 << " " << result3 << endl;
  
    return 0;
}

Problem Solving

Finding if the given number is odd or not

The task is to input a number from the user and print 1 if it's odd; otherwise, print 0.

C++ Code
# include <iostream>
using namespace std;

int main(){
    int n;
    
    cout << "Input the number : ";
    cin >> n; 

    bool result = (n%2 == 1); // if result is 1, then it is odd, else not

    cout << result << endl;

    return 0;
}

Chocolate Distribution Possible ?

If we are given m chocolates and there are n people, then can we divide these m chocolates into n people, such that each of them will get an equal number of chocolates and each person should get an integer number of chocolates?

Write a program for the above problem statement.

C++ Code
# include <iostream>
using namespace std;

int main(){
    int m, n;

    cout << "The number of chocolates and number of people are : ";
    cin >> m >> n;

    // Basically we just have to find out if m is divisible by n or not, if it is, 
    // then yes we can divide, else we can't.

    bool result = (m%n == 0);

    cout << result << endl;
    
    return 0;
}

Logical Operators

&& (AND) Operator

#include <iostream>
using namespace std;

int main() {
    int age;
    bool isStudent;
    
    cout << "Enter your age: ";
    cin >> age;

    cout << "Are you a student? (1 for Yes, 0 for No): ";
    cin >> isStudent;

    if (age >= 18 && isStudent) {
        cout << "You are an adult student." << endl;
    }
    else if (age >= 18 && !isStudent) {
        cout << "You are an adult non-student." << endl;
    }
    else if (age < 18 && isStudent) {
        cout << "You are a minor student." << endl;
    }
    else {
        cout << "You are a minor non-student." << endl;
    }

    return 0;
}
Explanation:

This code asks your age and student status and categorizes you as follows:

  • If you're 18 or older and a student, it displays "You're an adult student."
  • If you're 18 or older but not a student, it displays "You're an adult non-student."
  • If you're under 18 and a student, it displays "You're a minor student."
  • If you're under 18 and not a student, it displays "You're a minor non-student."

The code uses the && operator to evaluate both conditions, and the appropriate message is shown based on your input.

|| (OR) Operator

#include <iostream>
using namespace std;

int main() {
    int age;
    bool isStudent;

    cout << "Enter your age: ";
    cin >> age;

    cout << "Are you a student? (1 for Yes, 0 for No): ";
    cin >> isStudent;

    if (age < 18 || isStudent) {
        cout << "You are either a minor or a student or both." << endl;
    } else {
        cout << "You're neither a minor nor a student." << endl;
    }

    return 0;
}

! (NOT) Operator

#include <iostream>
using namespace std;

int main() {
    bool isSunny;

    cout << "Is it sunny today? (1 for Yes, 0 for No): ";
    cin >> isSunny;

    if (!isSunny) {
        cout << "You might need an umbrella today." << endl;
    } else {
        cout << "Enjoy the sunny day!" << endl;
    }

    return 0;
}

Quiz

What is the output of the following code?

#include <iostream>
using namespace std;

int main() {
    int num = 7;
    bool condition1 = (num > 5) && (num < 10);
    bool condition2 = (num > 8) || (num < 2);

    cout << "Result: " << (condition1 || condition2) << endl;
    return 0;
}

Task

Find the maximum number amongst n1, n2, and n3. You have to first take these numbers as inputs from the user.

C++ Code
#include <iostream>
using namespace std;

int main() {
    int n1, n2, n3;

    cout << "Enter the first number: ";
    cin >> n1;

    cout << "Enter the second number: ";
    cin >> n2;

    cout << "Enter the third number: ";
    cin >> n3;

    int maxNum = n1;  // first assigning maxNum with n1

    // check if n2 > maxNum and greater than equal to n3, if yes then its the maximum value
    if (n2 > maxNum && n2 >= n3) {
        maxNum = n2;
    }

    // check if n3 > maxNum and greater than equal to n2, if yes then its the maximum value
    if (n3 > maxNum && n3 >= n2) {
        maxNum = n3;
    }

    cout << "The maximum number among " << n1 << ", " << n2 << ", and " << n3 << " is: " << maxNum << endl;

    return 0;
}