+2 Computer Science

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

What to " Study" ?

1. commands Variation of di erent languages


2. using of command on proper way
3. Finding Error
4. Solving Error

To code
use https://www.onlinegdb.com/

Start with Basics


1. Variables
2. Data type
3. Arithmetic operators (+ , - , * , /)
4. assignment operator ( = , += , -+ )
5. relational Operator( == , <> , <= , =>)
6. Logical operators(&&, !!, !)
7. if
8. if else
9. if else ladder
10. switch
11. For loop

how many "languages" to study ?


◦ C++
◦ HTML (not a language)
◦ CSS (not a language)
◦ JavaScript
◦ SQL
◦ Php

Start with C++


◦ Print
◦ Variable
◦ Datatype
◦ Input
Language : C++
Must code :
#include <iostream>
using namespace std;
int main() {

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 ;

Data Types in C++


Data types are the type of data that a variable can store in a program.

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!

Conditional Statements in C++


Conditional statements allow us to control the ow of the program based on certain conditions.
It helps us to run a speci c section of code based on a condition.
1. The if statement
If statement executes a block of code if and only if the given condition is true.
Syntax
if (condition)
{
// Code to be executed if the condition is true
}
2. The if-else statement
If the condition inside the if statement is true, then the code inside the if block will get executed,
otherwise code inside the else block will get executed.
Syntax
if (condition)
{

// Code to be executed if the condition is true


}
else
{
// Code to be executed if the condition is false
}

3. The else-if statement


The else if statement allows you to check for multiple conditions sequentially.
Syntax
if (condition1)
{
// Code to be executed if condition1 is true
}
else if (condition2)
{
// Code to be executed if condition1 is false and condition2 is true
}
else
{
// Code to be executed if all conditions are false
}
4. Switch statement
The switch statement evaluates the expression and compares the value of the expression with the cases. If the
expression matches the value of any of the cases, the code associated with that case will be executed.
Break and default keywords are generally used with switch and cases.

Break and Default


Break: The break keyword is used to exit the switch statement when one of the cases matches.

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.

2. Write a program to check if a number is even or odd.

3. Write a program to calculate the factorial of a number.

4. Write a program to swap two numbers using a temporary variable.

5. Write a program to nd the largest of three numbers.

6. Write a program to display the multiplication table of a number.

7. Write a program palindrome .

8. Write a program to nd whether a number is prime or not.

9. Write a program to nd the sum of digits of a number.

10. Write a program to sort an array in ascending order.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy