Programming Fundamentals 06
Programming Fundamentals 06
PROGRAMMING
OBJECTIVE
S
In this chapter, you will:
• Learn about control structures
• Discover how to use the selection control
structures
• if,
• if...else,
• if-else-if,
• and switch in a program
2
CONTROL
STRUCTURES
• A computer can proceed:
– In sequence: Simple Program
3
CONTROL STRUCTURES
(CONTINUED)
4
SELECTION: IF AND IF...ELSE
• One-Way Selection
• Two-Way Selection
• Compound (Block of) Statements
• Multiple Selections: Nested if
• Comparing if...else Statements with
a Series of if Statements
ONE-WAY SELECTION
#include <iostream>
int main() {
int number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
return 0;
}
OUTPUT-1 & 2
• Enter an integer: 5
• You entered a positive number: 5
• This statement is always executed.
• When the user enters 5, the condition number > 0 is evaluated to true and the statement inside the
body of if is executed.
• Enter a number: -5
• This statement is always executed.
• When the user enters -5, the condition number > 0 is evaluated to false and the statement inside the
body of if is not executed.
TWO-WAY SELECTION
• Two-way selection takes the form:
• The if statement can have an optional else clause. Its syntax is:
if (condition)
{
// block of code if condition is true
}
else {
// block of code if condition is false
}
The if..else statement evaluates the condition inside the parenthesis
HOW IF…ELSE STATEMENT WORKS
HOW IF…ELSE STATEMENT WORKS
#include <iostream>
int main() {
int number;
cout << "Enter an integer: ";
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
else {
cout << "You entered a negative integer: " << number << endl;
}
return 0;
}
OUTPUT 1& 2
• Enter an integer: 4
• You entered a positive integer: 4
• In the above program, we have the condition number >= 0. If we enter
the number greater or equal to 0, then the condition evaluates true.
• Here, we enter 4. So, the condition is true. Hence, the statement inside the
body of if is executed.
• Enter an integer: -4
• You entered a negative integer: -4.
• Here, we enter -4. So, the condition is false. Hence, the statement inside
the body of else is executed.
C++ IF...ELSE...ELSE IF STATEMENT
• The if...else statement is used to execute a block of code among two alternatives. However, if we need
to make a choice between more than two alternatives, we use the if...else if...else statement.
if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else{
// code block 3
}
CONT…
• Use the switch statement to select one of many code blocks to be executed.
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
BREAK KEYWORD
• When C++ reaches a break keyword, it breaks out of the switch block.
• This will stop the execution of more code and case testing inside the block.
• When a match is found, and the job is done, it's time for a break. There is no need for
more testing.
SWITCH EXAMPLE
NESTED IF & IF/ELSE STATEMENTS IN C++
if (mark == 100) {
else {
}
NESTED IF ELSE EXAMPLE