0% found this document useful (0 votes)
21 views10 pages

Simple Basics and Tips

Uploaded by

lewissikanyika35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views10 pages

Simple Basics and Tips

Uploaded by

lewissikanyika35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SIMPLE AND EASY C++

PROGRAMING BASICS
AND TIPS
User Input and Output in C++

 To get user input, we use cin.


 To show output to the user, we use cout.
 #include <iostream> is needed to use cin and cout.
 using namespace std; makes it easier to use cin and cout.
 cout << "Hello!"; prints "Hello!" to the screen.
 int age; declares a variable named age.
 cin >> age; gets a number from the user and stores it in age.
 We can combine cout and cin for interaction: cout << "Enter your age: "; cin >> age;.
 endl moves to the next line in the output.
 You can also use \n to move to the next line: cout << "Hello!\n";.
 Use getline(cin, name); to read a whole line of text.
 string name; declares a variable named name that can hold text.
 To print both text and variables: cout << "You are " << age << " years old.";
 You can print multiple variables: cout << "Name: " << name << ", Age: " << age;.
 Always remember to include #include <iostream> at the top of your program.
 After you get input from cin, use cin.ignore(); to clear the input buffer if you want to read text
next.
Conditions (if and switch case) In C++

 Conditions allow your program to make decisions.


 Use if to check a condition.

Example: if (age > 18) { cout << "You are an adult."; }
The code inside {} runs if the condition is true.
Use else for what to do if the condition is false.

Example: if (age > 18) { cout << "You are an adult."; } else { cout << "You are not an adult."; }
You can check multiple conditions with else if.
Example: if (age > 18) { cout << "Adult"; } else if (age > 12) { cout << "Teen"; } else { cout << "Child"; }
switch is used for multiple choices based on a single variable.

Example:
cpp
Copy code
switch (day) {
case 1: cout << "Monday"; break;
case 2: cout << "Tuesday"; break;
default: cout << "Other day";
}

 switch checks the value of day.


 Each case checks for a different value.
 break ends each case to prevent falling into the next one.
 default runs if none of the cases match.
 Always remember to put a break after each case.
 Use == to compare values in if and switch.
 Example of if: if (a == b) { cout << "Equal"; }
 Example of switch for grades:
Copy code
switch (grade) {
case 'A': cout << "Excellent"; break;
case 'B': cout << "Good"; break;
case 'C': cout << "Average"; break;
default: cout << "Poor";}
Loops (for, while, do-while)
 Loops let you run the same code multiple times.
 for loop runs a set number of times.

Example:
for (int i = 0; i < 5; i++)
{
cout << i;
}
 i = 0 starts the loop.
 i < 5 is the condition to keep running.
 i++ increases i by 1 each time.
 while loop runs while a condition is true.
Example:
while (i < 5)
{
cout << i;
i++;
}
 i < 5 is the condition to keep running.
 do-while loop runs at least once, then checks the condition.
Example:
cpp
Copy code
do {
cout << i;
i++;
} while (i < 5);

 do starts the loop.


 while (i < 5); is the condition to keep running.
 for is good when you know how many times to loop.
 while is good when you loop until something happens.
 do-while is good when you need to run at least once.
 break stops the loop early.

Example:
for (int i = 0; i < 10; i++)
{
if (i == 5)
break;
}
 continue skips to the next loop iteration.

Example:
for (int i = 0; i < 10; i++)
{
if (i == 5)
continue;
cout << i;
}
 Always check your loop conditions to avoid infinite loops.

Example of an infinite loop:
while (true)
{
cout << "Forever";
}
Functions (void and return types)
 Functions let you group code together to use it multiple times.
 void functions don't return a value.

Example:
void greet()
{
cout << "Hello!";
}
 Call a function with its name: greet();
 Functions can take parameters (input).
Example:
void greet(string name)
{
cout << "Hello, " << name;
}
 Call with a parameter: greet("Alice");
 Functions can return a value.

Example: int add(int a, int b)


{
return a + b;
}
 int sum = add(3, 4); calls add and stores the result in sum.
 return ends the function and sends back a value.
 Use void for actions, and types like int for calculations.

Example:
cpp
Copy code
void printSum(int a, int b) {
int sum = a + b;
cout << "Sum: " << sum;
}

Example of a function that returns a value:


cpp
Copy code
int multiply(int a, int b) {
return a * b;
}
 You can have multiple functions in a program.
 Always declare functions before using them.

Example of declaration and definition:


cpp
Copy code
int subtract(int, int); // Declaration
int subtract(int a, int b) { // Definition
return a - b;
}
 Use comments to describe what your functions do.
 Example of using a void function with parameters:

Copy code
void sayHello(string name) {
cout << "Hello, " << name << "!";
}
Call it with: sayHello("Bob");
Arrays
 Arrays store multiple values of the same type.

Example: int numbers[5]; declares an array of 5 integers.

 Access elements with an index: numbers[0] = 10; sets the first element.
 Indexes start at 0.

Example: cout << numbers[0]; prints the first element.
Initialize with values: int numbers[5] = {1, 2, 3, 4, 5};

 Use loops to access array elements.



Example:
cpp
Copy code
for (int i = 0; i < 5; i++) {
cout << numbers[i];
}

 Arrays can hold any type: float, char, string.

Example: float scores[3] = {98.5, 85.0, 90.0};

Access all elements with a loop:


cpp
Copy code
for (int i = 0; i < 3; i++) {
cout << scores[i];
}

 Change values by index: numbers[2] = 100;


 Arrays are fixed in size.

Example: char letters[4] = {'A', 'B', 'C', 'D'};


Print array with a loop:
cpp
Copy code
for (int i = 0; i < 4; i++) {
cout << letters[i];
}
 Declare without initializing: int data[10];
 Be careful with array bounds (indexes must be within size).

Example of accessing elements:
cpp
Copy code
int ages[3] = {20, 25, 30};
cout << ages[0]; // Prints 20
cout << ages[2]; // Prints 30

You can use arrays with functions.


Example
Copy code
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i];
}
}
TASKS TO TRY AND GRAB THE BASICS

 Hello World Program


 Simple Calculator
 Number Guessing Game
 Todo List
 Simple File Management System
 Temperature Converter
 Student Grade Book
 Bank Account Manager
 Number Patterns
 Character Counter
 Palindrome Checker
 Factorial Calculator
 Prime Number Checker
 Basic File Reader/Writer
 Simple Stopwatch
 BMI Calculator
 Simple Calendar
 Currency Converter

Good luck learning.

You might also like