0% found this document useful (0 votes)
68 views14 pages

C Basics

This document provides an introduction to C++ basics, including: - The structure of a simple C++ program with include headers, main function, and return 0 - How a C++ program is compiled to check for errors and translate to machine code - Common elements like comments, indentation, statements ending with semicolons - An example "Hello World" program - How to declare variables, read input with cin, and output to the console with cout - Basic control structures like if/else statements and while loops - Boolean operators and conditions to use in control structures - An example program demonstrating if/else and while loops

Uploaded by

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

C Basics

This document provides an introduction to C++ basics, including: - The structure of a simple C++ program with include headers, main function, and return 0 - How a C++ program is compiled to check for errors and translate to machine code - Common elements like comments, indentation, statements ending with semicolons - An example "Hello World" program - How to declare variables, read input with cin, and output to the console with cout - Basic control structures like if/else statements and while loops - Boolean operators and conditions to use in control structures - An example program demonstrating if/else and while loops

Uploaded by

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

C++Basics

CSci107

AC++program
//include headers; these are modules that include functions that
you may use in your
//program; we will almost always need to include the header that
// defines cin and cout; the header is called iostream.h
#include <iostream.h>
int main() {
//variable declaration
//read values input from user
//computation and print output to user
return 0;
}
AfteryouwriteaC++programyoucompileit;thatis,yourunaprogramcalled
compilerthatcheckswhethertheprogramfollowstheC++syntax
ifitfindserrors,itliststhem
Iftherearenoerrors,ittranslatestheC++programintoaprograminmachine
languagewhichyoucanexecute

Notes

whatfollowsafter//onthesamelineisconsideredcomment

indentationisfortheconvenienceofthereader;compilerignoresallspaces
andnewline;thedelimiterforthecompileristhesemicolon

allstatementsendedbysemicolon

Lowervs.uppercasematters!!
Voidisdifferentthanvoid
Mainisdifferentthatmain

Theinfamous
Helloworldprogram
Whenlearninganewlanguage,thefirstprogrampeople
usuallywriteisonethatsalutestheworld:)
HereistheHelloworldprograminC++.
#include <iostream.h>
int main() {
cout << Hello world!;
return 0;

Variabledeclaration
type variable-name;
Meaning:variable<variablename>willbeavariableoftype<type>
Wheretypecanbe:
int
double
char

//integer
//real number
//character

Example:
int a, b, c;
double x;
int sum;
char my-character;

Inputstatements
cin >> variable-name;
Meaning:readthevalueofthevariablecalled<variable
name>fromtheuser

Example:
cin >>
cin >>
cin >>
cin >>

a;
b >> c;
x;
my-character;

Outputstatements
cout << variable-name;
Meaning:printthevalueofvariable<variablename>totheuser
cout << any message ;
Meaning:printthemessagewithinquotestotheuser
cout << endl;
Meaning:printanewline
Example:
cout << a;
cout << b << c;
cout << This is my character: << my-character <<
he he he
<< endl;

Ifstatements
True
if(condition){
S1;
}
else{
S2;
}
S3;

condition

False

S2

S1

S3

Booleanconditions
..arebuiltusing
Comparisonoperators
==equal
!=notequal
<lessthan
>greaterthan
<=lessthanorequal
>=greaterthanorequal

Booleanoperators
&&and
||or
!not

Examples
Assumewedeclaredthefollowingvariables:
int a = 2, b=5, c=10;
Herearesomeexamplesofbooleanconditionswecanuse:
if (a == b)
if (a != b)
if (a <= b+c)
if(a <= b) && (b <= c)
if !((a < b) && (b<c))

Ifexample
#include <iostream.h>
void main() {
int a,b,c;
cin >> a >> b >> c;
if (a <=b) {
cout << min is << a << endl;
}
else {
cout << min is << b << endl;
}
cout << happy now? << endl;
}

Whilestatements
while(condition){
S1;
}
S2;

True

condition

S1

S2

False

Whileexample
//read 100 numbers from the user and output their sum
#include <iostream.h>
void main() {
int i, sum, x;
sum=0;
i=1;
while (i <= 100) {
cin >> x;
sum = sum + x;
i = i+1;
}
cout << sum is << sum << endl;
}

Exercise
Writeaprogramthataskstheuser
Do you want to use this program? (y/n)

Iftheusersaysythentheprogramterminates
Iftheusersaysnthentheprogramasks
Are you really sure you do not want to use this
program? (y/n)
Iftheusersaysnitterminates,otherwiseitprintsagainthe
message
Are you really really sure you do not want to use
this program? (y/n)
Andsoon,everytimeaddingonemorereally.

You might also like