Syntax For C++ Programming

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

C++ PROGRAMMING BY T G

C++ COMMENTS

Line comments
//put your comment here

Block Comments
/*
put your lines of comments here
you can put as many comments as possible
*/

THE DISPLAY STATEMENT

//display text without any variables


cout<<"Put your text in here"<<endl;

//key c++ terms on a cout statement


cout - send the print command to your computer
<< - insertion operator: it inserts text on the output console (which is
why when opening programs we select console application)

endl - Go to the new line

C++ VARIABLE TYPES

integer (int) e.g 1 , 20 , 34 i.e any number without a comma


float (float) e.g 1.00 , 12.03 , 100.34
string (string) e.g "I love C++" , "Donald William III"
double (double) e.g same as string but can contain upto 16 decimal places
character (char) e.g 'A' , 'a' , '&' , character values must be put inside
singlequotes ('')

DECLARING VARIABLES
1. Declaring and initializing a variable
variableType variableName = value; e.g int var1 = 10;

2. Declaring a varibale without initializing it


variableType variableName; e.g int var1;

3. Declaring a constant variable


const variableType VARIABLE_NAME = value; #value must be of variableType

THE DISPLAY STATEMENT WITH VARIABLES

//display a single variable on a line


cout<<"Put your text in here"<<variable_name<<endl;

//if you want to display more than one variable use the following
cout<<"Put your text in here"<<variable_name1<<"your text here
"<<variable_name12<<endl;
THE INPUT STATEMENT

cout<<"Your instruction here: ";


cin>>variable_name;

cin - sends a command to the computer to activate the cursor for user to
enter something
>> - extraction operator: it extract values from the keyboard

C++ OPERATORS

addition (+)
subtraction (-)
multiplication (*)
division (/) *note: this sign only returns the quotient after dividing two
numbers
division (%) *note: this sign only returns the remainder after dividing two
numbers

SYNTAX OF AN IF STATEMENTS

if(condition)
{
statements;
}

SYNTAX OF AN IF ELSE STATEMENTS


if(condition)
{
statements1;
}
else
{
statements2;
}

LOGICAL OPERATORS

AND (&&) returns true when all conditions are true


OR (||) returns true if one or both of the conditions is true
NOT (!=) returns true when the values are not equal
EQUALITY (==) returns true if both variables are equal

SYNTAX OF A WHILE LOOP


1. while loop without initialization

while(condition)
{
statements;
}
2. while loop with initialization

initialization;
while(condtion)
{
statements;
increament;
}

SYNTAX OF A NESTED IF ELSE STATEMENTS

if(condition1)
{
statements1;
}
else if(condition2)
{
statements2;
}
else if(conditionN)
{
statementsN;
}
else
{
statements;
}

SYNTAX OF A SWITCH STATEMENT

switch(selector)
{
case Label1:
statements1;
break;
case Label2:
statements2;
break;
:
case LabelN:
statementsN;
break;
default:
statementsD;
}

SYNTAX OF A FOR LOOP

for(initialization; condition; increament)


{
statements;
}
SYNTAX OF A NESTED FOR LOOP

for(initialization; condition; increament)


{
for(initialization2; condition; increament)
{
statements;
}
}

FUNCTIONS
FUNCTION HEADERS
ReturnType functionName(parameters)

FUNCTION DEFINITION
ReturnType functionName (parameters)
{
statements;
}

FUNCTION CALL
variableName = function(valueParameters);
cout<<functionName(valueParameters)<<endl;

functionName(valueParameters); //calling a void function

ARRAY DECLERATION

ONE DIMENSIONAL ARRAY


arrayType arrayName[ARRAY_SIZRE];

TWO DIMENSIONAL ARRAY


arrayType arrayName[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];

STRUCTS
struct structName
{
memberVariables;
};

DECLARING VARIABLES OF A STRUCT TYPE


structName variableName;

You might also like