C/C++语言 - 分支结构

Similar documents
C/C++ - 字符输入输出和字符确认

C/C++ - 函数

C/C++语言 - 运算符、表达式和语句

C/C++ 语言 - 循环

C/C++语言 - C/C++数据

C/C++ - 文件IO

CC213

C 语言 第七讲 分支 张晓平 武汉大学数学与统计学院 2017 年 4 月 5 日

新・解きながら学ぶC言語

C C

新・明解C言語入門編『索引』

C/C++程序设计 - 字符串与格式化输入/输出

新版 明解C言語入門編

C

新版 明解C++入門編

C 1

CC213

C

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

C/C++ - 字符串与字符串函数

Microsoft Word - 第3章.doc

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

新・解きながら学ぶJava

untitled

nooog

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

内 容 提 要 指 针 持 久 动 态 内 存 分 配 字 符 串 ( 字 符 数 组 ) 2

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

untitled

C/C++ - 结构体、共用体、枚举体

3.1 num = 3 ch = 'C' 2

Microsoft Word - CPE考生使用手冊 docx

C/C++ - 数组与指针

CHAPTER VC#

穨control.PDF

FY.DOC

CC213

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

untitled

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

WWW PHP Comments Literals Identifiers Keywords Variables Constants Data Types Operators & Expressions 2

untitled

Ps22Pdf

ebook14-4

1.ai

untitled

Computer Architecture

科学计算的语言-FORTRAN95

Microsoft Word - Final Exam Review Packet.docx

untitled

untitled

プログラムの設計と実現II

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

C

Microsoft Word - 01.DOC

Knowledge and its Place in Nature by Hilary Kornblith

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

epub 33-8

Microsoft PowerPoint - ds-1.ppt [兼容模式]

ebook39-5

C语言的应用.PDF

c_cpp

C++ 程式設計

PIC_SERVER (11) SMTP ( ) ( ) PIC_SERVER (10) SMTP PIC_SERVER (event driven) PIC_SERVER SMTP 1. E-

BC04 Module_antenna__ doc

第7章-并行计算.ppt

Gerotor Motors Series Dimensions A,B C T L L G1/2 M G1/ A 4 C H4 E

数据结构与算法 - Python基础

6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit

Microsoft Word - C-pgm-ws2010.doc

致 谢 本 论 文 能 得 以 完 成, 首 先 要 感 谢 我 的 导 师 胡 曙 中 教 授 正 是 他 的 悉 心 指 导 和 关 怀 下, 我 才 能 够 最 终 选 定 了 研 究 方 向, 确 定 了 论 文 题 目, 并 逐 步 深 化 了 对 研 究 课 题 的 认 识, 从 而 一

_汪_文前新ok[3.1].doc

, 7, Windows,,,, : ,,,, ;,, ( CIP) /,,. : ;, ( 21 ) ISBN : -. TP CIP ( 2005) 1

TX-NR3030_BAS_Cs_ indd

* RRB *

Microsoft Word - PHP7Ch01.docx

Guide to Install SATA Hard Disks

国防常识

Microsoft Word - template.doc

2009 Japanese First Language Written examination


untitled

2010 Japanese First Language Written examination

(Guangzhou) AIT Co, Ltd V 110V [ ]! 2

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

2009 Korean First Language Written examination

AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING

碩命題橫式

PowerPoint Presentation

技 巧 5: 避 免 除 以 0 的 運 算 在 做 除 的 運 算 時, 先 檢 查 除 數 的 數 值, 避 免 有 除 以 0 的 情 況 若 運 算 中 除 數 為 0,SAS 會 在 LOG 中 註 記 提 醒 並 將 運 算 結 果 設 定 為 遺 漏 值, 減 慢 程 式 的 執 行

Microsoft Word

2015 Chinese FL Written examination

条款

業 用 地 出 讓 最 低 價 標 準 不 得 低 於 土 地 取 得 成 本 土 地 前 期 開 發 成 本 和 按 規 定 收 取 的 相 關 費 用 之 和 工 業 用 地 必 須 採 用 招 標 拍 賣 掛 牌 方 式 出 讓 其 出 讓 價 格 不 得 低 於 公 佈 的 最 低 價 標

2015年计算机二级(C语言)模拟试题及答案(四)

3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不

星河33期.FIT)

