Docsity

Repetition and Flow Control Structures in C++: Lab Manual with Exercises, Schemes and Mind Maps of Programming Languages

Toggle sidebar
bg1
Programming Fundamentals
(CL1002)
LABORATORY MANUAL
BS-CE, Spring 2025
LAB 01
REPETITION AND FLOW CONTROL STRUCTURES
ESHA EMAN 24I-5702
CE-B
________________________________________ __________ __
__
STUDENT NAME ROLL NO
S
E
C
______________________________________
LAB ENGINEER'S SIGNATURE & DATE
Maryam Zafar
MARKS AWARDED: /10
_____________________________________________________________________
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD
Prepared by: Engr. Aamer Munir Version: 1.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
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Repetition and Flow Control Structures in C++: Lab Manual with Exercises and more Schemes and Mind Maps Programming Languages in PDF only on Docsity!

Programming Fundamentals

(CL1002)

LABORATORY MANUAL

BS-CE, Spring 2025

LAB 01

REPETITION AND FLOW CONTROL STRUCTURES

ESHA EMAN 24I- CE-B


__ STUDENT NAME ROLL NO S E C


LAB ENGINEER'S SIGNATURE & DATE Maryam Zafar

MARKS AWARDED: /

_____________________________________________________________________

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD

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:

Lab Learning Objectives:

In this lab session you will learn about:

  1. Repetition Control Structures or Loops
  2. Flow Control Structures

Tools needed:

  1. PC running Windows Operating System (OS)
  2. DevC++ (installed) Turn on the PC and log into it using your student account information. Note: The information and concepts discussed in this lab manual are in sufficient detail but are still not exhaustive. It is assumed that all these concepts are already taught to you in your PF theory class.

1. Repetition Control Structures or Loops

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(sum)/count<< endl; for loop: A while loop can be used for any task that requires repetition. However, C++ often provides optimized ways of writing code structures that are encountered very often by programmers. A for loop is such an example. A for loop is just a specialized form of writing a counter-controlled while loop. for(initial statement; expression; update statement) body of the loop The initial statement is executed only once before the for loop is entered. The expression is just like the expression in while loop which is evaluated and if true the body of the loop is executed. The update statement is executed at the end of each iteration. For example, the following while loop code: counter = 0; while(counter < N) { body of the loop counter+ +; } can be written as a for loop code as: for(counter = 0; counter < N; counter++) { body of the loop } Hence, for loop is ideally suited where the number of iterations is known. This does not mean it cannot be used in other situations. do-while loop: A do-while loop has the following syntax: do { body of the loop } while(expression) ;

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.

int main() {

long dividend, divisor; char ch; do //start of

do loop { //do some processing cout << “Enter

dividend: “; cin >> dividend; cout << “Enter

divisor: “; cin >> divisor; cout << “Quotient is

“ << dividend / divisor; cout << “, remainder is

“ << dividend % divisor; cout << “\nDo

another? (y/n): “; //do it again?

cin >> ch;

while( ch != ‘n’ ); //loop condition

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

cout << “Enter a number: “; cin >> n; //get

number to test for(j=2; j <= n/2; j++) //divide by

every integer from if(n%j == 0)

//2 on up; if remainder is

{ //it’s divisible by j

cout << “It’s not prime; divisible by “ << j <<

endl; exit(0); //exit from the program

cout << “It’s prime\n”;

Write a program in which user will enter the range e.g. 100 and your program should

display all the prime numbers from 1 to 100. Prime numbers start from 2.

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: