Starting Programming Fundamentals in BSCS or BSIT 1st semester? This beginner guide covers the C++ basics you need for your first assignments, quizzes and exams: program structure, variables and data types, input/output, operators, conditions, loops, functions and arrays, each with a short example.
1. Structure of a C++ Program
#include <iostream> // library for input and output
using namespace std;
int main() // program starts here
{
cout << "Welcome to C++" << endl;
return 0; // program ended successfully
}
Every C++ program has header files, the main() function and statements that end with a semicolon ;. Comments start with // and are ignored by the compiler.
2. Variables and Data Types
A variable is a named memory location that stores a value. Common data types:
int– whole numbers, e.g.int age = 19;float/double– decimal numbers, e.g.double cgpa = 3.45;char– a single character, e.g.char grade = 'A';bool–trueorfalsestring– text, e.g.string name = "Ali";(needs#include <string>)
3. Input and Output
int marks;
cout << "Enter your marks: ";
cin >> marks;
cout << "You entered " << marks << endl;
cout with << prints to the screen; cin with >> reads from the keyboard.
4. Operators
- Arithmetic:
+ - * / %(%gives the remainder, e.g.7 % 3is 1) - Relational:
== != > < >= <= - Logical:
&&(AND),||(OR),!(NOT) - Increment/decrement:
i++,i--
Watch out for integer division: 7 / 2 is 3, but 7.0 / 2 is 3.5.
5. Decisions: if, else and switch
if (marks >= 85)
cout << "Grade A";
else if (marks >= 70)
cout << "Grade B";
else if (marks >= 50)
cout << "Pass";
else
cout << "Fail";
Use switch when you compare one variable with many fixed values, such as menu choices 1, 2, 3.
6. Loops: for, while and do-while
// print the table of 5
for (int i = 1; i <= 10; i++)
cout << "5 x " << i << " = " << 5 * i << endl;
- for – when you know how many times to repeat
- while – repeats while a condition is true (it may run zero times)
- do-while – always runs at least once, useful for menus
7. Functions
int square(int n)
{
return n * n;
}
int main()
{
cout << square(6); // prints 36
return 0;
}
Functions split a big program into small reusable parts. A function has a return type, a name, parameters and a body.
8. Arrays
int marks[5] = {78, 65, 90, 55, 84};
int sum = 0;
for (int i = 0; i < 5; i++)
sum += marks[i];
cout << "Average: " << sum / 5.0;
Array indexes start at 0, so the last element of marks[5] is marks[4]. Going past the end is a common bug.
Common Beginner Mistakes
- Forgetting the semicolon at the end of a statement
- Using
=(assignment) instead of==(comparison) insideif - Using a variable before giving it a value
- Off-by-one errors in loops (
<=instead of<) - Writing
void main(): modern compilers requireint main()
Practice Questions
- Write a program that reads two numbers and prints the larger one.
- Print all even numbers from 1 to 50 using a loop.
- Write a function that returns the factorial of a number.
- Read 10 marks into an array and print the highest and lowest.
- Make a simple calculator using
switchfor +, −, × and ÷.
Want more free practice? See our free study resources (LearnCpp, CS50 and more).
FAQ
Which compiler should I use for C++?
Code::Blocks (with MinGW), Dev-C++, Visual Studio Community or the online compiler at onlinegdb.com are all free and work well for university labs.
Is C++ hard for beginners?
C++ is strict, but if you practise small programs every day and learn the basics above in order, it becomes much easier within a few weeks.
0 comments:
Post a Comment