項 訴 求 在 考 慮 到 整 體 的 財 政 承 擔 以 及 資 源 分 配 的 公 平 性 下, 政 府 採 取 了 較 簡 單 直 接 的 一 次 性 減 稅 和 增 加 免 稅 額 方 式, 以 回 應 中 產 家 庭 的 不 同 訴 求 ( 三 ) 取 消 外 傭 徵 費 6. 行 政 長

(f) (g) (h) (ii) (iii) (a) (b) (c) (d) 208

Transcription:

C/C++

Table of contents 1. if 2. if else 3. 4. 5. 6. continue break 7. switch 1

if

if i // colddays.c: # include <stdio.h> int main ( void ) { const int FREEZING = 0; float temperature ; int cold_ days = 0; int all_ days = 0; printf (" Enter the list of daily low temperature.\n"); printf (" Use Celsius, and enter q to quit.\n"); while ( scanf ("%f", & temperature ) ==1) { all_ days ++; if ( temperature < FREEZING ) 2

if ii } cold_ days ++; } if ( all_ days!= 0) printf ("%d days total : %.1 f%% below freezing.\n", all_days, 100.0*( float ) cold_days / all_days ); if ( all_ days == 0) printf ("No data entered.\n"); return 0; 3

if Enter the list of daily low temperature. Use Celsius, and enter q to quit. -10-5 0 12 5 6-4 8-2 15 q 10 days total : 40. 0% were below freezing. 4

if if if ( condition ) statement if ( condition ){ statements } condition statements if while if while 5

if condition condition 0 if ( score >= 60) printf (" Pass!\n"); if (a > b){ a ++; printf (" You } lose. b.\n"); 6

if else

if else if ( condition ) statement1 else statement2 7

if else if ( condition ) statement1 else statement2 condition statement1 condition statement2 7

if else if else // wrong structure if (x > 0) printf (" Incrementing x ++; else printf ("x <= 0\n"); x:\n"); 8

if else if else // wrong structure if (x > 0) printf (" Incrementing x ++; else printf ("x <= 0\n"); x:\n"); printf if x++; if else if 8

if else // right structure if (x > 0){ printf (" Incrementing x ++; } else printf ("x <= 0\n"); x:\n"); 9

if else if num > 10 no yes num *= 2; printf("%d\n", num); 10

if else if else num > 10 yes printf("%d\n", num); num *= 2; 11

getchar() putchar() getchar() ch = getchar () ; scanf ("%c", &ch); putchar() putchar (ch); printf ("%c", ch); 12

getchar() putchar() scanf() printf() stdio.h 13

getchar() putchar() 1 // cypher1.c 2 # include <stdio.h> 3 # define SPACE 4 int main ( void ) 5 { 6 char ch; 7 ch = getchar (); 8 while (ch!= \n ) { 9 if ( ch == SPACE ) 10 putchar (ch); 11 else 12 putchar (ch +1) ; 13 ch = getchar (); 14 } 15 putchar (ch); 16 return 0; 17 } 14

getchar() putchar() 1 // cypher1.c 2 # include <stdio.h> 3 # define SPACE 4 int main ( void ) 5 { 6 char ch; 7 ch = getchar (); 8 while (ch!= \n ) { 9 if ( ch == SPACE ) 10 putchar (ch); 11 else 12 putchar (ch +1) ; 13 ch = getchar (); 14 } 15 putchar (ch); 16 return 0; 17 } Hello Ifmmp World Xpsme 14

getchar() putchar() ch = getchar (); while (ch!= \n ) {... ch = getchar (); } while (( ch = getchar ())!= \n ) {... } 15

getchar() putchar() ch = getchar (); while (ch!= \n ) {... ch = getchar (); } while (( ch = getchar ())!= \n ) {... } C 15

getchar() putchar() while ( (ch = getchar ())!= \n ) {... } 16

getchar() putchar() ch ch = getchar()!= getchar() ch ch = getchar() ch ch 17

getchar() putchar() while ( ch = getchar ()!= \n ) {... } =!= getchar()!= \n 0 1 ch ch 0 1 getchar() 18

ctype.h // cypher2.c # include <stdio.h> # include <ctype.h> int main ( void ) { char ch; while (( ch = getchar ())!= \n ) { if ( isalpha (ch)) putchar (ch +1) ; else putchar (ch); } putchar (ch); return 0; } 19

ctype.h Look! It s a programmer! Mppl! Ju t b qsphsbnnfs! 20

ctype.h 1: isalnum isalpha isblank iscntrl isdigit isgraph ( ) Ctrl+B 21

ctype.h 2: islower isprint ispunct isspace isupper isxdigit 22

ctype.h 3: tolower toupper 23

ctype.h ch tolower (ch); ch ch = tolower (ch); 24

360kwh 320kwh 680kwh $0.12589/kwh $0.17901/kwh $0.20971/kwh 25

i // electric.c # include <stdio.h> # define RATE1 0. 12589 # define RATE2 0. 17901 # define RATE3 0. 20971 # define BREAK1 360. 0 # define BREAK2 680. 0 # define BASE1 ( RATE1 * BREAK1 ) # define BASE2 ( BASE1 + RATE2 * ( BREAK2 - BREAK1 ) ) int main ( void ) { double kwh, bill ; printf (" Please enter the kwh used.\n"); 26

ii } scanf ("%lf", & kwh ); if ( kwh <= BREAK1 ) bill = RATE1 * kwh ; else if ( kwh <= BREAK2 ) bill = BASE1 + RATE2 * ( kwh - BREAK1 ); else bill = BASE2 + RATE3 * ( kwh - BREAK2 ); printf (" The charge for %.1 f kwh is $ %.2 f.\n", kwh, bill ); return 0; 27

