You are on page 1of 8

University of Technology ‫الجامعة التكنولوجية‬

Department of Communication ‫قسم هندسة األتصاالت‬


Engineering

Report Title : Keywords in C++ Language


( Switch ) .

Course Name : Computer I ‫ برمجة الحاسوب‬: ‫اسم المادة‬


Programming I

Student’s Name : Mousa Saad Luaibi ‫ موسى سعد لعيبي فهد‬: ‫اسم الطالب‬
Fahad

‫ صباحي‬: ‫نوع الدراسة‬

Stage : First ‫ االولى‬: ‫المرحلة‬

2020-2019 : ‫السنة الدراسية‬


Contents

Cover ………...…………………………………………………… 1

Contents ……….....……………………………………………….. 2

Objectives ……………………………...…………………………. 3

Introduction ………………………………………...…………….. 3

Switch statement ………...……………………………………….. 3

Programs ……...……………………………………...…………... 5

Summary ……..…………………………………………....…....... 7

References of the report …………………………………...……... 8


Objectives

❖ We will learn about "switch" and know its properties, advantages and uses of it .
❖ Knowing how to formulate the logical statement for "switch" and how does the program
implement it ?
❖ Writing simple programs to implement what we learned during the report.
❖ We will write a helpful summary for "switch" and what we learned during the report.

Introduction

"Switch" is one of the most important keywords in the C ++ language and the second
statement of conditional statements, also it is synonymous or alternative to the "if" statement
in terms of purpose, but it differs from it in logical statement text and the steps to implement
the statement, it used to implement a specific statement when the required condition is
achieved One of the most important advantages of this statement is the ease of drafting the
logical statement and not setting more than a conditional possibility, as this is a special
feature of the "switch" statement that is distinguished from the "if" statement ...
In this report, we will try to explain the mechanism of the "switch", what are its most
important features, and how to write its logical statement and write some simple programs
regarding this.

Switch Statement

"Switch" is one of the most important statements of the condition, i.e. (that implements a
specific statement when a condition or set of conditions is set by the programmer during
programming a particular program), The most important characteristics that it has "switch" :

1. It only deals with variables of type "integer" and "letter" and does not deal with other
variables .

2. The condition cannot take more than one possibility! ... that this means that we must not
place more than one possibility (< , > , != , || , && , + , - , / , * ... etc) because the value of
the variable that the user will enter must be equal to a value specified by the programmer
and the condition must be an integral constant value and it is not correct to use variables for
example (x +1) .

3. The "switch" statement should start and end with the group bracket "{ }", unlike some "if"
statements .
General formula for a statement "switch" :

switch (n)(*)
{
case 1:
statement;
break;

case 2:
statement;
break;
.
.
.
case n:
statement;
break;

default:
statement;
}(*)

Figure : 1
How it works ( switch ) in the figure above :
After the user enters the entry, the program will examine the entry, does it fulfill one of the
conditions or not ?
As he will check the entry with the first case, if he checks, his statement is executed, and if
not, he will check it with the second issue, and so on. And when one of the conditions is
fulfilled, he will execute his statement and complete the implementation of the rest of the
programmatic lines of the program, and if one of the conditions is not met, the default status
statement will be implemented and the implementation will be completed The rest of the
lines of code .

Notes :
❖ The phrase "break" is used for the safe and proper exit of the "switch" statement. Care
must always be taken to append the end of each case with this phrase, because "switch"
even when you examine the entry and see it achieve a specific case will implement the
next case statement as long as we do not finish the first case with the phrase "break".

❖ The "default" case is not an antithesis or an exceptional case, but rather a hypothetical
case that the program implements in two situations :
1. When the entry does not achieve any case in "switch".
2. When the "switch" statements is not appended with the word "break" even if one of
the cases is realized and this means that the program will execute its statement even if
you fulfill the condition .

Programs

1. We will write a program that prints the name of the planet according to its distance from
Earth from the closest to farthest .

