0% found this document useful (0 votes)
37 views

CC 104 Lesson5

This document discusses different types of flow control operators in C++ including if/else statements, for loops, do/while loops, and while loops. It provides the syntax for each operator and short descriptions of when each would be used. Examples are given for if/else statements to check if a number is positive or negative, and the basic logic of for and while loops is explained.

Uploaded by

RicHArd
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)
37 views

CC 104 Lesson5

This document discusses different types of flow control operators in C++ including if/else statements, for loops, do/while loops, and while loops. It provides the syntax for each operator and short descriptions of when each would be used. Examples are given for if/else statements to check if a number is positive or negative, and the basic logic of for and while loops is explained.

Uploaded by

RicHArd
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/ 10

CC 104 – C+

+ FLOW
CONTROL
OPERATORS

• If Else Statement (with else if in video) (Not Nested)


• For Loop (Not Nested)
• Do and While Loop (Not Nested)
IF ELSE STATEMENT

• if specify a block of code to be executed, if a specified condition is true. Use else to


specify a block of code to be executed, if the same condition is false. Use else if to specify
a new condition to test, if the first condition is false.
Syntax:
if (condition) {
// body of if statement condition is true
}else{
// body of if statement condition false
}
SAMPLE PROBLEM

Create a program to check whether an integer is positive or negative. This program considers
0 as a positive number
FOR LOOP
for loops, are great for when you already know how
many times you want to loop through something. When
using a for loop, we typically use a counter that will
either increment or decrement until a condition is met.
Once the condition is met, the loop will stop. The
image below should give you a basic understanding of
how a for loop works.
Syntax
for (initialization; condition; update) {
// body of-loop
}
RANGE BASED LOOP
WHILE LOOP A while loop is slightly different than a for loop for
the fact that it’s good to use when we don’t know
how many times we want to loop through a problem
beforehand. This is the key difference between using
a for loop or a while loop. To get a basic idea of how
a while loop works, take a look at the image below.
Syntax:

while (condition) {
// body of the loop
}
DO WHILE LOOP A similar way to set up a while loop is with a do…while.
A do…while statement is similar to a while loop in the
fact that it will continue to run until the condition
becomes false. The only difference is the order in which
the loop runs. Here’s a simple example of a do…while
statement:
do {

// body of loop;

while (condition);
PLEASE VISIT PROGRAMIZ C++
TUTORIALS

https://www.programiz.com/cpp-programming/do-while-loop

You might also like