Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing Fundamentals

Similar presentations


Presentation on theme: "Computing Fundamentals"— Presentation transcript:

1 Computing Fundamentals
Lecture 10 Course Instructor: Engr.Romana Farhan Assistant Professor CPED

2 Today’s Lecture Conditional Operator Logical Operator goto Statement
While Loop Do-While Loop For Loop

3 Loops Loops cause a section of your program to be repeated a certain number of times. The repetition continues while a condition is true a certain number of times. When the condition becomes false, the loop ends and control passes to the statements following the loop.

4 Why Is Repetition Needed?
Suppose you want to add five numbers to find their average. From what you have learned so far, you could proceed as follows…

5 Why Is Repetition Needed?
But suppose you want to add and average 100, 1000, or even more numbers. You would have to declare that many variables and list them again in input statements and, perhaps, again in the output statements. This takes large amount of space and time. Also, if you want to run this program again with different values or with a different number of values, you have to rewrite the program.

6 Why Is Repetition Needed?
Suppose you want to add the following numbers: Consider the following statements, in which sum and num are variables of type int: sum = 0; cin >> num; sum = sum + num;

7 Why Is Repetition Needed?
Suppose you want to add the following numbers: Consider the following statements, in which sum and num are variables of type int: sum = 0; cin >> num; sum = sum + num; If you want to add 10 numbers, you can repeat statements 2 and 3 ten times. And if you want to add 100 numbers, you can repeat statements 2 and 3one hundred times.

8 Why Is Repetition Needed?
There are many other situations in which it is necessary to repeat a set of statements. For example, for each student in a class, the formula for determining the course grade is the same.

9 Kinds of Loop There are three kinds of loops in C++: while loop,
do loop, for loop,

10 While Loop

11 The while Loop It is a conditional loop statement.
It execute a statement as long as the given condition remains true. The while loop looks like a simplified version of the for loop. It contains a test expression but no initialization or increment expressions.

12 Syntax

13 Multiple Statements in a while Loop

14 While loop Although there is no initialization expression, the loop variable must be initialized before the loop begins. The loop body must also contain some statement that changes the value of the loop variable; otherwise the loop would never end.

15 Example #include <iostream> using namespace std; int main() { int c; c=1; while(c<=5){ cout<<“C++ programming”; c=c+1; } cout<<“program ends”;

16 Example // demonstrates WHILE loop #include <iostream> using namespace std; int main() { int n = 99; // make sure n isn’t initialized to 0 while( n != 0 ) // loop until n is 0 cin >> n; // read a number into n } return 0;

17 Output Here’s some sample output

18 Code example

19 Task: Modify this code so that your program also counts after how many tries user has succeeded. Write a program to calculate the sum of odd numbers between 1-10. Write a program to print the multiplication table of a number entered from the key board. Write a program to find out the factorial of an integer entered by user.. Write a program to print natural numbers 1 to 10 in descending order.

20 Output Try its code using while loop

21 The do Loop In a while loop, the test expression is evaluated at the beginning of the loop. If the test expression is false when the loop is entered, the loop body won’t be executed at all. But sometimes you want to guarantee that the loop body is executed at least once, no matter what the initial state of the test expression.

22 The do loop When this is the case you should use the do loop, which places the test expression at the end of the loop.

23 Syntax of the do loop

24 Example: #include<iostream> using namespace std; int main() { int n=1; do cout<<n<<endl; n++; }while(n<=10); }

25 Example Code

26 Output

27 Code Example

28 Output

29 The do loop The keyword do marks the beginning of the loop.
Then, as with the other loops, braces delimit the body of the loop. Finally, a while statement provides the test expression and terminates the loop. This while statement looks much like the one in a while loop, except for its position at the end of the loop and the fact that it ends with a semicolon.

30 The for Loop The for loop executes a section of code a fixed number of times. It’s usually (although not always) used when you know, before entering the loop, how many times you want to execute the code.

31 Example

32 OUTPUT

33 Example

34 How does this work?

35 Explanations The for statement controls the loop.
It consists of the keyword for, followed by parentheses that contain three expressions separated by semicolons:

36 Explanation These three expressions are the initialization expression, the test expression, and the increment expression. These three expressions usually (but not always) involve the same variable, which we call the loop variable.

37

38 Explanation The body of the loop is the code to be executed each time through the loop. In our example the loop body consists of a single statement:

39 Contdd This statement prints out the square of j, followed by endl. The square is found by multiplying j by itself. As the loop executes, j goes through the sequence 0, 1, 2, 3, and so on up to 14; so the squares of these numbers are displayed—0, 1, 4, 9, up to 196.

