The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.Alnabris Computer Programming: C++ Experiment #4 Loops Part II Experiment #3:Loops Part II Contents Loop Control Statement Nested loop Loop Control Statement Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. C++ supports the following control statements. Sr.No 1 2 3 2 Control Statement & Description break statement Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. goto statement Transfers control to the labeled statement. Though it is not advised to use goto statement in your program. Experiment #3:Loops Part II break Statement The break; statement terminates a loop (for, while and do..while loop) and a switch statement immediately when it appears. Syntax break; In real practice, break statement is almost always used inside the body of conditional statement (if...else) inside the loop. How break statement works? 3 Experiment #3:Loops Part II EX1: C++ program to add all number entered by user until user enters 0. In the above program, the test expression is always true. The user is asked to enter a number which is stored in the variable number. If the user enters any number other than 0, the number is added to sum and stored to it. Again, the user is asked to enter another number. When user enters 0, the test expression inside if statement is false and body of else is executed which terminates the loop. Finally, the sum is displayed. 4 Experiment #3:Loops Part II continue Statement It is sometimes necessary to skip a certain test condition within a loop. In such case, continue; statement is used in C++ programming. Syntax continue; In practice, continue; statement is almost always used inside a conditional statement. How continue statement works? 5 Experiment #3:Loops Part II EX2: Write program to display integer from 1 to 10 except 6 and 9.. In above program, when i is 6 or 9, execution of statement cout << i << "\t"; is skipped inside the loop using continue; statement. 6 Experiment #3:Loops Part II goto Statement goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program. Syntax goto label; ... .. ... ... .. ... ... .. ... label: statement; ... .. ... in the syntax above, label is an identifier. When goto label; is encountered, the control of program jumps to label: and executes the code below it. 7 Experiment #3:Loops Part II How continue statement works? EX3: Write program calculates the average of numbers entered by user If user enters negative number, it ignores the number and calculates the average of number entered before it... 8 Experiment #3:Loops Part II You can write any C++ program without the use of goto statement and is generally considered a good idea not to use them (: . 9 Experiment #3:Loops Part II Nested Loop A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting. Syntax The syntax for a nested for loop statement in C++ is as follows for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); // you can put more statements . The syntax for a nested while loop statement in C++ is as follows while(condition) { while(condition) { statement(s); } statement(s); // you can put more statements. } The syntax for a nested do...while loop statement in C++ is as follows do { statement(s); // you can put more statements. do { statement(s); } while( condition ); } while( condition ); 10 Experiment #3:Loops Part II EX4: Write program uses a nested for loop to find the prime numbers from 2 to 100 11 Experiment #3:Loops Part II Lab Work: 1- Write program to Check Armstrong Number or not ; Note: A positive integer is called an Armstrong number if the sum of cubes of individual digit is equal to that number itself. For example: 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3 Armstrong number. 12 is not equal to 1 * 1 * 1 + 2 * 2 * 2 Armstrong number. // 153 is an // 12 is not an 2- Write program to print half pyramid using * Note: User will entered the number of row * * * * * * * * * * * * * * * 12 Experiment #3:Loops Part II Homework 1- Write program to print pyramid using loop Note: user will entered the number of row 2- Write program to display prime numbers between two intervals Output Enter two numbers(intervals): 20 50 Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47 Good Luck 13