[go: up one dir, main page]

0% found this document useful (0 votes)
42 views23 pages

The Basics of C++: Syntax

This document provides an overview of some basic C++ concepts: 1) It describes different data types in C++ like char, int, float, double, and bool and notes that different data types take up different amounts of memory. 2) It explains that characters are indicated with single quotes and special characters use escape sequences like \n and \t. 3) It provides examples of common loops and conditional statements like for, while, if/else, and switch that allow program flow control.

Uploaded by

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

The Basics of C++: Syntax

This document provides an overview of some basic C++ concepts: 1) It describes different data types in C++ like char, int, float, double, and bool and notes that different data types take up different amounts of memory. 2) It explains that characters are indicated with single quotes and special characters use escape sequences like \n and \t. 3) It provides examples of common loops and conditional statements like for, while, if/else, and switch that allow program flow control.

Uploaded by

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

The Basics of C++

Syntax

char: characters like a, t, x, etc.


int: integers like 1, 9, -23
float: number including decimal like 1.2 and 3.14
double: number including decimal
bool: boolean (true/false) value

Numbers can be signed or unsigned

Data Types

Different data types necessitate different amounts


of space (talking of memory).
What is space?
Bits & Bytes
RAM vs. your Hard Drive

Data Types Take Space

Different data types necessitate different amounts


of space (talking of memory).
A character needs just a single byte.
Decimal precision necessitates more bytes.
Space is compiler specific.

Data Types Take Space

Notice that chars are indicated by single quotes.


Rather than use single quotes, you can use the
decimal
representation.

A word on characters

http://www.asciitable.com/

The backslash character (\) is called the escape


character; it is used to signal that a special
character follows. Some special characters:
\0: null terminator
\n: new line
\t: tab

A word on characters

The FOR Loop


for(i=0; i <20; i++)
{
Do something

Loops

The WHILE Loop


i=0;
while(i <20)
{
Do something
i++;

Loops

Incrementing and
Decrementing Variables
i++
i++ is
is equivalent
equivalent to
to i=i+1
i=i+1
i-i-- is
is equivalent
equivalent to
to i=i-1
i=i-1
i+=5
i+=5 is
is equivalent
equivalent to
to i=i+5
i=i+5
i-=2
i-=2 is
is equivalent
equivalent to
to i=i-2
i=i-2

If then
if(some condition)
{
Do something

Conditional Statements

10

If thenelse
if(some condition)
{
Do something

}
else
{
Do something else

Conditional Statements

11

If thenelse
if(some condition)
{
Do something

}
else if(some other condition)
{
Do something else

Conditional Statements

12

&&: and
||: or
==: equal to
!=: not equal to
>: greater than
<: less than
>=: greater than or equal to
<=: less than or equal to

Conditional Operators

13
if(x==3)
{

if(x==3)
{

do something

}
if(x==4)
{
do something

do something

}
else if(x==4)
{
do something

If x=3, ifif statement makes two comparisons. The


ifelse if statement only makes 1.

Conditional Statements

14

The SWITCH Statement


switch(c)
{
case 1: Do something
case 2: Do something
case 3: Do something

Conditional Statements

15

After Loops and Conditional Statement headers,


all commands within the braces { } will be
executed.
If there is only one command, the braces are
optional.
for(i=0; i<j; i++)
{
cout << i << endl;
}
cout << done.;

or

for(i=0; i<j; i++)


cout << i << endl;
cout << done.;

A Word on Braces

16

With the exception of the header line of


conditional and loop statements, all other
statements must end with a semicolon (;).
You can have as many statements on a single
line. White space means nothing.
A statement is executed when the semicolon is
encountered.

The semicolon

17

1
2
3
4
5
6
7
8
9
10

x=3
y=2
if(x==3)
{
y++
}
else
{
y=y-1
}

Where do the Semicolons Go?

18

To have a comment run multiple lines,


/* blah, blah, blah, blah,
blah, blah, blah, blah */
To have a comment on a single line,
// blah, blah, blah, blah

A Comment on Comments

19

To take input and output from the console


window, we use the following syntax:
cin>> response; //the user types in
//some value that is
//stored in a variable
//called response
cout<< You responded << response;

Interacting with User

20

int main ()
{
int i, j;
i=3;
j=i++;
return j;

Function name
Data type to be returned
Parameter list
-This function does not have any parameters.

Return statement

Function Format

21

The main function is the entry point for a


program.
You can only have one main function.
You must have a main function.
While programming practices often have the
main function return an integer, although the
main function can return any data type or none
(void).

The main Function

22

int sum_two_numbers(int x, int y)


{
return x+y;

Parameter list
There are two parameters of type int.

Function Format

23

int main()
{
int i, j;
i=3;
j=i++;
return j;

What is this code doing??

A Sample Program

You might also like