+2 Computer Science
+2 Computer Science
+2 Computer Science
To code
use https://www.onlinegdb.com/
return 0 ;
}
Variables in C++
Syntax to Declare a variable
Declaring a single variable data_type var_name;
eg:= int var_name;
char name;
oat weight;
Declaring multiple variables of same datatype : data_type var1_name, var2_name, var3_name;
eg:= int age, phone_number, pin_code ;
1. Integer
It is used to store integers.
Example
int var = 123;
2. Character
It is used to store characters.
Example
char var = ‘a’;
3. Floating Point
It is used for storing single-precision oating-point numbers.
Example
oat num = 1.23;
4. Boolean
It is used to store logical values that can be either true or false.
Example
bool b = false;
5. String
A string is a collection of characters surrounded by double quotes. The string data type is
used to store words or sentences.
Example
string str = “Hello world”;
Input and Output in C++
1. Output on the console: We can print output on the console using cout from the iostream library.
For example,
cout << “Hello World”;
1. Input from user: We can take input from the user using cin from the iostream library. For example,
int var;
cin >> var;
New Lines
We can use \n character to insert a new line.
For example,
cout<< “Hello World! \n“;
cout << “Hello World!”;
output:
Hello World
Hello World
if we not use \n
cout<< “Hello World! “;
cout << “Hello World!”;
output :
Hello World!Hello World!
Default: Default keyword is optional in switch statements and the code inside the default block is executed
when none of the cases matches the value of the expression.
Syntax
switch (expression)
{
case value1:
// Code to be executed if expression matches value1
break;
case value2:
// Code to be executed if expression matches value2
break;
default:
// Code to be executed if expression does not match any case
break;
}
Loop in C++
Loops are used to repeatedly execute a block of code multiple times.
Types of Loops
1. For Loop
For loop helps us to execute a block of code a xed number of times.
Syntax
for (initialization; test ; update) {
// body of the loop
// statements we want to execute
}
2. While Loop
While loop repeatedly executes a block of code till the given condition is true.
Syntax
while (condition) {
// statements update_condition;
}
3. Do-While Loop
Do-while loop also executes the block of code till the condition is true but the di erence
between a while and a do-while loop is that the do-while executes the code once without
checking the condition and the test condition is tested at the end of the loop body.
Syntax
do {
// Code to be repeated
} while (condition);
Arrays in C++
An array is a data structure that allows us to store a xed number of elements of the same data
type in contiguous memory locations.
Syntax to Declare an Array
#include <iostream>
#include <string>
using namespace std;
int main() {
// Declare and initialize an array of strings
string fruits[] = {"Apple", "Banana", "Orange", "Grapes"};
// Access and print each element of the array
for (int i = 0; i < 4; i++) {
cout << "Fruit at index " << i << ": " << fruits[i];
}
return 0;
}
Output
Fruit at index 0: Apple Fruit at index 1: Banana Fruit at index 2: Orange Fruit at index 3: Grapes
1. Write a program to nd the sum of two numbers.