0% found this document useful (0 votes)
17 views

Syntax For C++ Programming

This document provides an overview of key concepts in C++ programming including comments, variables, data types, operators, control flow statements like if/else and loops, functions, and arrays. It defines line and block comments, and describes how to declare and initialize different variable types like int, float, string, and char. It also covers output/input statements, basic math operators, if/else and while loop syntax, and how to create functions and arrays in C++.

Uploaded by

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

Syntax For C++ Programming

This document provides an overview of key concepts in C++ programming including comments, variables, data types, operators, control flow statements like if/else and loops, functions, and arrays. It defines line and block comments, and describes how to declare and initialize different variable types like int, float, string, and char. It also covers output/input statements, basic math operators, if/else and while loop syntax, and how to create functions and arrays in C++.

Uploaded by

hoelland
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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