40 The Initialization Expression
The initialization expression is executed only once, when the loop first starts. It gives the loop variable an initial value. In our example it sets j to 0.

41 The Test Expression The test expression usually involves a relational operator. It is evaluated each time through the loop, just before the body of the loop is executed. It determines whether the loop will be executed again. If the test expression is true, the loop is executed one more time. If it’s false, then loop ends, and control passes to the statements following the loop.

42 Contdd. . . . In our example the statement
is executed following the completion of the loop.

43 Increment /Decrement Expression
The increment expression changes the value of the loop variable, often by incrementing it. It is always executed at the end of the loop, after the loop body has been executed. How Many Times? In the figure shown in previous slides, The first time j=0, This is ensured in the initialization expression. The last time through the loop, j is 14. This is determined by the test expression j<15.

44 Contdd When j becomes 15, the loop terminates; the loop body is not executed when j has this value. Next slide shows a flow chart for loop operation. The arrangement shown is commonly used to do something a fixed number of times: start at 0, use a test expression with the less-than operator and a value equal to the desired number of iterations increment the loop variable after each iteration.

45

46 Another for loop example
for(count=0; count<100; count++) What are your comments about this example?

47 Multiple Statements in the Loop Body
We want to execute more than one statement in the loop body. Multiple statements are delimited by braces. Note : there is no semicolon following the final brace of the loop body, although there are semicolons following the individual statements in the loop body.

48 The for Loop Remember: In the for statement, if the loop condition is omitted, it is assumed to be true.

49 The for Loop In a for statement, you can omit all three statements—initial statement, loop condition, and update statement. The following is a legal for loop: for ( ; ; ) cout << "Hello" << endl; This is an infinite for loop, continuously printing the word Hello.

50 Example // cubelist.cpp // lists cubes from 1 to 10 #include <iostream> #include <iomanip> //for setw int main() { int numb; //define loop variable for(numb=1; numb<=10; numb++) //loop from 1 to 10 cout << setw(4) << numb; //display 1st column int cube = numb*numb*numb; //calculate cube cout << setw(6) << cube << endl; //display 2nd column } return 0;

51 Output Here’s the output from the program: 1 1 2 8 3 27 4 64 5 125

52 Blocks and Variable Visibility
The loop body, which consists of braces delimiting several statements, is called a block of code. One important aspect of a block is that a variable defined inside the block is not visible outside it. Advantage of restricting the visibility of variables is that the same variable name can be used within different blocks in the same program.

53 Indentation and Loop Style
Good programming style dictates that the loop body be indented—that is, shifted right, relative to the loop statement. This indentation is an important visual aid to the programmer. It makes it easy to see where the loop body begins and ends. The compiler doesn’t care whether you indent or not.

54 Indentation and Loop Style
There is a common variation on the style we use for loops. We show the braces aligned vertically, but some programmers prefer to place the opening brace just after the loop statement, like this: for(numb=1; numb<=10; numb++) { cout << setw(4) << numb; int cube = numb*numb*numb; cout << setw(6) << cube << endl; }

55 for Loop Variations The increment expression doesn’t need to increment the loop variable; it can perform any operation it likes. In the next example it decrements the loop variable.

56 Example // factor.cpp // calculates factorials, demonstrates FOR loop #include <iostream> int main() { unsigned int numb; unsigned long fact=1; //long for larger numbers cout << “Enter a number: “; cin >> numb; //get number for(int j=numb; j>0; j--) //multiply 1 by fact *= j; //numb, numb-1, ..., 2, 1 cout << “Factorial is “ << fact << endl; return 0; }

57 Output Enter a number: 10 Factorial is

58 Variables Defined in for Statements
The loop variable can also defined inside the for statement like: for(int j=numb; j>0; j--) It defines the variable as closely as possible to its point of use. Variables defined in the loop statement this way are visible in the loop body only.

59 Multiple Initialization and Test Expressions
You can put more than one expression in the initialization part of the for statement, separating the different expressions by commas. You can also have more than one increment expression. You can have only one test expression. Multiple initialization expressions and multiple increment expressions are separated by commas.

60 Example for( j=0, alpha=100; j<50; j++, beta-- ) { // body of loop }

61 When to Use Which Loop The for loop is appropriate when you know in advance how many times the loop will be executed. The while and do loops are used when you don’t know in advance when the loop will terminate. The while loop when you may not want to execute the loop body even once, and the do loop when you’re sure you want to execute the loop body at least once.

62 Questions????


Download ppt "Computing Fundamentals"

Similar presentations


Ads by Google