02 Syntax II
02 Syntax II
Syntax II
Syntax II Control Flow Functions
Overview
This lecture shall cover the basics of statements in C++. We will be touching upon:
● Selection statements
● Iteration statements
● Functions (subsection of statements)
Since this is a competitive programming course, we will skip pass the following:
● Pointers
● References
● Object-oriented programming
As always, feel free to read these on your own if you are curious :D
Syntax II Control Flow Functions
if statements
We use the if statement whenever we want to execute statement(s) based
on some condition. It has the following format
● if (condition) statement(s):
○ The statement(s) part executes only if the condition is true.
#include <iostream>
int main() {
bool b = true;
if (b) std::cout << "The condition is true.";
}
Syntax II Control Flow Functions
if statements (multiple)
We can use the block scope {} to execute multiple statements if the
condition is true.
#include <iostream>
int main() {
bool b = true;
if (b) {
std::cout << "This is the first statement.";
std::cout << "\nThis is a second statement.";
}
}
Syntax II Control Flow Functions
if statements (multiple)
If your condition is more complex you can also utilize the boolean operators
mentioned previously
#include <iostream>
int main() {
int n = 10;
if (n > 0 && n%2==0) { //you can replace && with and
std::cout << "N is a positive even integer";
}
if (n > 0 and n%2==0) { //you can also replace || with or
std::cout << "N is a positive even integer";
}
}
Syntax II Control Flow Functions
if-else statements
Building upon what we’ve learnt so far, an if-else statement can be described as
follows:
if (condition) statement(s)
else other_statement(s)
#include <iostream>
int main() {
bool b = false;
if (b) std::cout << "The condition is true.";
else std::cout << "The condition is false.";
}
for statements
Here is how you do a for-loop in C++
You must have the 3 things in the for loop separated by semicolons.
Syntax II Control Flow Functions
for statements
The for statement executes code in a loop. The execution depends on the condition.
Consider the following example.
#include <iostream>
int main() {
for (int i = 0; i < 10; i++) {
std::cout << "The counter is: " << i << '\n';
}
}
Syntax II Control Flow Functions
While statements
while(condition is true){
do stuff
}
Note that if your condition never becomes false your program will die
Syntax II Control Flow Functions
While statements
#include <iostream>
int main() {
//code does the same thing as the first for loop example
int i = 0;
while(i < 10){
std::cout<< “The counter is: “<< i << ‘\n’;
i++;
}
}
Syntax II Control Flow Functions
int i = 1;
while(i <= 10){
std::cout<< “The counter is: “<< i << ‘\n’;
i++;
}
int i = 0;
while(i < 10){
std::cout<< “The counter is: “<< i << ‘\n’;
i+=2;
}
Syntax II Control Flow Functions
Calling on someone
Program
When you run a C++ program, the main function is called. You can
have helper functions that assist the main function.
The syntax is the exact same except you have to change the name of
the function and you can provide input parameters as well.
If your function is not a void function you MUST have a return value or
else it will return garbage.
Syntax II Control Flow Functions
Example
#include <iostream>
void print(int i) {
int main() {
print(i);
print(100);
}
Syntax II Control Flow Functions
Example
#include <iostream>
int add(int a,int b){
return a+b;
}
int main() {
cout<<add(1,2)<<’\n’;
cout<<add(add(3,4),5)<<’\n’;
}