CS Chapter3 2024
CS Chapter3 2024
1. Introduction
A conditional instruction is an instruction which allows you to carry out a test (condition) and
to execute or not a group of instructions depending on the value of the test carried out. The
condition is a Boolean expression (which evaluates to either true or false).
There are 3 forms:
• simple conditional: if
• alternative conditional: if else
• multiple choice conditional: switch
2. Simple conditional if
If we have a code that we sometimes want to execute and sometimes we want to skip we
can use the if statement.
Syntax:
if (boolean_expression){
statement;
}
The if flowchart statement is a two-way system that executes two blocks of statements; it's a
kind of conditional flowchart.
If the condition inside the if block is true, the program executes all the statements within
that if block. However, if the condition within the if block is false, the program will not
execute this statement.
false
boolean_expression
true
statement
b. A classic error
if(a==b)
if(a=b)
Example:
#include<iostream>
using namespace std;
int main(){
int n;
cout<<“please enter an integer number:";
cin>> n;
if(n>10){
cout<<“The number entered is greater than 10 "<<endl;
}
cout<<“End of the program!";
return 0;
}
Memory Screen
please enter an integer number: 7
End of the program!
Screen
Memory
please enter an integer number: 14
The number entered is greater than
10
End of the program!
c. Complex conditions
It is often necessary to write fairly complicated conditions. You will need to use logical and,
logical or, and logical not..
Syntax:
if (boolean_expression) {
statement1
}else{
statement2
}
If the condition inside the if block is true, the program executes all the statements within the
block statement1. However, if the condition within the if block is false, the program will
execute the Else block's statement (statement 2). If quoted in simple words, the flowchart if
Then Else statement demands that "Do the task if a condition is true; otherwise, then do
another task".
Memory Screen
Memory Screen
4. Multiple Alternatives
A test with if therefore opens two paths, corresponding to two different treatments. But in
some cases, these two paths are not enough for all possible solutions.
Syntax:
if (boolean_expression1) {
statement1;
}else if (boolean_expression 2) {
statement2;
}
}else {
statement3;
}
Example:
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Please print an integer:";
cin>> n;
if(n>0){
cout<<"Positive number"<<endl;
} else if(n==0) {
cout<<"Zero number"<<endl;
}else{
cout<<"Negative number"<<endl;
}
return 0;
}
Syntax:
switch(identifier)
{
case c1:statement1;break;
case c2:statement2;break;
case c3:statement3;break;
...
default: statement;break;
}
Example:
#include <iostream>
using namespace std;
int main( ){
int a, b, x;
cout << "Enter two integer numbers \n";
cin >> a >> b;
cout << "1 for addition \n";
cout << "2 for subtraction \n";
cout << "3 for multiplication \n";
cout << "4 for division \n";
cout << "enter your choice \n";
cin >> x;
switch ( x ){
case 1: cout << a + b;
break;
case 2: cout << a - b;
break;
case 3: cout << a * b;
break;
case 4: cout << a / b;
break;
default: cout << "Error! The operator is not correct"
break;}
return 0;
}
• We first prompt the user to enter two numbers, which are stored in the integer
variables a and b.
• We then write on the screenthe number of each of the fourth operations, and then
we prompt the user to enter the desired number which means desired operation.
This input is then stored in the integer variable named x.
• The switch statement is then used to check the number entered by the user:
• If the user enters 1, addition is performed on the numbers.
• If the user enters 2, subtraction is performed on the numbers.
• If the user enters 3, multiplication is performed on the numbers.
• If the user enters 4, division is performed on the numbers.
• If the user enters any other number, the default code is printed.
• Notice that the break statement is used inside each case block. This terminates the
switch statement.
• If the break statement is not used, all cases after the correct case are executed