الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

case and switch

مغلق
بدأه Rachid Abdallah في 2 نوفمبر 2004 · 3 رد · 469 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

how can we use the case and switch keywords in C i have'nt read anything about them before and i was hoping that someone would help me here

==================================

#2
#include <stdio.h>
#include <conio.h>
main()
{
int m;
scanf("%d",m);
switch(m)
{
case '5':
printf("5");
break;
case '6':
printf("6");
break;
}
}
لا اله الا الله
#3
switch
Test variable for multiple values
Series of case labels and optional default case
	switch ( variable ) {	case value1:        // taken if variable == value1
  statements
  break;             // necessary to exit switch
   
  case value2:
  case value3:    // taken if variable == value2 or == value3
  statements
  break;

  default:        // taken if variable matches no other cases
  statements  break;
	}
================================================
Example upcoming
Program to read grades (A-F)
Display number of each grade entered
Details about characters
Single characters typically stored in a char data type
char a 1-byte integer, so chars can be stored as ints
Can treat character as int or char
97 is the numerical representation of lowercase ‘a’ (ASCII)
Use single quotes to get numerical representation of character
cout << "The character (" << 'a' << ") has the value "      << static_cast< int > ( 'a' ) << endl; 
Prints
The character (a) has the value 97 
================================================
1      // Fig. 2.22: fig02_22.cpp
2      // Counting letter grades.
3      #include <iostream>
4      
5      using std::cout;
6      using std::cin;
7      using std::endl;
8      
9      // function main begins program execution
10    int main()
11    {
12       int grade;       // one grade
13       int aCount = 0;  // number of As
14       int bCount = 0;  // number of Bs
15       int cCount = 0;  // number of Cs
16       int dCount = 0;  // number of Ds
17       int fCount = 0;  // number of Fs
18    
19       cout << "Enter the letter grades." << endl
20            << "Enter the EOF character to end input." << endl;
21    
22       // loop until user types end-of-file key sequence
23       while ( ( grade = cin.get() ) != EOF ) {
24    
25          // determine which grade was input
26          switch ( grade ) {  // switch structure nested in while
27    
28             case 'A':        // grade was uppercase A
29             case 'a':        // or lowercase a
30                ++aCount;     // increment aCount
31                break;        // necessary to exit switch
32    
33             case 'B':        // grade was uppercase B
34             case 'b':        // or lowercase b
35                ++bCount;     // increment bCount    
36                break;        // exit switch
37    
38             case 'C':        // grade was uppercase C
39             case 'c':        // or lowercase c
40                ++cCount;     // increment cCount    
41                break;        // exit switch
42    
43             case 'D':        // grade was uppercase D
44             case 'd':        // or lowercase d
45                ++dCount;     // increment dCount    
46                break;        // exit switch
47    
48             case 'F':        // grade was uppercase F
49             case 'f':        // or lowercase f
50                ++fCount;     // increment fCount    
51                break;        // exit switch
52    
53             case '\n':       // ignore newlines,  
54             case '\t':       // tabs, 
55             case ' ':        // and spaces in input
56                break;        // exit switch
57    
58             default:         // catch all other characters
59                cout << "Incorrect letter grade entered."
60                     << " Enter a new grade." << endl;
61                break;        // optional; will exit switch anyway
62    
63          } // end switch
64    
65       } // end while
66    
67       // output summary of results
68       cout << "\n\nTotals for each letter grade are:" 
69            << "\nA: " << aCount   // display number of A grades
70            << "\nB: " << bCount   // display number of B grades
71            << "\nC: " << cCount   // display number of C grades 
72            << "\nD: " << dCount   // display number of D grades
73            << "\nF: " << fCount   // display number of F grades
74            << endl;
75    
76       return 0;  // indicate successful termination
77    
78    } // end function main

================

Enter the letter grades.

Enter the EOF character to end input.

a

B

c

C

A

d

f

C

E

Incorrect letter grade entered. Enter a new grade.

D

A

b

^Z

Totals for each letter grade are:

A: 3

B: 2

C: 3

D: 2

F: 1

تم تعديل هذه المشاركة بواسطة walcom في 5 نوفمبر 2004 في 07:47

12.gif

cs.jpg

#4

thanks

now i learned how to use SWITCH and CASE

this is one step forward to proffesionalism

==================================

هذا الموضوع مغلق.

مواضيع مشابهة