Lesson 3 Control Structures C++ For Students
Lesson 3 Control Structures C++ For Students
C++ if Statement................................................................................................................................1
Working of if Statement................................................................................................................1
Flowchart of if..................................................................................................................................2
Example 1: C++ if Statement......................................................................................................3
C++ if...else.........................................................................................................................................4
Working of if...else Statement....................................................................................................4
Flowchart of if...else......................................................................................................................5
Example 2: C++ if...else Statement..........................................................................................5
C++ Nested if...else...........................................................................................................................6
Syntax of Nested if...else.............................................................................................................6
Example 3: C++ Nested if...else Statement............................................................................7
Conditional/Ternary Operator ?:....................................................................................................8
C++ switch Statement...........................................................................................................................8
Syntax of switch....................................................................................................................................8
Flowchart of switch Statement........................................................................................................9
Example 1: C++ switch Statement................................................................................................10
C++ for Loop Syntax....................................................................................................................12
How for loop works in C++ Programming?............................................................................12
Flowchart of for Loop in C++.....................................................................................................12
Example 1: C++ for Loop...........................................................................................................13
C++ break and continue Statement................................................................................................14
C++ break Statement......................................................................................................................14
Syntax of break.............................................................................................................................15
Working of break Statement......................................................................................................15
Example 1: C++ break................................................................................................................15
C++ continue Statement.................................................................................................................16
Syntax of continue.......................................................................................................................16
Working of continue Statement................................................................................................17
Example 2: C++ continue...........................................................................................................17
C++ goto Statement............................................................................................................................18
Syntax of goto Statement...................................................................................................................18
Example 1: goto Statement............................................................................................................18
Reason to Avoid goto Statement................................................................................................19
The if , if...else and nested if...else statement are used to make one-time decisions in
C++ Programming, that is, to execute some code/s and ignore some code/s depending upon
the test condition. Without decision making, the program runs in similar way every time.
Decision making is an important feature of every programming language using C++
programming. Before you learn decision making, you should have basic understanding of
relational operators.
C++ if Statement
The if statement checks whether the test condition is true or not. If the test condition is
true, it executes the code/s inside the body of if statement. But it the test condition is false,
it skips the code/s inside the body of if statement.
Working of if Statement
The if keyword is followed by test condition inside parenthesis ( ). If the test condition is
true, the codes inside curly bracket is executed but if test condition is false, the codes
inside curly bracket { } is skipped and control of program goes just below the body of if as
shown in figure above.
Flowchart of if
#include <iostream>
using namespace std;
int main() {
int number;
cout<< "Enter an integer: ";
cin>> number;
Output 1
Enter an integer: 5
Output 2
Enter a number: -5
C++ if...else
The if...else executes body of if when the test expression is true and executes the body
of else if test expression is false.
Flowchart of if...else
Example 2: C++ if...else Statement
C++ Program to check whether integer entered by user is positive or negative
(Considering 0 as positive)
#include <iostream>
using namespace std;
int main() {
int number;
cout<< "Enter an integer: ";
cin>> number;
if ( number >= 0) {
cout << "You entered a positive integer: "<<number<<endl;
}
else {
cout<<"You entered a negative integer: "<<number<<endl;
}
Output
Enter an integer: -4
else {
statements to be executed if all test expressions are false;
The nested if...else statement has more than one test expression. If the first test
expression is true, it executes the code inside the braces{ } just below it. But if the first test
expression is false, it checks the second test expression. If the second test expression is
true, if executes the code inside the braces{ } just below it. This process continues. If all the
test expression are false, code/s inside else is executed and the control of program jumps
below the nested if...else
#include <iostream>
using namespace std;
int main() {
int number;
cout<< "Enter an integer: ";
cin>> number;
if ( number > 0) {
cout << "You entered a positive integer: "<<number<<endl;
}
else if (number < 0){
cout<<"You entered a negative integer: "<<number<<endl;
}
else {
cout<<"You entered 0."<<endl;
}
cout<<"This statement is always executed because it's outside nested if..else statement." ;
return 0;
Output
Enter an integer: 0
You entered 0.
Conditional/Ternary Operator ?:
Conditional operators are the peculiar case of if...else statement in C++ Programming.
Consider this if..else statement:
if ( a < b ) {
a = b;
else {
a = -b;
a = (a < b) ? b : -b;
Both codes above check whether a is less than b or not. If a is less than b, value of b is
assigned to a if not, -b is assigned to a.
Consider a situation in which, only one block of code needs to be executed among many blocks.
This type of situation can be handled using nested if...else statement but, the better way of
handling this type of problem is using switch...case statement.
Syntax of switch
switch (n) {
case constant1:
break;
case constant2:
break;
.
default:
The value of n is either an integer or a character in above syntax. If the value of n matches constant
in case , the relevant codes are executed and control moves out of the switch statement. If
the ndoesn't matches any of the constant in case, then the default statement is executed and control
moves out of switch statement.
#include <iostream>
using namespace std;
int main() {
char o;
float num1,num2;
cout<<"Select an operator either + or - or * or / \n";
cin>>o;
cout<<"Enter two operands: ";
cin>>num1>>num2;
switch(o) {
case '+':
cout<<num1<<" + "<<num2<<" = "<<num1+num2;
break;
case '-':
cout<<num1<<" - "<<num2<<" = "<<num1-num2;
break;
case '*':
cout<<num1<<" * "<<num2<<" = "<<num1*num2;
break;
case '/':
cout<<num1<<" / "<<num2<<" = "<<num1/num2;
break;
default:
return 0;
}
Output
4.5
The break statement at the end of each case cause switch statement to exit. If break statement is
not used, all statements below that case statement are also executed.
for Loop
while Loop
do...while Loop
code/s to be executed;
int main() {
int i, n, factorial = 1;
Factorial of 5 = 120
In the above program you can see that, variable i is not used outside for loop. In such
9case, it is better to define variable at the time of initialization statement.
... .. ...
In the above C++ code, i is local to for loop, that is, you cannot use it outside for loop but
makes program more readable.
There are two statements ( break; and continue; ) built in C++ programming to alter the
normal flow of program.
Loops are used to perform repetitive task until test expression is false but sometimes it is
desirable to skip some statement/s inside loop or terminate the loop immediately with
checking test condition. On these type of scenarios, continue; statement
and break; statement is used respectively. The break; statement is also used to terminate
switch statement.
Syntax of break
break;
In real practice, break statement is almost always used inside the body of conditional
statement(if...else) inside the loop.
#include <iostream>
using namespace std;
int main() {
float number, sum = 0.0;
if (number != 0.0) {
sum += number;
}
else {
break; // terminating the loop if number equals to 0.0
}
}
cout<<"Sum = "<<sum;
return 0;
}
Output
Enter a number: 4
Enter a number: 0
Sum = 9.6
In this C++ program, the test expression is always true. The user is asked to enter a
number which is stored in variable number. If the user enters any number other than 0, that
number is added to sumand stored to it and again user is asked to enter another number.
When user enters 0, the test expression inside if statement is false and body of else is
executed which terminates the loop. Finally, the sum is displayed.
Syntax of continue
continue;
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; ++i) {
if ( i == 6 || i == 9) {
continue;
}
cout<<i<<"\t";
}
return 0;
}
Output
1 2 3 4 5 7 8 10
In C++ programming, goto statement is used for altering the normal sequence of program execution
by transferring control to some other part of the program.
... .. ...
... .. ...
... .. ...
label:
statement;
... .. ...
In syntax above, label is an identifier. When goto label; is encountered, the control of program
jumps to label: and executes the code below it.
# include <iostream>
using namespace std;
int main() {
float num, average, sum = 0.0;
int i, n;
cout<<"Maximum number of inputs: ";
cin>>n;
jump:
average=sum/(i-1);
cout<<"\nAverage = "<<average;
return 0;
}
Output
Average = 3.95
You can write any C++ program with use of goto statement and it is generally considered good idea
not to use goto statement.
The goto statement can be replaced in most of C++ program with the use of break and continue
statements.