






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







ESHA EMAN 24I- CE-B
__ STUDENT NAME ROLL NO S E C
LAB ENGINEER'S SIGNATURE & DATE Maryam Zafar
Prepared by: Engr. Aamer Munir Version: 1. Date last Last edited by: Engr. Azhar Rauf, Engr. Aamer Munir Sep 27, 2018 edited: Date last Verified by: Engr. Azhar Rauf Oct 1, 2023
edited:
In this lab session you will learn about:
Whenever we need to do any task repetitively, we use loops. There are three types of loops used in C++ -- while loop, for loop and do-while loop. while loop: The structure of a while loop is: while(expression) body of the loop where the expression provides an entry condition to the loop, i.e., the loop will be executed only when the expression evaluates to true. Once the body of the loop is executed, the expression is checked again and if it evaluates to true the body of the loop is executed again. This evaluation of the expression and execution of body of the loop continues until the expression evaluates to false after which the code next to the body of the loop executes. Each execution of the body of the loop is called an iteration. If expression always evaluates to true , such a loop is known as an infinite loop. A while loop is usually one of the four main types: i) Counter-Controlled while loop: When we wish a while loop to execute or iterate N number of times, we use the expression to test a counter variable value against N and increment the counter each times the body of loop executes. counter = 0; while(counter < N) { body of the loop counter+ +; }
int num, sum = 0; //even and odd is linked to integers int count = 0; bool NumReached= false; while(!NumReached) { cin >> num; if (num>=0 && num%2==0) { count++; sum += num; } if(count == 10) NumReached= true; } cout << "Ave = " <<static_cast
Note the reserved word ' do ' and the semicolon at the end of while(expression). In a do-while loop, the body of the loop is executed first and then expression is evaluated. If the expression evaluates to true , the body of the loop is executed again, else the loop is exited. A do-while loop will be executed at least once because contrary to while and for loops, a do- while loop does not have an entry condition, rather an exit condition. Our example, DIVDO, invites the user to enter two numbers: a dividend (the top number in a division) and a divisor (the bottom number). It then calculates the quotient (the answer) and the remainder, using the / and % operators, and prints out the result.
2. break and continue statements Remember the break statement in a switch-case structure. It provides an exit from switch structure. Similarly, break can be used to exit from a loop. For example, instead of using a flag in a flag-controlled while loop: bool flag = false; while(! flag) { body of the loop if(expression) flag = true; } consider the following while loop with a break statement to do the same job: while(true) { body of the loop if(expression) break; }
Task # 6: Code file name lab1_6.cpp Write a program that ask user to enter password. User is allowed to enter password five times. Correct password is 12345. If he enters correct password, print the number of trials on which he guessed right. Otherwise, terminate the program if he/she cannot guess in five trials. HINT: You can use break; to end a loop early. TASK 01:
TASK 02:
TASK 04:
TASK 05: