C++

Nils Wiki>Formal Sciences>Computer Sciences>Software Systems>Programming languages>C++

Control Loops
while( condition ) statement;

do while ''statement; while ( condition)

for ( initialization; condition; increase ) statement;

Jump statements: break		for (n=10; n>0; n--) { cout << n << ", "; if (n==3) { cout << "countdown aborted!"; break; } } return 0; continue continiue	for (int n=10; n>0; n--) { if (n==5) continue; cout << n << ", "; }cout << "FIRE!\n"; return 0; goto		int n=10;loop:cout << n << ", "; n--;if (n>0) goto loop;cout << "FIRE!\n"; return 0; exit		void exit (int exitcode); switch		switch (expression){case constant1:group of statements 1;break;case constant2:group of statements 2;break;default:default group of statements}

Conditional Execution: if 		(total>O) printf("OK"); if else		if (condition) statement1;else statement2; if else if else

, Comma is used to separate two or more expressions that are included where only one expression is expected. a = (b=3, b+2); <<	SHL	Shift Left >>	SHR	Shift Right change case		int i; float f = 3.14; i = (int) f; a = sizeof (char);
 * used at the end of a statement

Constants: 75        // decimal 0113      // octal 0x4b      // hexadecimal 75        // int 75u       // unsigned int 75l       // long 75ul      // unsigned long 'p' "Hello world" L"This is a wide character string" const int pathwidth = 100;
 * 1) define PI 3.14159265

Structure and Nesting:

Functions:	A function is simply a chunk of C code (statements) that you have grouped together and given a name. Use that "chunk" of code repeatedly simply by writing its name demo{printf("Hello"); total = total + 1;} Once you have defined your function you can use it within a program: main{demo;} you can declare variables within it

You can define special variables called parameters which are used to carry data values into a function sum( int a, int b){int result;result=a + b;} sum(l,2);is a call to the sum function with a set to 1 and b set to 2 A function can return a value via its name - it's as if the name was a variable and had a value. return result; int sum(int a, int b){int result;result = a + b;return result;} declare the function separately at the start of the program. For example: int sum;main{etc...

main{}	The main Program-Code,sort of. demo{}	Can be used in the main - function, as a seperat block of code, that erases itself at the end of the block.

Arrays:

main { int al,a2,a3,a4,a5; scanf("%d %d %d %d %d",&a1,&a2,&a3,&a4,&a5); printf("%d %d %d %d %d'',a5,a4,a3,a2,a1); }

Shorts down the amount of int or char you have to put into the code.

main { char a[5]; int i;

for(i=0;i<5;++i) scanf("%c",&a[i]);

for(i=4;i>=0;--i) printf("%c",a[i]); }

Pointers: int *a		Creates a pointer. A pointer can store a certain variables adress, in other words, find a variables value. & 		is the reference operator and can be read as "address of"
 * is the dereference operator and can be read as "value pointed by"