Computer Programming Control Structure

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

Chapter 04 (Part III) Control Statements: Part I.
 Control structures  Algorithm & flowchart  If statements  While statements.
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Chapter 3 - Structured Program Development
Introduction to Computers and Programming Lecture 7:
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. 1 Algorithms Computing problems –Solved by executing a series of actions in a specific order Algorithm.
Control Structures in C++ while, do/while, for switch, break, continue.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Structured Program Development in C
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
1 Lecture 3 Control Structures else/if and while.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Introduction to Computers and Programming Lecture 7:
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 7 JavaScript: Control Statements, Part 1
Introduction to Computer Programming
Algorithm: procedure in terms of
Control Structures Sequential execution Transfer of control
while Repetition Structure
Decisions Chapter 4.
Lecture 7: Repeating a Known Number of Times
JavaScript: Control Statements I
Week 4 – Repetition Structures / Loops
Chapter 4 – Control Structures Part 1
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Chapter 3 - Structured Program Development
2.6 The if/else Selection Structure
Chapter 4 - Control Structures: Part 1
EPSII 59:006 Spring 2004.
Presentation transcript:

Computer Programming Control Structure Lecture No. 5

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();

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.

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

#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

#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

#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

#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

#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

#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

#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

#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

#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

#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

#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

#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

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 -= 4 (d = d - 4) e *= 5 (e = e * 5) f /= 3 (f = f / 3) g %= 9 (g = g % 9)

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.

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

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

Increment and Decrement Operators When variable not in expression Preincrementing and postincrementing have same effect ++c; printf(“c”); and c++; are the same

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

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

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“);

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”);

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.

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);

Some ASCII values ASCII values from 65-92 are for A-Z.

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

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.

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’.

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

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