Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a =

Similar presentations


Presentation on theme: "Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a ="— Presentation transcript:

1 Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a = a % 2 ;a %= 2 ; b = b + ( c + 2 ) ;b += c + 2 ; d = d * ( e - 5 ) ;d *= e - 5 ;

2 int number = 10, sum = 0; int i; printf("enter int: "); scanf("%d", &i); switch (i) { case 1: printf("Exam2 =>Nov18\n"); printf("1:sum =%d number=%d\n", sum, number); break; case 2: number +=3; printf("2:sum =%d number=%d\n", sum, number); case 3: sum = number++; printf("3:sum =%d number=%d\n", sum, number); case 4: sum = --number; printf("4:sum =%d number=%d\n", sum, number); case 5: printf("fuzzball\n"); break; default: sum = number; printf("D:sum =%d number=%d\n", sum, number); } printf("Final: sum =%d number=%d\n", sum, number); What is the output?

3 Problems with Reading Characters  When getting characters, whether using scanf( ) or getchar( ), realize that you are reading only one character.  What will the user actually type? The character he/she wants to enter, followed by pressing ENTER.  So, the user is actually entering two characters, his/her response and the newline character.  Unless you handle this, the newline character will remain in the stdin stream causing problems the next time you want to read a character. Another call to scanf() or getchar( ) will remove it.

4 Reading Chars #include int main ( ) { char myChar, newline ; printf (“Enter a character: “) ; myChar = getchar( ) ; newline = getchar( ) ; /* could also usescanf(“%c”, &newline) ; */ printf (“The value of |%c| is |%c|.\n”, myChar, newline ) ; return 0 ; } User’s input is a Lets use this as an Enter symbol What happens when user hits Enter key on the keyboard?  \n

5 Reading Chars #include int main ( ) { char myChar='o', newline ; printf(“Enter e to exit\n") ; while(myChar!=‘e') { printf("Enter a character:") ; myChar = getchar( ); newline = getchar( ); printf ("The value of |%c| is |%c|.\n", myChar, newline ) ; } printf(“See You Later!") ; return 0; }

6 Additional Concerns with Garbage in stdin  When we were reading integers using scanf( ), we didn’t seem to have problems with the newline character, even though the user was typing ENTER after the integer.  That is because scanf( ) was looking for the next integer and ignored the newline (whitespace).  If we use scanf (“%d”, &num); to get an integer, the newline is still stuck in the input stream.  If the next item we want to get is a character, whether we use scanf( ) or getchar( ), we will get the newline.  We have to take this into account and remove it.

7 switch in a event controlled loop  Create a menu that continually prompts the user to enter 'H' to print a greeting in English, ‘A’ to print a greeting in Hawaiin, 'N' to print a greeting in Hindi, and S to print a greeting in Hebrew. The menu should also include an option 'E' which exits the program. Menu should look something like this: Please enter: H -> To print greeting in English A -> To print greeting in Hawaiian N -> To print greeting in Hindi S -> To print greeting in Hebrew E -> To exit 'H', it prints "Hello"; 'A' it prints "Aloha"; 'N' it prints "Namasthe"; ‘S’ it prints "Shalom". 'E', the program should print "Adios" before it exits the program.

8 getchar( ) Example Using EOF #include int main () { int grade, aCount, bCount, cCount, dCount, fCount ; aCount = bCount = cCount = dCount = fCount = 0 ; while ( (grade = getchar( ) ) != EOF ) { switch ( grade ) { case ‘A’: aCount++; break ; case ‘B’: bCount++; break ; case ‘C’ : cCount++; break ; case ‘D’: dCount++; break ; case ‘F’: fCount++; break ; default : break ; } return 0 ; }


Download ppt "Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a ="

Similar presentations


Ads by Google