COMPUTER
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
– Selectively (branch) - making a choice: program
executes particular statements depending on some condition.
– Repetitively (iteratively) – looping: the program repeats
particular statements a certain numbers of times based on some
conditions.
• Some statements are executed only if certain conditions are met.
• A condition is met if it evaluates to true
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
• The syntax of one-way selection is:
• The statement is executed if the value of the
expression is true
• The statement is bypassed if the value is
false; program goes to the next statement
• if is a reserved word
ONE-WAY SELECTION
(CONTINUED)
C++ IF STATEMENT
• The if statement evaluates the condition inside the parentheses ( ).
• If the condition evaluates to true, the code inside the body of if is
executed.
• If the condition evaluates to false, the code inside the body of if is
skipped.
EXAMPLE 1: C++ IF STATEMENT
// Program to print positive number entered by the user
// If the user enters a negative number, it is skipped
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
// checks if the number is positive
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
cout << "This statement is always executed.";
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:
• If expression is true, statement1
is executed; otherwise, statement2
is executed
– statement1 and statement2 are any
C++ statements
• else is a reserved word
TWO-WAY SELECTION
(CONTINUED)
C++ IF...ELSE
• 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
• If the condition evaluates true
• the code inside the body of if is executed.
• the code inside the body of else is skipped from execution.
• If the condition evaluates false
• the code inside the body of else is executed
• the code inside the body of if is skipped from execution
EXAMPLE: C++ IF...ELSE STATEMENT
#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;
}
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.
• The syntax of the if...else if...else statement is:
if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else{
// code block 3
}
CONT…
• If condition1 evaluates to true, the code block 1 is executed.
• If condition1 evaluates to false, then condition2 is evaluated.
• If condition2 is true, the code block 2 is executed.
• If condition2 is false, the code block 3 is executed.
C++ SWITCH STATEMENTS
• 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++
• A nested if in C is an if statement that is the target of another if statement. Nested if
statements mean an if statement inside another if statement. Yes, both C and C++ allow us
to nested if statements within if statements, i.e, we can place an if statement inside
another if statement.
NESTED IF EXAMPLE
int mark = 100;
if (mark >= 50) {
cout << "You passed." << endl;
if (mark == 100) {
cout <<"Perfect!" << endl;
else {
cout << "You failed." << endl;
}
NESTED IF ELSE EXAMPLE