Statements in C++

Topic 7 of 13

C++ Statements

Welcome to your C++ learning journey! This comprehensive guide provides detailed notes, designed to help you master the fundamental mechanics of writing C++ code. Whether you’re an absolute beginner or looking to solidify your basic knowledge, understanding how instructions are processed and displayed is step one in learning C++ step-by-step.

Defining the Core: What is a Statement?

In the simplest terms, programming is all about giving instructions to a computer.

Definition:

A program is defined as a set of instructions given to a computer to perform a specific task [1]. In the language of C++, these instructions are called Statements.

  • A Statement is considered as the building block of any program.
  • It is essentially a single line of instruction or code.
  • Every single statement is designed to perform a specific task (like displaying something using cout statement), upon execution.
  • A C++ program can contain a single statement or multiple statements working together.

Most Commonly Used Types of Statements in C++:

Here are the most commonly used types of statements in C++:

Expression Statements:

Expression statements are the most commonly used statements in C++. It contains expressions, and with statement terminator (;). Some expression statements are output statements (e.g. cout<<’’ 7Scribes!’’; ), assignment statements (x= 10; ), and function call (add(10+6); )

Declearation Statements:

Declearation Statements are the type of statements in C++, used to declare (introduce) variables (e.g. string name;), functions (int add( int a, int b);), or constants (e.g. const size=10;). These statements also ends with statement terminator (;).

Conditional Statements:

These types of the statements are used in the C++, while making decision based on some specific condition(s). The common conditional statements are if (e.g if(x==10){//block of statements}), if else (e.g. else if( x<=10)), switch (e.g. switch(a){//block if statements with multiple condition })

Note: We will learn these ( if, if else, else, switch) later in detail.

Iteration Statements:

In C++, Iteration statements are used to execute a part of code again and again, until a certain condition met. for loop, while loop, do while loop, and for each loop, are the iterator statements used in C++:

Note: We will learn these topics in detail in upcoming lectures.

Jump Statements:

Jump statements are used to transfer the control to the another part of the code( e.g. break , continue, return, and goto statemets are used as Jump statements in C++).

Compound Statemets:

Basically, compound statements are the combination of the rest types of the statements like expression, declaration, conditional, iterator statements enclosed in curly braces {}. These are used in the body of the functions, loops, and conditions e.t.c.

 

#include <iostream>

using namespace std; // Using-Directive (acts like a declaration statement)

int main() {
        int score; //  Declaration Statement 
        bool isActive = true;

        score = 90; // Expression Statement (Assignment)
        cout << "Your score is set to: " << score << endl; // Expression Statement (Output)

        if (score >= 60) { // Selection Statement (if-else) 
             // Compound Statement (Block of code) starting from here
             cout << "You passed the test!" << endl;
             isActive = false;
             // Compound Statement (Block of code) ending here
        }

        else {
             cout << "You failed the test." << endl;
        }

       int countdown = 3;
       while (countdown > 0) { // Iteration Statement (while loop)
            cout << countdown << "..." << endl;
            countdown = countdown - 1;
       }
       return 0; // Jump Statement (return)
}

Statement Terminator (;)

How does the computer know where one instruction ends and the next begins?

  • Every statement in C++ must be ended or terminated with a semicolon (;).
  • The semicolon acts as the Statement Terminator. When the compiler sees the semicolon, it knows that the line of code (the instruction) is complete.
    • # include <iostream>
      using namespace std; //ending with statement terminator (;)
      int main(){
          cout<<"Happy Coding with 7Scribes!!!"; //ending with statement terminator
          return 0; //ending with statement terminator
      }