Please enter the kwh used. 580 The charge for 580. 0 kwh is $84. 70. 28

if else if kwh<=break1 else kwh<=break2 yes bill=rate1*kwh bill=base2+ RATE3* (kwh-break2); yes bill=base1+ RATE2* (kwh-break1); 29

if ( kwh <= BREAK1 ) bill = RATE1 * kwh ; else if ( kwh <= BREAK2 ) bill = BASE1 + RATE2 * ( kwh - BREAK1 ); else bill = BASE2 + RATE3 * ( kwh - BREAK2 ); if ( kwh <= BREAK1 ) bill = RATE1 * kwh ; else if ( kwh <= BREAK2 ) bill = BASE1 + RATE2 * ( kwh - BREAK1 ); else bill = BASE2 + RATE3 * ( kwh - BREAK2 ); 30

if else if else else 31

else if if ( score < 1000) bonus = 0; else if ( score < 1500) bonus = 1; else if ( score < 2000) bonus = 2; else if ( score < 2500) bonus = 3; else bonus = 4; C99 127 32

else if // elseif.c: # include <stdio.h> int main ( void ) { int number ; printf (" Enter an integer : "); scanf ("%d", & number ); if ( number > 6) if ( number < 12) printf (" You re close!\n"); else printf (" Sorry, you loose a turn!\n"); return 0; } 33

else if Enter an integer : 5 34

else if Enter an integer : 5 Enter an integer : 10 You re close! 34

else if Enter an integer : 5 Enter an integer : 10 You re close! Enter an integer : 15 Sorry, you loose a turn! 34

else if else if 35

else if if ( number > 6) if ( number < 12) printf (" You re close!\n"); else printf (" Sorry, you loose a turn!\n"); 36

else if else if if ( number > 6) { if ( number < 12) printf (" You re close!\n"); } else printf (" Sorry, you loose a turn!\n"); 37

38

i # include <stdio.h> # include < stdbool.h> int main ( void ) { unsigned long num, div ; bool isprime ; printf (" Enter an integer (q to quit ).\n"); while ( scanf ("%lu", & num ) == 1) { for ( div = 2, isprime = true ; div * div <= num ; div ++) { if ( num % div == 0) { if ( div * div!= num ) printf ("% lu is divisible by % lu and % lu.\n", num, div, num / div ); 39

ii } else printf ("%lu is divisible by %lu.\n", num, div ); isprime = false ; } } if ( isprime ) printf ("%lu is prime.\n", num ); printf (" Enter another integer ( q to quit ).\ n "); } printf (" Bye.\n"); return 0; 40

Enter an integer ( Enter q to quit ). 36 36 is divisible by 2 and 18. 36 is divisible by 3 and 12. 36 is divisible by 4 and 9. 36 is divisible by 6. Enter another integer ( Enter q to quit ). 149 149 is prime. Enter another integer ( Enter q to quit ). 30777 30777 is divisible by 3 and 10259. Enter another integer ( Enter q to quit ). q Bye. 41

if, else 42

