Download presentation
Presentation is loading. Please wait.
1
Computer Programming Control Structure
Lecture No. 5
2
Introducing getch() and getche() functions
We can also receive input into char (character) type variable using getch() or getche() functions. Because getch() and getche() are 3rd party person codes. So, first we must have to declare its file in our code. #include <conio.h> The main difference between getch() and getche() is that (with getche() the character you type from the keyboard is visualize on the screen). While with getch() the character is not visualize on the screen. What the user will enter from the keyboard, it is put into the ch “variable”. char ch; ch = getch(); char ch; ch = getche();
3
Formulating Algorithms (Counter-Controlled Repetition)
Example A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. Counter-controlled repetition Loop repeated until counter reaches certain value Same as we have studied in “Program Counter” register of control unit.
4
Formulating Algorithms (Counter-Controlled Repetition)
Pseudocode for example: Set total to zero Set grade counter to one while grade counter is greater than ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average Next: C code for this example
5
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Total = 0 gradeCounter = 0 inputGrade = 0
6
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Total = 0 gradeCounter = 0 inputGrade = 0 average = 0
7
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Total = 0 gradeCounter = 0 inputGrade = 0 average = 0
8
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Enter the grade 67 Total = 0 gradeCounter = 0 inputGrade = 67 average = 0
9
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Total = 67 gradeCounter = 0 inputGrade = 67 average = 0
10
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Total = 67 gradeCounter = 1 inputGrade = 67 average = 0
11
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Total = 67 gradeCounter = 1 inputGrade = 67 average = 0
12
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Total = 67 gradeCounter = 1 inputGrade = 67 average = 0
13
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Enter the grade 78 Total = 67 gradeCounter = 1 inputGrade = 78 average = 0
14
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Total = 145 gradeCounter = 1 inputGrade = 78 average = 0
15
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Total = 145 gradeCounter = 2 inputGrade = 78 average = 0
16
#include <stdio.h>
#include <conio.h> void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); Total = 145 gradeCounter = 2 inputGrade = 78 average = 0
17
Assignment Operators Assignment expression abbreviations
Addition assignment operator c = c + 3; abbreviated to c += 3; Statements of the form variable = variable operator expression; can be rewritten as variable operator= expression; Other assignment operators d -= (d = d - 4) e *= (e = e * 5) f /= (f = f / 3) g %= (g = g % 9)
18
Increment and Decrement Operators
Increment operator (++) - can be used instead of c += 1 Decrement operator (--) - can be used instead of c -= 1 Preincrement When the operator is used before the variable (++c or –-c) Variable is changed, then the expression it is in is evaluated. Postincrement When the operator is used after the variable (c++ or c--) Expression the variable is in executes, then the variable is changed.
19
Increment and Decrement Operators
Increment operator (++) Increment variable by one c++ Same as c += 1 Decrement operator (--) similar Decrement variable by one c— Same as c -= 1
20
Increment and Decrement Operators
If c = 5, then printf(“%d”,++c); c is changed to 6, then printed out printf(“%d”,c++); Prints out 5 (cout is executed before the increment.) c then becomes 6
21
Increment and Decrement Operators
When variable not in expression Preincrementing and postincrementing have same effect ++c; printf(“c”); and c++; are the same
22
Pre/Post Increment Example (1)
int a = 2, b = 3; a++; // a = 3 a++; // a = 4 b = a + a; // b = 8 a++; // a = 5 b++; // b = 9 printf (“%d %d”,a, b); Example 1:- int a = 2, b = 3; b = ++a + ++a; printf (“%d %d”,++a,++b); Output a=5 b=9
23
Pre/Post Increment Example (2)
int a = 2, b = 3; b = a + a; // b = 4 a++; // 3 a++; // 4 a++; // 5 printf (“%d %d”,a, b); b++; // 5 Example 1:- int a = 2, b = 3; b = a++ + a++; printf (“%d %d”,++a,b++); Output a = 5 b = 4
24
Logical Operators in Multiple conditions
Used as conditions in loops, if statements && (logical AND) true if both conditions are true if ( gender == 1 && age >= 65 ) ++seniorMalesStudents; || (logical OR) true if either of condition is true if ( semesterAverage >= 90 || finalExam >= 90 ) printf("Student grade is A“); ^^ (EXCLUSIVE OR) true if either of condition is true, but not both. if ( semesterAverage >= 90 ^^ finalExam >= 90 ) printf("Student grade is A“);
25
Complex if Conditions (cont)
If we are combining together a larger number of conditions, it may be a good idea to put () around each individual condition definition. This makes the overall condition more readable. Example:- int temp = 20, visibility = 30; if ( (temp > 15 && temp < 30) || (visibility > 0 && visibility < 40) ) printf(“If body statement”); else Printf(“else body statement”);
26
ASCII values Each text character in “C Language” contains a value, that is called ASCII value. For example “character A” contains a ASCII value 65 and “character a” contains a ASCII value 97. We know in “C language” we can store a text character in “char” type variable. The size of char type variable is 8 bits, so the maximum value of ASCII would be 255. We can print a ASCII value of any character using “%d” signature.
27
ASCII Example Example 1 Example 2 Example 3
printf(“The ASCII value of A is %d”,’A’); Example 2 char keyPress = ‘A’; printf(“The ASCII value of A is %d”,keyPress); Example 3 char keyPress = 65; printf(“The character is %c”,keyPress);
28
Some ASCII values ASCII values from 65-92 are for A-Z.
29
What these codes are doing?
void main (void) { int number1 = 10; while (number1 == 11) printf(“While statement”); } getch (); void main (void) { while (true) printf(“While statement”); } getch (); Both programs will never terminates. true is equivalent to 1 And false is equivalent to 0
30
What these codes are doing?
void main (void) { char keyPress; while (1) keyPress = getch(); printf (“%c”,keyPress); if ( keyPress == ‘1’ ) exit (1); } getch (); void main (void) { char keyPress; while (1) keyPress = getch(); printf (“%d”,keyPress); if ( keyPress == ‘1’ ) exit (1); } getch (); These programs will be terminate only when user press ‘1’ from keyboard.
31
Sample Program (1) Program description:
Write a C program that reads a stream of characters from the user, and converts the “upper case” characters to “lower case”. converts the “lower case” characters to “upper case”. The code will terminates only when press ‘1’.
32
Sample Program (1) #include <stdio.h> #include <conio.h>
void main (void) { char charToPrint = ‘ ’; while (1) // while loop start charToPrint = getch(); // get a character from user if ( charToPrint == ‘1’ ) // if character is ‘1’ then terminate exit(1); //By force exit the program
33
Sample Program (1) (Cont)
if ( charToPrint >= 65 && charToPrint <= 92 ) printf(“%c”,charToPrint-32); // convert the small characters to capital letters if ( capitalFlag >= 97 && charToPrint <= 127 ) printf(“%c”,charToPrint+32); // convert the capital characters to small letters } // end of while } // end of program
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.