GAME OF CODE’22 — DAY 1

Get Started with C++

Hello learners!

Let’s get started with C++. I hope you are excited to immerse in this journey full of C++ and fun activities?

Let us begin with this journey of learning. If this is the first time you are encountering a programming language then it might look confusing to you but trust me, at the end of this program you will be clear with all the basic stuff.

In this blog, you will know about the history of C++, Some basic syntax with examples and programs. After reading this post you would be familiar with comments, variables, data types, tokens, operators.

Who Developed C++?

C++ was developed by Bjarne Stroustrup in 1979 at AT&T Bell Labs.

Welcome to Game of Code 2022

A simple program to print “ — — — — — — -Welcome to Game of Code’ 2022; happy coding 😊 — — — — — — ”

Code:

Aghhhh looks crazy! But this is how a code really looks !!

· Let’s start with line 1. #include <iostream> where we include a header file! Header file? Okay so if we start writing a code there has to be a file that knows all the meanings of the words you are going to write in a program. The header file imports all the features of the program. So, to include any kind of file we use #include and whichever file is o be included is written inside the angular brackets, like <iostream> where iostream is a header file that defines the standard objects/keywords in our program. It stands for ‘Input-Output Stream’

· Noice! Let’s switch to line 2. using namespace std this line defines that we are using a few objects/words in the code, from the standard library that is abbreviated as std.

namespace: it is a word used to organize codes into logical units or groups.

Example :

we can see we declare a region in our code i.e. GOC and GOC2 using namespace.

— — -Now if we write using namespace GOC, any word DAY1 will be found inside the region of GOC and that particular definition of the word will be used. — — — And if we write using namespace GOC2, any word DAY1 will be found inside the region of GOC2 and that particular definition of the word will be used.

Right now, the definition of line 2. might look hazy to you but in progressive days this will be clear to you. Don’t go nuts before this line. We will discuss this line in brief after we study the oops and ambiguity concept.

· Wow! Great going… let’s move to line 3.

1. this is the MAIN function of our program.

2. For simple clarity a function is a block of code that can be executed whenever it is called.

3. How is a function called? okay will tell you but we will study this in detail during later blogs.

You can call a function by writing the name of the function in between your code and the code written inside the curly braces of that function will get executed. A depth discussion will be done later.😊

Similarly main is also a function that can also be called like other functions by writing main().

4. What is special about main and why do we have to write in our code? int main() is a special function which is called first. Anything written inside the main function will be executed. That is where you will have to write all your code.

In this line, you will declare the function main. Write all the things that your main function will perform or mainly what your program will do. The execution of everything else all other functions, classes other things will be done through main regardless of wherever else they are declared or called.

5. {}: curly braces, are generally used to divide codes in a block.

In this code, in line 3 and line 6 the curly braces are written. Between that, the code of the function main, or things to be done by the main function is written. This code will also be called a function’s body. We will discuss curly braces in detail later in this blog.

MORE ON CURLY BRACES ( {..} )

First, let’s discuss {…}

// code written inside curly braces is referred to as blocks of codes. If we declare anything inside a block, will not be accessible outside that block

Example :

Line 1 will produce an error since b is declared in a block and we cannot access it.

These curly braces are used to define the codes of loops, functions & conditional statements (will study in later articles)

6. Jump to line 4. A line you will use this line regularly in programs.

1. Anything written inside “..” is a String. The string is a datatype which we will discuss in detail during later sections.

2. cout : Character output, is used to display values. It prints the output to the output device.

EX: in this specific program the output generated is

Things written inside “..” get printed as output.

3. <<: Insertion operator, the data needed to be displayed on the screen is inserted in the standard output stream (cout) using it.

4. It is a part of namespace std. If we do not include, using namespace std, we will have to use

where ‘std’ is the namespace and ‘::’ is the scope resolution operator. We will discuss this later in this blog.

5. endl : this keyword is used to print the next line. The content after endl will be printed in another line.

We hope you are able to make out a difference! 😊

READ ABOUT ESCAPE SEQUENCES:

7. Jump to line 5.

We will discuss the return statement in detail when we discuss everything about functions.

Till then a simple one-line explanation: Since the return type of main function is int (int main(): int in this line denotes return type) hence we have to return something. We hence return 0 at the end.

This line has to be included at the end of the main function, always.

Ques. Why is line 2 i.e., using namespace std written after line 1. i.e., #include<iostream>?

Ans. You can use ‘using namespace std’ in any line where you want to use it. Above line 1, inside the main function, at any line in the main function. But we generally prefer writing it in the header section i.e., after including statements. This is done so that we can use the std namespace in our entire program easily.

If we write it inside the main function, we will have to specify std using scope resolution operator (::) using std:: cout or std::endl in the program outside the main function. If we write it in line and our line 1 will show error without std::.

Example :

Line a in this produce error. As this program will be ambiguous about using std since it does not know we are using namespace std. Because a program is executed in the order in which it is written.

Line a in this program won’t show an error since we use std::.

Hence it is best to put ‘using namespace std’ in the header section.

NOTE :

1. The program is executed and runs in the order in which it is written and the main function is the first function to run.

2. Each line in a C++ program is terminated with the help of a semi-colon ( ; )

Comment in C++

Comments in C++ are the part of code that is not executable. Like I mentioned word lines in my code, but we cannot write anything in the code, as that will produce an error, as our compiler does not understand word lines. Hence we use comments. Comments are used by programmers to explain their code side by side as they write so that anyone can understand the reason why wrote a line.

Single-Line Comments:

We use this // to comment out the single line of code.

Eg: // This is the single-line comment.

Multi-line comments:

When we have to comment more than one line we use /* comment */.

E.g. /* print (“C++ is very hard to understand”);

Here is the example of multiline comment */

Tokens in C++

C++ program contains several items and those are identified as tokens by a compiler.

Variable

It is a container for storing values in the RAM. Syntax to declare a variable in the program:

Example:

int: data type (discussed later)

year: variable name

=: assignment operator, we assign value

2022: value

Restriction for writing a variable

  • Variable name must start with a letter or an underscore.
  • Variable names must not contain white spaces.
  • Variable names must not contain any special symbols like $, @, # etc.
  • Variable names must not be the same as any keyword like if, while, struct

Initializing a variable

The syntax for initializing a variable

E.g. int a = 10; here a is a variable name whose value is 10.

Data Types

VARIOUS DATA TYPES IN DETAIL :

Program:

C++ program demonstrating the use of variables and int data type.

Output :

Taking Inputs

To take inputs from users we use cin keyword. It is also a part of namespace std.

The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard.

Sample I/O Code:

Output :

Operators

Operators are divided on the basis of their categories:

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators

Arithmetic Operators

Relational Operators

Logical Operators

Bitwise Operators

Bitwise operators first convert the numbers in bitwise format and then they are operated upon.

YAYYYY ! Now you know basic structure of a C++ program !

Blog Credits: muskankumari, Isha Verma

Post Credit : Vandana chauhan

--

--