Professional Documents
Culture Documents
Control Structures in C++
Control Structures in C++
1. Sequence Statement
2. Selection Statements
3. Repetition Statements
CPE01 | PARAISO
C++ CONDITIONS AND IF STATEMENTS
C++ supports the usual logical conditions C++ has the following conditional
from mathematics: statements:
1. Single Alternative
2. Double Alternative
3. Multiple Alternatives
CPE01 | PARAISO
2. SELECTION STRUCTURE | SINGLE ALTERNATIVE
SINGLE ALTERNATIVE
This structure has the form:
If (condition) then:
[Module A]
[End of If structure]
CPE01 | PARAISO
2. SELECTION STRUCTURE | SINGLE ALTERNATIVE
EXAMPLE 1 EXAMPLE 2
DOUBLE ALTERNATIVE
If (Condition), then:
[Module A]
Else:
[Module B]
[End if structure]
CPE01 | PARAISO
2. SELECTION STRUCTURE | DOUBLE ALTERNATIVES
EXAMPLE 1 EXAMPLE 2
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
int i = 20; int i = 25;
if (i == 10) if (i > 15)
cout << "i is 10"; cout << "i is greater than 15";
else else
cout << "i is 20\n"; cout << "i is smaller than 15";
cout << "Ang gwapo mo talaga Vissel"; return 0;
return 0; }
}
CPE01 | PARAISO
2. SELECTION STRUCTURE | MULTIPLE ALTERNATIVES
MULTIPLE ALTERNATIVES
This structure has the form:
• The if Statement
• The if-else Statement
• The if-else-if ladder
• The switch-case Statement
CPE01 | BATAICAN
2. SELECTION STRUCTURE | THE IF STATEMENT
If (number < 0)
cout<<”The number is negative\n”; SYNTAX:
If (condition)
{
The message “The number is negative“ will statement 1;
be displayed if the value of a number is less statement 2;
than zero. This means that the condition is statement 3;
equal to true. statement 4;
}
CPE01 | BATAICAN
2. SELECTION STRUCTURE | THE IF-ELSE STATEMENT
SYNTAX: EXAMPLE:
if (condition) if (num < 0)
statement 1; cout<<num<<”is a negative number\n”;
else else
statement 2; cout<<num<<”is a positive number\n”;
if(grade>=75) cout<<"Passed\n";
} }
CPE01 | ARREZA
2. SELECTION STRUCTURE | THE SWITCH-CASE STATEMENT
• Switch is a multi-branch
decision statement in
C++ that tests a list of
integer and character
constants. When a match
is found, a statement
block is executed.
CPE01 | ARREZA
2. SELECTION STRUCTURE | THE SWITCH-CASE STATEMENT
There are three important things to know about the switch statement:
1. The switch differs from if in that switch can only test for equality,
whereas the if can evaluate a relational or logical expression.
2. No two case constants in the same switch can have identical
values. Of course, a switch statement enclosed by an outer switch
may have case constants that are the same.
3. If character constants are used in the switch, they are automatically
converted to their integer values.
CPE01 | ARREZA
2. SELECTION STRUCTURE | THE SWITCH-CASE STATEMENT
BREAK
• The break statement has two uses,
the first one is to terminate a case in
the switch statement, while the
second one is to terminate a loop
and bypass the normal loop
conditional tests.
• When a break statement is
encountered inside the loop, the
loop is immediately terminated and
the program control resumes at the
next statement following the loop.
CPE01 | ARREZA
2. SELECTION STRUCTURE | THE SWITCH-CASE STATEMENT
EXAMPLE: OUTPUT:
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"input a number from 1 to 5: ";
cin>>num;
switch (num)
{
case 1:cout<<"ONE";
break;
case 2:cout<<"TWO";
break;
case 3:cout<<"THREE";
break;
case 4:cout<<"FOUR";
break;
case 5:cout<<"FIVE";
break;
default: cout<<"Number is out of range";
}
return 0;
}
CPE01 | ARREZA
2. SELECTION STRUCTURE | THE SWITCH-CASE STATEMENT
SYNTAX:
for (initialization ; condition ; increment)
/statement
CPE01 | ESMERO
3. REPETITION STRUCTURE | FOR LOOP
EXAMPLE: OUTPUT:
#include <iostream>
using namespace std;
int main()
{
return 0;
}
CPE01 | BARIA
3. REPETITION STRUCTURE | WHILE LOOP
EXAMPLE: OUTPUT:
#include<iostream>
using namespace std;
int main()
{
int number;
number=0;
while (number<3)
{
cout<<"Congratulations\n";
number=number+1;
}
return 0;
}
CPE01 | BARIA
3. REPETITION STRUCTURE | DO-WHILR LOOP
SYNTAX:
do
{
statement;
}
while (condition);
CPE01 | BARIA
3. REPETITION STRUCTURE | DO-WHILR LOOP
do
the most common use of the do-while
{
loop is in a menu-selection routine. cout<<”Choose an operation “;
Because, you will always want a menu- cout<<”[1] Addition \n”;
selection routine to execute at least cout<<”[2] Subtraction \n”;
once, the do-while loop is an obvious cout<<”[3] Multiplication \n”;
cout<<”[4] Division \n”;
choice. By testing for a valid response at cout<<”Enter your choice: “;
the bottom of the loop, you can re- cin>>choice;
prompt the user until a valid response is } while(choice<1 || choice>4);
entered. The following program
fragment shows how to add a do-while After the options have been displayed, the
loop onto the menu for the four basic program will loop until a valid option is
operations program. selected. Loops can also be nested.
CPE01 | BARIA
3. REPETITION STRUCTURE | DO-WHILE LOOP
EXAMPLE 1 OUTPUT:
#include<iostream>
using namespace std;
int main()
{
int num;
do
{
cin>>num;
}
while(num>100);
return 0;
}
EXAMPLE 2 OUTPUT:
#include <iostream>
using namespace std;
int main()
{
int number=44;
int guess;
cout<<"Guess a number between 1 and 100\n";
do{
cout<<"Enter your guess: ";
cin>>guess;
if (guess > number)
cout<<"Too high\n";
if (guess < number)
cout<<"Too low\n";
} while (guess != number);
cout<<"You win. The answer is "<<number;
return 0;
}
REFERENCES/SOURCES
circuitstoday.com. https://www.circuitstoday.com/control-structures-in-c-and-cpp
cplusplus.com. http://www.cplusplus.com/doc/oldtutorial/control/
Geeks for Geeks. https://www.geeksforgeeks.org/control-structures-in-programminglanguages/
Little Drops. https://www.cpp.thiyagaraaj.com/control-structures
informIT https://www.informit.com/articles/article.aspx?p=1321841&seqNum=2#:~:text=C%2B%2B%20has%20only%20three%20kinds,while).
w 3 s c h o o l s . c o m . https://www.w3schools.com/cpp/cpp_conditions.asp
https://www.cs.fsu.edu/~myers/c++/notes/control1.htm