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