0% found this document useful (0 votes)
6 views17 pages

02 Syntax II

Uploaded by

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

02 Syntax II

Uploaded by

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

Syntax II Control Flow Functions

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.";
}

Your task now is to implement this in multiple statements. This should be


pretty easy.
Syntax II Control Flow Functions

for statements
Here is how you do a for-loop in C++

for (initialization;condition;do stuff) {


//stuff you do in the for loop
}

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

for statements (more examples)


for (int i = 1; i <= 10; i++) {
std::cout << "The counter is: " << i << '\n';
}
for (int i = 0; i < 10; i+=2) {
std::cout << "The counter is: " << i << '\n';
}
for (int i = 9; i >= 0; i--) {
std::cout << "The counter is: " << i << '\n';
}
Syntax II Control Flow Functions

for statements(more examples)


What is wrong with this code?

for (int i = 9; i >= 0; i++) {


std::cout << "The counter is: " << i << '\n';
}
Syntax II Control Flow Functions

While statements

The while statement executes a block of code as the condition specified


within it is true. It will terminate only when the condition specified within
it is false.

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

The while statement executes a block of code as the condition specified


within it is true. It will terminate only when the condition specified within
it is false.

#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

While statements(more examples)

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

Sometimes you can’t do something yourself.

So you have to call for help.

That is basically what a function is for.


Syntax II Control Flow Functions

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) {

std::cout << "The counter is: " << i << '\n';

int main() {

for (int i = 0; i < 10; i++) {

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’;
}

You might also like