Vous êtes sur la page 1sur 39

,

-4

: . //


.
.
www.emust.edu.mn

.
, ?
. , ;
, , ;
, ..
.

. C++- .
Ifelse . ( else -
.) 2 switch
.

. .

IF

IF . Ifdemo
.

//ifdemo.cpp
#include <iostream.h>

void main()
{
int x;
cout << : ;
cin >> x;
if (x>100) cout << 100- \n
}

If .
If While .
If , While -
.
.

: 2000
100-

100- .

IF

() .
//if2.cpp
#include <iostream.h>
void main() :
{
int x; : 12345
cout << : ; 12345 100-
cin >> x;
if (x>100)
{
cout << x << ;
cout << 100- \n
}
}


. .. , ,
, .
prime- .
.( 1
.)

//prime.cpp
#include <iostream.h>
#include <process.h>
void main()
{
unsigned long n, j;
cout << : ;
cin >> n;
for (j=2; j<=n/2; j++)
if (n%j==0)
{
cout << << j
<< - . << endl;
exit(0);
}
cout << \n; }

N , 2- N/2 .
j . j N
. 0-
.
. :

: 13

: 22229

: 22231
11- .

(
.)
for {} . If
.

Exit ()


. exit
.
.
. 0
. ( (*.bat)
errorlevel .)

IF ELSE

if
.
. ,
ifelse .

if () { } else { }

.

ifelse.cpp .
//ifelse.cpp
#include <iostream.h>

void main()
{
int x;
cout << : ;
cin >> x;
if (x>100)
cout << 100- \n;
else
cout << 100- \n;
}
,
.

GETCHE ()

while ifelse .
getche . Chcount
.

//chcount.cpp
#include <iostream.h>
#include <conio.h>
void main()
{
int chcount=0;
int wdcount=1;
char ch=a;
while (ch!=\r) {
ch=getche();
if (ch= )
wdcount++;
else
chcount++; }
cout << \n= << wdcount << endl
<< = << chcount << endl; }

CIN .
ENTER
. .. , ENTER
.
. getche() .
. ,
conio.h . getche
ch . ( getch()
getche()
.)
space ( ) wdcount
, chcount
. ..
. .

:
This is an example
=4
=15

ENTER getche() \r
.


//chcnt2.cpp
#include <iostream.h>
#include <conio.h>
void main() {
int chcount=0;
int wdcount=1;
char ch;
while ((ch=getche())!=\r)
{
if (ch= )
wdcount++;
else
chcount++;
}
cout << \n= << wdcount << endl
<< = << chcount << endl; }


. getche() ch while
.
. .. getche() a
ch=getche() a .
\r- .
.

x=y=z=0
Z 0 . 0 Y 0 ,
X 0 .
(ch =
getche() ) .
ch .
chcnt2 while
. .

ifelse

adifelse .

//adifelse.cpp
#include <iostream.h>
#include <conio.h>

void main()
{
char dir=a;
int x=10, y=10;
cout << Enter \n
while (dir!=\r)
{
cout << : << x <<, << y;
cout << \n (n, s, e, w):;

dir=getche();
if (dir==n)
y--;
else
if (dir==s)
y++;
else
if (dir==e)
x++;
else
if (dir==w)
x--;
}
}

. 10, 10

. ENTER .
,
. ,
. .

: 10,10
(n, s, e, w): n
: 10,9
(n, s, e, w): e
: 11,9
(n, s, e, w):


.
.

.
.
.
.

ifelse .
.

//badelse.cpp
#include <iostream.h>

void main()
{
int a, b, c;
cout << a, b, c :;
cin >> a >> b >> c;
if (a==b)
if (b==c)
cout << \n;
else
cout << a, b ;
}

a, b, c- . .
else a, b .
. else- if-
if- .
Ifelse- ,
. if else- if- else-
. .

if (a==b)
if (b==c)
cout << a, b, c ;
else
cout << b, c ;

else
cout << a, b ;

else
.

if (a==b)
{
if (b==c)
cout << a, b, c ;
}
else
cout << a, b ;

SWITCH

,
switch .
.
.

//platters.cpp
#include <iostream.h>
void main() {
int speed;
cout << \n33, 45, 78- : ;
cin >> speed;
switch(speed)
{
case 33:
cout << \n;
break;
case 45:
cout << \n;
break;
case 78:
cout << \n;
break; } }

Switch- . Case

. Switch
case- break
( ) .
:

33, 45, 78- : 33




Break

case break .
( !) .
,
case .
break .

DEFAULT

//adswitch.cpp
#include <iostream.h>
#include <conio.h>

