Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 06

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Sukkur IBA University Khairpur Campus

PROGRAMMING FUNDAMENTALS
Lab 06: Understanding Looping Structure
Under

Course Programming Fundamentals


Course Code CSC-102
Course Instructor(s) Nadeem Ahmed Tunio
Date (Lab Conducted) 25th September 2024 Date (Lab Submitted)

Objectives of Lab No. 6:


1. For loop
2. While loop
3. Do while loo

Introduction:

Loops: Loops are used in programming to repeat a specific block of code until
some end condition is met. There are three types of Loops in C++ Programming.

1. For loop
2. While loop
3. Do while loop
for Loop Syntax:

How for Loop works?

1. The initialization statement is executed only once at the beginning.


2. Then, the test expression is evaluated.
3. If the test expression is false, for loop is terminated. But if the test
expression is true,codes inside body of for loop is executed and update
expression is updated.
4. Again, the test expression is evaluated and this process repeats until the
test expressionis false.
while loop Syntax:

How while loop works?

 The while loop evaluates the test expression.


 If the test expression is true, codes inside the body of while loop is evaluated.
 Then, the test expression is evaluated again. This process goes on until
the testexpression is false.
 When the test expression is false, while loop is terminated.

do while loop Syntax:

How do while loop works?

 The codes inside the body of loop is executed at least once. Then, only the
test expressionis checked.
 If the test expression is true, the body of loop is executed. This process
continues untilthe test expression becomes false.
 When the test expression is false, do...while loop is terminated.

Sample Task 1: Write a program to print and sum 1 – 10 numbers.


Exercise

1: (OBSERVING FOR/WHILE/DO-WHILE BEHAVIOR)


Write a program to ask user input START/END and iterate loop from start to end
and performfollowing tasks:
a) Print numbers using FOR/WHILE/DO WHILE.
b) Test on following values and observe output (You may be asked
anytime during lab regarding his observation)

c) While testing on last two inputs, it may work abnormally. Now modify above
program so that it should work t time where t is difference between
start/end.
d) How for/while/do-while loop behaves?

2:
Write a C++ Program and ask user to enter any positive integer and then display the multiplication table
upto some range given by user.

3:
Write a program to display the prime number b/w 0 to 100

4:
Write a program to ask user input n_courses (Total Courses), then iterate loop
n_courses time and ask user marks in each course and perform following tasks:
a) Calculate total percentage of all courses.
c) PASS.

5:
Write a program that will ask you to enter any number and then the program
should tell y o u whether the number is palindrome or not?

…END…

You might also like