MCT-242L CP1 2022 LM11 10
MCT-242L CP1 2022 LM11 10
OBJECTIVE:
This lab will introduce advanced concepts in loops available in C Language Program. At the end of this lab,
you should be able to:
APPARATUS:
Laptop\PC with following tools installed
o Visual Studio Code with C/C++ and Code Runner Extensions
o C/C++ mingw-w64 tools for Windows 10
When execution leaves a scope, all automatic objects that were created in that scope are destroyed. It
means that when a function or block of code finishes executing, all of the local variables that were created
within that function or block of code are automatically destroyed. This is known as automatic garbage
collection. Automatic garbage collection is a feature of many programming languages, including C, C++,
and Java. It is important because it ensures that memory is not wasted. When an object is no longer needed,
it is automatically destroyed by the garbage collector. This frees up memory so that it can be used for other
things.
void my_function() {
int i = 10; // i is an automatic object
// Do something with i
}
int main() {
my_function();
// i is no longer in scope and is automatically destroyed by the garbage collector
return 0;}
1|Page Computer Programming-I Lab
Department of Mechatronics and Control Engineering
University of Engineering and Technology Lahore
In this example, the variable i is an automatic object because it is declared within the my_function()
function. When the my_function() function returns, the variable i is automatically destroyed by the
garbage collector. This is because the i variable is no longer in scope. Automatic garbage collection can
help to prevent memory leaks and make code more efficient.
break Statement
break;
Syntax of break Statement
#include <stdio.h>
void print_loop_control_variable(int num) {
int i = 0;
while (i < num) {
printf("The value of loop control variable is %d\n", i++);
if (i > 4) {
/* terminate the loop using break statement */
break;
}
}
}
int main() {
int num = 0;
printf("Enter a number >> ");
scanf("%d", &num);
void print_stars_pattern(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j > 3)
break;
else
printf("*");
}
printf("\n");
}
}
int main() {
int num = 0;
printf("Enter a number >> ");
scanf("%d", &num);
print_stars_pattern(num);
return 0;
}
// End of program
Enter a number >> 6
****
****
Program Output 1 ****
****
****
****
Enter a number >> 3
****
Program Output 2
****
****
LAB SUBMISSION:
Create a Visual Studio Code Workspace, Lab_11 and c files (Task_11_1.c to Task_11_9.c) for individual
tasks and add them to Lab_11 workspace.
Answer:
#include <stdio.h> int Isprime(int num); int main() { int num = 0; printf("Enter your number:"); scanf("%d", &num);
Sum10Num().
Enter n1 >> 12
Enter n2 >> 43
Sample Output 1 Enter n3 >> 21
Enter n4 >> -1
Sum = 76.00
Enter n1 >> 12
Enter n2 >> 2
Enter n3 >> 0
Enter n4 >> 13
Enter n5 >> 5
Sample Output 2 Enter n6 >> 3
Enter n7 >> 54
Enter n8 >> 2
Enter n9 >> 1
Enter n10 >> 4
Sum = 96.00
Answer:
#include <stdio.h> int Sum10Num() { int sum = 0; int number; int count = 0; while (count < 10) { printf("Enter y
Answer:
#include <stdio.h> void Password(const unsigned int pass) { const unsigned int password = 341400; int enter_password
If you are using nested loops, the break statement will stop the execution of the innermost loop and start
executing the next line of code after the block.
continue Statement
For the for loop, continue statement causes the conditional test and increment
portions of the loop to execute. For the while and do...while loops, continue
statement causes the program control to pass to the conditional tests.
continue;
Syntax of continue Statement
#include <stdio.h>
int main() {
int num = 0;
return 0;
}
// End of program
Enter a number >> 3
The value of loop control variable is 0
Program Output 1
The value of loop control variable is 1
The value of loop control variable is 2
Enter a number >> 5
The value of loop control variable is 0
Program Output 2 The value of loop control variable is 1
The value of loop control variable is 2
The value of loop control variable is 4
Answer:
#include <stdio.h> int Sumoddnum() { int oddsum = 0; int number; for (int i = 0; i < 6; i++) { printf("Enter your nu
Answer:
#include <stdio.h> void printStars(int n, int current) { if (current > n) { return; } for (int i = 0; i < current; i++) {
goto Statement
goto label;
...
...
label: statement;
Syntax of goto Statement
Here label can be any plain text except C keyword, and it can be set anywhere in the C program above or
below to goto statement.
#include <stdio.h>
void print_triangle(int num) {
int i = 0, j = 0;
// goto statement
goto PRINT;
NEXT_LINE:
for (j = 0; j <=i; j++) {
printf("*");
}
printf("\n");
i++;
// goto statement
PRINT: if (i < num) {
goto NEXT_LINE;
}
}
int main() {
int n = 0;
printf("Enter a number >> ");
scanf("%d", &n);
print_triangle(n);
// Call the print_triangle function
return 0;
}
// End of program
Enter a number >> 5
*
**
Program Output 1
***
****
*****
Enter a number >> 3
*
Program Output 2
**
***
To implement loops and conditional statements. The goto statement can be used to implement loops and
conditional statements in a way that is more efficient than using other control flow statements, such as the
for loop and the if statement. However, this can make the code more difficult to read and maintain.
To break out of nested loops and conditional statements. The goto statement can be used to break out of
nested loops and conditional statements without having to use multiple break statements. This can make the
code more concise and readable.
To implement error handling. The goto statement can be used to implement error handling by jumping to
a labeled statement where the error can be handled. This can make the code more efficient and robust.
#include <stdio.h>
int main()
{
int num = 0;
printf("Enter a number: ");
scanf("%d", &num);
print_numbers(num);
return 0;
}
// End of program
Enter a number: 6
0
1
Program Output 1 2
3
4
5
Enter the number of times to print the numbers: 3
0
Program Output 2
1
2
---------------------------------------------------------------------------
Here is an example of how the goto statement can be used to implement a conditional
statement. Following code will prompt the user to enter a number and then print whether
the number is positive, negative, or zero. The goto statement is used to jump to the
appropriate labeled statement based on the value of the number.
---------------------------------------------------------------------------
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */
#include <stdio.h>
POSITIVE:
printf("The number is positive.\n");
goto END;
NEGATIVE:
printf("The number is negative.\n");
goto END;
ZERO:
printf("The number is zero.\n");
END:
return;
}
int main() {
int num = 0;
return 0;
}
// End of program
Enter a number: 0
Program Output 1
The number is zero.
Enter a number: -2
Program Output 2
The number is negative.
#include <stdio.h>
error:
printf("The number cannot be zero.\n");
return 1;
end:
printf("the entered number is %d\n",num);
return 0;
}
int main() {
int num = 0;
It is important to note that the goto statement should be used with caution. It can make code difficult to read
and maintain, and it can lead to spaghetti code. However, it can be useful in some cases to improve the
performance or efficiency of a program.
#include <stdio.h> void Print_triangle(int num); int main() { int num = 0; printf("Enter the number of stars:"); scanf("%
Enter a number: 0
Program Output 1
The number is zero.
Enter a number: -2
Program Output 2
The number is negative.
Answer:
#include <stdio.h> void Print_number_type(int num); int main() { int num = 0; printf("Enter your number:"); scanf("%
Write a program that will ask user to enter a number and display all
the natural numbers up to the number entered. You are not allowed to
use any loop. Make a function NaturalNums().
Enter a number: 7
Sample Output 1
1 2 3 4 5 6 7
Enter a number: 19
1 2 3 4 5 6 7
Sample Output 2
8 9 10 11 12 13 14
15 16 17 18 19
Answer:
#include <stdio.h> void print_natural_numbers(int num); int main() { int num = 0; printf("Enter number:"); scanf("%d
A loop becomes an infinite loop if a condition never becomes false. The for loop is traditionally used for
this purpose. Since none of the three expressions that form the ‘for’ loop are required, you can make an
endless loop by leaving the conditional expression empty.
#include <stdio.h>
void print_infinite_loop() {
unsigned long int i = 1;
for (;;) {
printf("%ld This is an infinite loop!\n", i++);
}
}
int main() {
// Call the print_infinite_loop function
print_infinite_loop();
return 0;
}
// End of program
When the conditional expression is absent, it is assumed to be true. You may have an initialization and
increment expression, but C programmers more commonly use the for(;;) construct to signify an infinite
loop.
Answer:
#include <stdio.h> void Print_infinite_loop(int num); int main() { int num; // call the function Print_infinite_loop(num);
Students are advised to fill the manual and submit it before the upcoming lab. Kindly rename the file as
‘MCT-242L_CP1_2022_LM11_XX’, where XX is your roll number. After completing the manual, turn it in Google Classroom.