#include <iostream>
using namespace std;
int main()
{ int x;
cout<< " Enter number a planet "<< endl;
cin>> x;
switch (x)
{case 1: cout<<" its a Mercury"<<endl; break;
case 2: cout<<" its a Venus"<<endl; break;
case 3: cout<<" its a earth"<<endl; break;
case 4: cout<<" its a Mars"<<endl; break;
case 5: cout<<" its a Jupiter"<<endl; break;
case 6: cout<<" its a Saturn"<<endl; break;
case 7: cout<<" its a URANUS"<<endl; break;
case 8: cout<<" its a NEPTUNE"<<endl; break;
case 9: cout<<" its a PLUTO"<<endl; break;
case 10: cout<<" its a senda "<<endl; break;
default:
cout<< " Error no planet to resemble this number";
break; }
return 0; }

Output :
Enter number a planet
7
its a URANUS

2. We will write a program that converts Arabic numbers to the corresponding numbers in
Latin .

#include<iostream>
using namespace std;
int main()
{ int x;
cout<<" Enter the decimal number : ";
cin>> x;
switch (x)
{ case 1: cout<<" I"<<endl; break;
case 2: cout<<" II"<<endl; break;
case 3: cout<<" III"<<endl; break;
case 4: cout<<" IV"<<endl; break;
case 5: cout<<" V"<<endl; break;
case 6: cout<<" VI"<<endl; break;
case 7: cout<<" VII"<<endl; break;
case 8: cout<<" VIII"<<endl; break;
case 9: cout<<" IX"<<endl; break;
case 10: cout<<" XI"<<endl; break;
case 11: cout<<" XII"<<endl; break;
case 12: cout<<" XIII"<<endl; break;
default: cout<<" Invalid"; break; }
return 0; }

Output :
Enter the decimal number : 7
VII
Summary

❖ It is considered one of the most important keywords in the C ++ language and the second
conditional statement, as it closely resembles the "if" statement in terms of purpose, but
differs from it in terms of the mechanism of work and the logical statement.

❖ It has the advantage of dealing with integer and letters only, and the variables in it must
be fixed and complete, free of operations ‫ش‬nd its cases should start and end with the
group's bow .

❖ General formula for “switch” :

switch (n)
{ case 1: statement; break;
case 2: statement; break;
case n: statement; break;
default: statement; }

❖ A scheme showing the "Switch" working mechanism :

❖ “Switch” checks the entry in the order and executes the statement of the case that fulfills
the condition. If no condition is met, it implements a default statement.

❖ It is necessary to end each statement with the phrase "break" because if the statement
does not end with this word, the statement of the next case will be executed even if the
condition in the previous case is fulfilled.

❖ “defualt” statement is not exceptional or contradictory, but a general case that is


implemented when the condition has not been fulfilled with any case that precedes it,
knowing that it is implemented even if you fulfill the condition in the case that precedes
it, but that is when the case is not clarified with the phrase “break”.
References of the report

1. C++ Primer , 5th , By : Stanley B. Lippman with Josee Lajoie and Barbara E.
Moo , The pages from 241 to 247 .

2. Programming Principles and Practice Using C++ , 2th , By : Bjarne Stroustrup ,


The pages from 115 to 118 .

3. Elixir In C++ Language , 2005 edition , By : Sultan Mohammed Al-Thebety , The


pages from 31 to 34 , It has been translated into English .

4. Establish yourself in programming using C++ Language , 1th , By : Ismaail Al-


Shahili with Hani Abdullrahman , The pages from 17 to 19 , It has been
translated into English .

5. Object-oriented programming in C++ , 4th , By : Robert Lafore , The pages from


107 to 111 .

6. Introduction to Programming With c++ , 3th , By : Mohit P. Tahiliani & Nitk


Surathka & Y. Daniel Liang , The pages from 117 to 121 .

7. C++ Programming : from problem analysis to program design , 5th , By: D.S
Malik , The pages from 215 to 223 .

You might also like