if ( condition ) statement if ( condition ) statement1 else statement2 if ( condition1 ) statement1 else if ( condition2 ) statement2 else statement3 43

44

// chcount.c: # include <stdio.h> # define PERIOD. int main ( void ) { int ch; int charcount = 0; while (( ch = getchar ())!= PERIOD ) { if (ch!= " && ch!= \ ) charcount ++; } printf (" There are %d non - quote characters.\n", charcount ); return 0; } 45

"I m fine ". There are 7 non - quote characters. 46

1. 2. if && charcount 1 3. 47

4: &&! 48

4: &&! exp1 exp2 exp1 exp2 exp1 && exp2 exp1 exp2 exp1 exp2 exp1! exp1 exp1! exp1 48

iso646.h C99 iso646.h and && or not! 49

iso646.h C99 iso646.h and && or not! iso646.h if (ch!= " && ch!= \ ) charcount ++; if (ch!= " and ch!= \ ) charcount ++; 49

iso646.h 5: && iso646.h and or! not 50

! && a > b && b > c b > d ((a > b) && (b > c)) (b > d) 51

C b = (5 + 3) * (9 + 6) 5 + 3 9 + 6 C 52

C && C 53

while ((c = getchar ())!= && c!= \n ) 54

while ((c = getchar ())!= && c!= \n ) c c 54

while ( x ++ < 10 && x + y < 20) 55

while ( x ++ < 10 && x + y < 20) && x 1 55

if ( number!= 0 && 12/ number == 2) printf (" The number is 5 or 6.\ n"); 56

if ( number!= 0 && 12/ number == 2) printf (" The number is 5 or 6.\ n"); number 0 0 56

&& 90 100 if ( score >= 90 && score <= 100) printf (" Excellent!\n"); 57

if (90 <= score <= 100) printf (" Excellent!\n"); 58

if (90 <= score <= 100) printf (" Excellent!\n"); <= (90 <= score ) <= 100 90 <= score 1 0 100 range 58

59

59

1 // pseudo code 2 read a character 3 while there is more input 4 increment character count 5 if a line has been read, increment line count 6 if a word has been read, increment word count 7 read next character 60

// while (( ch = getchar ())!= STOP ) {... } 61

// while (( ch = getchar ())!= STOP ) {... } 61

getchar() STOP 62

63

63

c!= && c!= \n && c!= \t! isspace (c) // # include <ctype.h> c == c == \n c == \t isspace (c) // # include <ctype.h> 64

( inword) 1 inword 1 inword 0 // pseudo code if c is not a whitespace and inword is false set inword to true and count the word if c is a white space and inword is true set inword to false 65

i // wordcnt.c: # include <stdio.h> # include <ctype.h> # include < stdbool.h> # define STOP int main ( void ) { char c; char prev ; long n_chars = 0L; int n_ lines = 0; int n_ words = 0; int p_ lines = 0; bool inword = false ; 66

ii printf (" Enter text ( to quit ):\n"); prev = \n ; while ((c = getchar ())!= STOP ) { n_chars ++; if (c == \n ) n_lines ++; if (! isspace (c) &&! inword ) { inword = true ; n_words ++; } if ( isspace (c) && inword ) inword = false ; prev = c; } if ( prev!= \n ) 67

iii } p_ lines = 1; printf (" characters = % ld, words = % d, lines = %d, ", n_chars, n_words, n_lines ); printf (" partial lines = %d\n", p_lines ); return 0; 68

Enter text ( to quit ): Reason is a powerful servant but an inadequate master. characters = 56, words = 9, lines = 3, partial lines = 0 69

C if else? : C 70

x = (y < 0)? -y : y; 71

x = (y < 0)? -y : y; y 0 x = -y x = y if else if (y < 0) x = -y; else x = y; 71

expression1? expression2 : expression3 72

expression1? expression2 : expression3 expression1 expression2 expression1 expression3 72

max = (a > b)? a : b; 73

if else 74

200 75

// paint.c: # include <stdio.h> # define COVERAGE 200 int main ( void ) { int sq_feet, cans ; printf (" Enter number of square feet to be painted :\n"); while ( scanf ("%d", & sq_feet )) { cans = sq_ feet / COVERAGE ; cans += ( sq_ feet % COVERAGE == 0)? 0 : 1; printf (" You need %d %s of paint.\n", cans, cans == 1? " can " : " cans "); printf (" Enter next value (q to quit ):\n"); } return 0; } 76