void main()
{
char dir=a;
int x=10, y=10;

while (dir!=\r)
{
cout << << x <<, << y;
cout << \n (n, s, e, w):;
dir=getche();
switch(dir)

{
case n : y--; break;
case s : y++; break;
case e : x++; break;
case w : x--; break;
case \r : cout \n; break;
default : cout \n;
break;
}
}
}

Adswitch switch Default
. case-
default
. Try again .
break .

Switch Ifelse

switch , ifelse ?
.

If (SteamPressure*Factor>56)
.....
else if (VoltageIn+VoltageOut<23000)
.....
else if (day==Thursday)
.....

ifelse .
switch-
. (a<3) case (a<3)
ifelse .
case (a+32) switch .

switch
.


Case-
3


, .
.

If (alpha<beta)
Min=alpha;
Else
Min=beta;

C++- .
(+, - ..) 2 .

Min=(Alpha<Beta) ? alpha:beta

? : .
, ( : )
.

.
. min .
.

Absvalue=(n<0) ? n:n;

8 ( * ) condi.cpp .

//condi.cpp
#include <iostream.h>

void main()
{
for (int j=0; j<80; j++)
{
char ch=(j%8) ? :* ;
cout << ch;
}
}

j- 8 , 8- 0
() , 0- () .
8- ,
. .

cout << ((j%8) ? :*);


.
.

1. ,
2. , 2- , , ,
, ,
3. U.CS101
4. he2must.blogspot.com

Vous aimerez peut-être aussi

  • Lecture 12
    Lecture 12
    Document34 pages
    Lecture 12
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 9
    Lecture 9
    Document46 pages
    Lecture 9
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lec1-1 Corporate Activity Shine
    Lec1-1 Corporate Activity Shine
    Document61 pages
    Lec1-1 Corporate Activity Shine
    Amarsaikhan Tuvshinbayar
    0% (1)
  • Lecture 15
    Lecture 15
    Document40 pages
    Lecture 15
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 13
    Lecture 13
    Document46 pages
    Lecture 13
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 11
    Lecture 11
    Document24 pages
    Lecture 11
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 8 2018
    Lecture 8 2018
    Document54 pages
    Lecture 8 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • Lecture 7 20108
    Lecture 7 20108
    Document47 pages
    Lecture 7 20108
    Amarsaikhan Tuvshinbayar
    75% (4)
  • Lecture - 10
    Lecture - 10
    Document35 pages
    Lecture - 10
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 4 2018
    Lecture 4 2018
    Document47 pages
    Lecture 4 2018
    Amarsaikhan Tuvshinbayar
    100% (4)
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 3 2018
    Lecture 3 2018
    Document42 pages
    Lecture 3 2018
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 2 2018
    Lecture 2 2018
    Document33 pages
    Lecture 2 2018
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Baikal Hotolbor
    Baikal Hotolbor
    Document2 pages
    Baikal Hotolbor
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 5 2018
    Lecture 5 2018
    Document54 pages
    Lecture 5 2018
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 6 2018
    Lecture 6 2018
    Document50 pages
    Lecture 6 2018
    Amarsaikhan Tuvshinbayar
    100% (3)
  • IT101 Lecture 4
    IT101 Lecture 4
    Document72 pages
    IT101 Lecture 4
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 1 2018
    Lecture 1 2018
    Document39 pages
    Lecture 1 2018
    Amarsaikhan Tuvshinbayar
    100% (6)
  • Lecture 1
    Lecture 1
    Document54 pages
    Lecture 1
    Amarsaikhan Tuvshinbayar
    100% (2)
  • Lecture 2
    Lecture 2
    Document80 pages
    Lecture 2
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Lecture 3
    Lecture 3
    Document72 pages
    Lecture 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 8
    Programming Lec 8
    Document32 pages
    Programming Lec 8
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Programming Lec 9
    Programming Lec 9
    Document39 pages
    Programming Lec 9
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 6
    Programming Lec 6
    Document51 pages
    Programming Lec 6
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Programming Lec 7
    Programming Lec 7
    Document57 pages
    Programming Lec 7
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Koosen 2018 Algorithm 2
    Koosen 2018 Algorithm 2
    Document48 pages
    Koosen 2018 Algorithm 2
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • PL - 3
    PL - 3
    Document47 pages
    PL - 3
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation
  • Koosen 2018 Algorithm Programming 3
    Koosen 2018 Algorithm Programming 3
    Document46 pages
    Koosen 2018 Algorithm Programming 3
    Amarsaikhan Tuvshinbayar
    100% (1)
  • Programming Lec 4
    Programming Lec 4
    Document39 pages
    Programming Lec 4
    Amarsaikhan Tuvshinbayar
    Pas encore d'évaluation