Professional Documents
Culture Documents
1) Instruksionet ciklike
Instruksionet e kaprcimit
break
continue
return
Sintaksa:
Instruksioni while
while(<kusht>) {
instruksion1;
.....................
}
Semantika:
Kur programi arrin fjaln els while,
kontrollohet kushti.
Nse kushti sht i vrtet ekzekutohet dhe
vazhdon t ekzekutohet trupi i ciklit, derisa
kushti t bhet false.
Nj instruksion i trupit t ciklit ndryshon
variablin e ciklit.
Kur kushti bhet false, trupi i ciklit kaprcehet
dhe kalohet n instruksionin pasues.
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
int n; // numri i notave
int nr_note; //variabli i ciklit
double note; // notat
double Sh = 0, nmes;
cout << "Sa nota? " << endl;
cin >> n;
nr_note = 1;
while (nr_note <= n) {
cin>>note; //lexon nje note
Sh = Sh + note;//shuma e notave
nr_note = nr_note + 1; //variabli i ciklit + 1
}
nmes = Sh/n;
cout<<"nmes ="<<nmes<<endl;
system("PAUSE");
return 0; }
int num = 0;
while (num < 100) {
cout<<num= << num; // prsritet pa fund
num = num * 1;
} //OOPS! Variabli ciklik mbetet 0
ose
int num = 0;
while (num < 100) {
cout<<num= << num; // prsritet pa fund
} //OOPS ! Variabli ciklik harrohet t ndryshohet
Kujtojm q n C++ nj
vler
integer
=
0
konsiderohet si vler FALSE
ne kushtin e while dhe
TRUE ne rastet e tjera.
S=
OUTPUT:
Nga rezultati shihet se ksaj here numri i karaktereve sht 10, pasi num
rritet hern e fundit q cikli prsritet e pastaj testohet kushti false se shtypet
Return.
Kaprcimi i strukturuar
Instruksionet q lejojn ndryshimin e rregullt t fluksit t
instruksioneve dhe n rastin e cikleve jan:
break : provokon daljen e menjhershme nga cikli pr
t vazhduar n instruksionin q pason ciklin.
while ( kusht ) {
instruksione;
...continue;
instruksione
}
Instruksioni break
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
char c;
while (1) { // kusht gjithnj true
cout << Jepni nj karakter!" << endl;
cin >> c;
if (c != 'a')
cout << c << endl;
else
break; // dilet nga cikli kur shtypet a
}
system("PAUSE");
return 0;
}
#include <cstdlib>
#include <iostream>
using namespace std;
int main(){
int vl, s = 0;
cout<<"Jepni vlerat qe do te mblidhni: "<<endl;
while (cin >> vl) { // kusht i ciklit sht nj vler int q lexohet me cin
if (vl == 0)
{
cout<<"Shtypet 0, Dolet nga cikli."<<endl;
break; // dilet nga cikli kur shtypet numri 0
}
s = s + vl;
} // mbyllet cikli
cout<<"s="<<s<<endl;
system("PAUSE");
return 0;
}
Instruksioni continue
#include <cstdlib>
#include <iostream>
using namespace std;
int main(){
int x=0 ;
while(x <10){
++x;
if (x == 7) continue;
cout<<"x=" << x<<endl;
}
system("PAUSE");
return 0;
}
Llogaritje e faktorialit
#include <cstdlib>
#include <cstdlib>
Llogaritja e mesatares pr nr lnd pr 20 student.
#include <iostream>
using namespace std;
int main() {
int n; // numri i notave
int nr_note; //variabli i ciklit
double note; // notat
double Sh, nmes;
cout << "Sa nota? " << endl;
cin >> n;
int nr_stud =1;
while(nr_stud <= 3) {
Sh = 0;
nr_note = 1;
while (nr_note <= n) {
cin>>note;
//lexon nje note
Sh = Sh + note; //shuma e notave
++nr_note;// ndryshon variabli i ciklit
}
nmes = Sh/n;
cout<<"nmes = "<<nmes<<endl;
++nr_stud;
}
system("PAUSE");
return 0; }