Enter number of square feet to be painted : 200 You need 1 can of paint. Enter next value ( q to quit ): 225 You need 2 cans of paint. Enter next value ( q to quit ): q 77

continue break

continue break continue break 78

continue break continue continue continue 79

continue break continue 1-100 1-100 80

continue break continue i # include <stdio.h> int main ( void ) { const float MIN = 0.0 f, MAX = 100. 0 f; float score ; float total = 0.0 f; int n = 0; float min = MAX, max = MIN ; printf (" Enter the first score (q to quit ): "); while ( scanf ("%f", & score ) == 1) { if ( score < MIN score > MAX ) { printf (" %.1 f is invalid. Try again : ", score ); continue ; 81

continue break continue ii } printf (" Accepting %.1 f:\n", score ); min = ( score < min )? score : min ; max = ( score > max )? score : max ; total += score ; n ++; printf (" Enter next score (q to quit ): "); } if (n > 0) { printf (" Average of %d scores is %.1 f.\n", n, total /n); printf (" Low = %.1f, High = %.1 f.\n", min, max ); } else 82

continue break continue iii } printf ("No valid scores were entered.\n"); return 0; 83

continue break continue Enter the first score ( q to quit ): 20 Accepting 20. 0: Enter next score ( q to quit ): -1-1.0 is invalid. Try again : 90 Accepting 90. 0: Enter next score ( q to quit ): 110 110. 0 is invalid. Try again : q Average of 2 scores is 55. 0. Low = 20.0, High = 90. 0. 84

continue break continue while do while continue for 85

continue break continue count = 0; while ( count < 10) { ch = getchar (); if (ch == \n ) continue ; putchar (ch); count ++; } 10 86

continue break continue for ( count = 0; count < 10; count ++) { ch = getchar (); if (ch == \n ) continue ; putchar (ch); } 10 87

continue break break break break 88

continue break break 89

continue break break i # include <stdio.h> int main ( void ) { float length, width ; printf (" Enter the length of the rectangle : "); while ( scanf ("%f", & length ) == 1) { printf (" Length = %.2 f.\n", length ); printf (" Enter its width : "); if ( scanf ("%f", & width )!= 1) break ; printf (" Width = %.2 f;\n", width ); printf (" Area = %.2 f; \n", length * width ); 90

continue break break ii } printf (" Enter the length of the rectangle : " ); } printf (" Done.\n"); return 0; 91

continue break continue Enter the length of the rectangle : 10 Length = 10. 00. Enter its width : 20 Width = 20. 00; Area = 200. 00; Enter the length of the rectangle : 10 Length = 10. 00. Enter its width : q Done. 92

continue break break break for break break 93

continue break break int p, q; scanf ("%d", &p); while (p > 0) { printf ("%d\n", p); scanf ("%d", &q); while (q > 0) { printf ("%d\n", p*q); if (q > 100) break ; scanf ("%d", &q); } if (q > 100) break ; scanf ("%d", &p); } 94

switch

switch if ( condition1 )... else if ( condition2 )... else if ( condition3 )... else switch 95

switch i // animals.c # include <stdio.h> # include <ctype.h> int main ( void ) { char ch; printf (" Give a letter, and I will give you an "); printf (" animal name beginning with that letter.\n"); printf (" Please type in a letter : # to quit.\ n" ); while (( ch = getchar ())!= # ) { 96

switch ii if ( \n == ch) continue ; if ( islower (ch)) { switch (ch) { case a : printf (" alligator \n"); break ; case b : printf (" buffalo \n"); break ; case c : printf (" camel \n"); break ; 97

switch iii case d : printf (" dove \n"); break ; case e : printf (" eagle \n"); break ; default : break ; } } else printf (" I only recognize only lowercase letters.\n"); while ( getchar ()!= \n ) continue ; 98

switch iv } printf (" Please typer another letter or a #.\ n"); } printf (" Bye!\n"); return 0; 99

switch Give a letter, and I will give you an animal name beginning with that letter. Please type in a letter : # to quit. dog dove Please typer another letter or a #. a alligator Please typer another letter or a #. eff eagle Please typer another letter or a #. # Bye! 100

switch // switch switch ( integer expression ) { case constant1 : statements case constant2 : statements... default : statements } 101

switch 1 int char enum 102

