Professional Documents
Culture Documents
HH) Lec 8 Repeatation Structure
HH) Lec 8 Repeatation Structure
Introduction to Computer
Programming
condition that tests for the final value of the control variable
(i.e., whether looping should continue).
int counter = 1;
// display result
cout << "Class average is " << average << endl;
getch();
return 0; // indicate program ended successfully
} // end function main
Enter grade: 90
Enter grade: 57
Enter grade: 79
Enter grade: 82
Enter grade: 94
Class average is 81
// code2.cpp
// Class average program with sentinel-controlled repetition.
#include <iostream>
#include <conio.h>
#include <iomanip> // parameterized stream manipulators
using std::cout; using std::cin; using std::endl; using std::fixed;
using std::setprecision; Data type double also used// to
sets numeric output precision
represent floating point numbers.
//function main begins program execution
main()
{
int total, gradeCounter, grade; / / sum of grades
getch();
Syntex Syntex
Sum is 2550
The switch/case Multiple-Selection Structure:
Switch:
Useful when a variable or expression is tested for all the values it can
assume and different actions are taken.
Format
Series of case labels and an optional default case
switch ( value ){
case '1':
actions
case '2':
actions
default:
actions
}
break; causes exit from structure
The switch Multiple-Selection Structure:
true
case a case a action(s) break
false
true
case b case b action(s) break
false
.
.
.
true
case z case z action(s) break
false
default action(s)
Switch /case :
/* C program to demonstrate the working of switch...case statement */ /* Program to create a simple calculator for addition, subtraction, multiplication and division */
# include <iostream>
# include <conio.h>
using namespace std;
int main()
{
char a;
float num1,num2;
cout<<"Enter a +, - , * or / :\n";
a = getche();
cout<<"\nEnter two operands:\n";
cin>>num1;
cin>>num2;
switch(a) {
case '+':
cout<<"num1+num2="<<num1+num2;
getch();
break;
case '-':
cout<<"num1-num2="<<num1-num2;
getch();
break;
case '*':
cout<<"num1*num2="<<num1*num2;
getch();
break;
case '/':
cout<<"num1/num2="<<num1/num2;
getch();
break;
default:
break;
}
The do/while Repetition Structure
Format:
do {
statement
} while ( condition );
Good practice to put brackets in, even if not required
The do/while Repetition Structure
action(s)
true
condition
false
Do-While Example:
// code10.cpp
// Using the do/while repetition structure.
#include <iostream>
using std::cout;
using std::endl;
do {
cout << counter << " "; // display counter
} while ( ++counter <= 10 ); // end do/while
cout << endl;
Break:
• Continue
1 2 3 4 6 7 8 9 10 Program Output
Used continue to skip printing the value 5
Logical operators:
• Used as conditions in loops, if statements
• ! (logical NOT, logical negation)
– Returns true when its condition is false, & vice versa
if ( !( grade == -1 ) )
cout << "The next grade is " << grade << endl;
Alternative:
if ( grade != -1 )
cout << "The next grade is " << grade << endl;
• || (logical OR)
– true if either of condition is true
if ( semesterAverage >= 90 || finalExam >= 90 )
cout << "Student grade is A" << endl;
Structured-Programming Summary
• Structured programming
3) Any rectangle (action) can be replaced by any control structure (sequence, if,
if/else, switch, while, do/while or for)
Rule 3 Rule 3
25
Structured-Programming Summary
• All programs broken down into
– Sequence
– Selection
• if, if/else, or switch
• Any selection can be rewritten as an if statement
– Repetition
• while, do/while or for
• Any repetition structure can be rewritten as a while
statement
26
Assignment 1
How to print
******
*****
****
***
**
*
by using while loop?
Assignment 2
How to print
*
**
***
****
*****
******
by using for loop?
Assignment 3
• Write a program to print the factorial of a number
entered by user?
Precautions
Submission will be in the form of print screen image with output, code and comments in front
of each line. (1 print page for each assignment, 3 prints in total)
All files must be shown and saved with your name in screen shot.
Last Date of Submission for all three assignments is . Monday (2-12-2013) 3:00 Pm
• http://www.codingunit.com/c-tutorial-for-loop-w
hile-loop-break-and-continue
• http://www.tutorialspoint.com/cprogramming/c
_loops.htm
• http://www.tenouk.com/clabworksheet/labwork
sheet7_1.html