0% found this document useful (0 votes)
1 views8 pages

CS Chapter3 2024

Uploaded by

takisamai058
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views8 pages

CS Chapter3 2024

Uploaded by

takisamai058
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

University of Batna 2 Module: Computer Science 1

Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree


Department of Common Core Studies

Chapter 3: Conditional statements

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;
}

If boolean_expression evaluates to true, then statement is executed.

If boolean_expression evaluates to false, then statement is skipped.

Note that the boolean_expression enclosed in parentheses must evaluate to true or


false.

a. the if statement flowchart:

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.

Dr. Beloucif Assia 1


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

false
boolean_expression

true
statement

Figure 1. the if statement flowchart

b. A classic error

To perform an equality test, you must use the symbol = twice.


For example:

if(a==b)

A common mistake is to write:

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;
}

Dr. Beloucif Assia 2


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

Execution trace of example entering 7 as a value of the variable n:

Memory Screen
please enter an integer number: 7
End of the program!

Execution trace of example entering 14 as a value of the variable

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..

For example, we cannot write the constraint 0<x<200 directly as a condition in if


statement, otherwise we can write it using logical operators as: x > 0 and x < 200.

3. Alternative conditional Instructions:


If we want to choose between two alternatives, we use the if/else statement.

Syntax:
if (boolean_expression) {
statement1
}else{
statement2
}

Dr. Beloucif Assia 3


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

If boolean_expression evaluates to true, then statement1 is executed.

If boolean_expression evaluates to false, then statement2 is executed.

The if else flowchart:

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".

Figure 2. the if else statement flowchart


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{
cout<<“Negative number"<<endl;
}
return 0;
}

Dr. Beloucif Assia 4


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

Execution trace of example entering +3 as a value of the variable n

Memory Screen

Please print an integer: +3


Positive number

Execution trace of example entering -10 as a value of the variable n

Memory Screen

Please print an integer: +3


Positive number

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;
}

Dr. Beloucif Assia 5


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

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;
}

5. The selective statement (switch statement)


A switch statement allows a variable to be tested for equality against a list of values. Each
value is called a case, and the variable being switched on is checked for each case switch
statement objective is to check several possible constant values for an expression.

Syntax:
switch(identifier)
{
case c1:statement1;break;
case c2:statement2;break;
case c3:statement3;break;
...
default: statement;break;
}

Dr. Beloucif Assia 6


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

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;
}

Dr. Beloucif Assia 7


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

How This Program Works

• 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

Dr. Beloucif Assia 8

You might also like