switch // switch1. c: float is not allowed in switch # include <stdio.h> int main ( void ) { float x = 1. 1; switch (x){ case 1.1: printf (" Choice is 1"); break ; default : printf (" Choice other than 1, 2 and 3" ); break ; } return 0; } // Compiler Error : switch quantity not an integer 103

switch 2 break switch switch break switch 104

switch // switch2. c: There is no break in all cases # include <stdio.h> int main () { int x = 2; switch (x) { case 1: printf (" Choice is 1\n"); case 2: printf (" Choice is 2\n"); case 3: printf (" Choice is 3\n"); default : printf (" Choice other than 1, 2 and 3\n"); } return 0; } 105

switch Choice is 2 Choice is 3 Choice other than 1, 2 and 3 106

switch // switch3. c: There is no break in some cases # include <stdio.h> int main () { int x = 2; switch (x){ case 1: printf (" Choice is 1\n"); case 2: printf (" Choice is 2\n"); case 3: printf (" Choice is 3\n"); case 4: printf (" Choice is 4\n"); break ; default : printf (" Choice other than 1, 2, 3 and 4\n"); break ; } printf (" After Switch "); return 0; } 107

switch Choice is 2 Choice is 3 Choice is 4 After Switch 108

switch 3 case case 109

switch // switch4. c: A program with variable expressions in labels # include <stdio.h> int main () { int x = 2; int arr [] = {1, 2, 3}; switch (x) { case arr [0]: printf (" Choice 1\n"); case arr [1]: printf (" Choice 2\n"); case arr [2]: printf (" Choice 3\n"); } return 0; } // Compiler Error : case label does not reduce to an integer constant 110

switch 4 default switch 111

switch // switch5. c: The default block is placed above other cases. # include <stdio.h> int main () { int x = 4; switch (x) { default : printf (" Choice other than 1 and 2"); break ; case 1: printf (" Choice is 1"); break ; case 2: printf (" Choice is 2"); break ; } return 0; } 112

switch // switch5. c: The default block is placed above other cases. # include <stdio.h> int main () { int x = 4; switch (x) { default : printf (" Choice other than 1 and 2"); break ; case 1: printf (" Choice is 1"); break ; case 2: printf (" Choice is 2"); break ; } return 0; } Choice other than 1 and 2 112

switch i 5 case switch 113

switch i // Statements before all cases are never executed # include <stdio.h> int main () { int x = 1; switch (x) { x = x + 1; // This statement is not executed case 1: printf (" Choice is 1"); break ; case 2: printf (" Choice is 2"); break ; default : printf (" Choice other than 1 and 2"); 114

switch ii } break ; } return 0; Choice is 1 115

switch i 6 case 116

switch i // switch7. c: Program where two case labels have same value # include <stdio.h> int main () { int x = 1; switch (x) { case 2: printf (" Choice is 1"); break ; case 1+1: printf (" Choice is 2"); break ; } return 0; } 117

switch ii // Compiler Error : duplicate case value 118

switch # a, e, i, o, u 119

switch i // vowels.c: # include <stdio.h> int main ( void ) { char ch; int na, ne, ni, no, nu; na = ne = ni = no = nu = 0; printf (" Enter some text : enter # to quit.\n"); while (( ch = getchar ())!= # ) { switch (ch) { case a : case A : na ++; break ; case e : 120

switch ii } case E : ne ++; break ; case i : case I : ni ++; break ; case o : case O : no ++; break ; case u : case U : nu ++; break ; default : break ; } 121

switch iii } printf (" Number of text : %4c %4c %4c %4c\n", A, E, I, U ); printf (" %4d %4d %4d %4d\n", na, ne, ni, nu); return 0; 122

switch Enter some text : enter # to quit. See you tommorrow!# Number of text : A E I U 0 2 0 1 123

switch i switch case i : break ni++ I 124

switch ctype.h toupper() 125

switch ch = toupper (ch); switch (ch) { case A : na ++; break ; case E : ne ++; break ; case I : ni ++; break ; case O : no ++; break ; case U : nu ++; break ; default : break ; } 126

switch ch switch ( toupper (ch)) { case A : na ++; break ; case E : ne ++; break ; case I : ni ++; break ; case O : no ++; break ; case U : nu ++; break ; default : break ; } 127

switch switch if else switch if if ( integer < 1000 && integer > 2) switch 128