LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

C++ syntax

So far our programs run from top to bottom, one line after another. This lesson introduces the tools that let a program make decisions (if statements) and repeat things (loops).

Conditions:

  1. If statements These are the building blocks of our code. An if statement runs the code between its curly braces only when its condition is true. It is written as: if(condition){}, for example:

    int main(){ 
    	int a = 3;
    	
    	if(a > 3){
    		cout<<"a is larger than 3";
    	}
    	return 0;
    }

    Here the condition is a > 3 ("is a larger than 3?"). Since a is exactly 3, the condition is false and the program prints nothing.

  2. Else: This block executes if our condition wasn't fulfilled: if(condition){}else{}

    int main(){ 
    	int a;
    	cin>>a;
    	
    	if(a > 3){
    		cout<<"a is larger than 3";
    	}else{
    		cout<<"a is not larger than 3";
    	}
    	return 0;
    }
  3. We can combine them and create else if blocks: if(condition){} else if(condition2){} The conditions are checked from top to bottom, and only the first one that is true runs:

    int main(){ 
    	int a;
    	cin>>a;
    	
    	if(a > 10){
    		cout<<"a is larger than 10";
    	}else if(a>5){
    		cout<<"a is not larger than 10, but it is larger than 5";
    	}else if(a > 2){
    		cout<<"a is not larger than 5, but it is larger than 2";
    	}else{
    		cout<<"a is not larger than 2";
    	}
    	return 0;
    }

Comparison:

When writing conditions for if statements we need to use some comparator between two values (like the > in if(a>2)).
These are all the operators that exist:

  • if (a > b) - is a greater than b
  • if (a < b) - is a less than b
  • if (a >= b) - is a greater than or equal to b
  • if (a <= b) - is a less than or equal to b
  • if (a == b) - is a equal to b
  • if (a != b) - is a not equal to b

Watch out for the double equals: a == b compares two values, while a single a = b assigns the value of b into a. Mixing these up is one of the most common beginner bugs.

The not operator ! can also be placed in front of other statements:

int main(){ 
	int a;
	cin>>a;
	
	if(!(a > 3)){
		cout<<"a is not greater than 3";
	}
	return 0;
}

We can also just put variables, if they are of the boolean type (technically we can use this for any data type but it is considered bad practice):

int main(){ 
	bool a = true;
	
	if(a){
		cout<<"a is true";
	}
	return 0;
}

Loops:

Loops repeat the code inside them until a condition is met

We have 2 main loops in c++ while() and for() they are interchangeable.

  1. While loops:
    They are written like while(condition), while the condition is true, the loop will repeat itself

Example:

int main(){ 
	int i = 0; //we initialize our own counter
	
	while(i < 10){ // while i is less than 10 the loop will repeat itself
		
		cout<<i<<" ";
		i++; //We have to increase the counter to avoid endless loops
	}
	return 0;
}

Output: 0 1 2 3 4 5 6 7 8 9

The i++ you see here is just a shorthand for i = i + 1 - "increase i by one".

  1. For loops:
    They are written like for(int i=0;i<n;i++)
    We have a few things to unpack here, so:
  • for() - calls the loop
  • int i=0; - declares a new integer i, and sets it value to 0
  • i < n; - this is the most important part, this tells our loop for how long to run for, we can treat it as while i is less than n
  • i++ - This is what happens after every iteration of the loop, it adds 1 to the value of i

Example:

int main(){ 
	for(int i=0; i<10;i++){
		cout<<i<<" ";
	}
}

Output: 0 1 2 3 4 5 6 7 8 9

Notice that the for loop does the exact same thing as the while loop above, just with the counter, the condition and the increase all packed into one line. That is why you will see for loops far more often in competitive programming.