0% found this document useful (0 votes)
2 views

LOOPS 1

The document outlines the course IT 122: Introduction to C Programming 1, targeting BS Information Technology students, and taught by Mr. Gerson A. Caga. It provides an overview of the C programming language, focusing on loops, particularly the while loop, including syntax, examples, and exercises for students. The course emphasizes understanding loop constructs and their application in programming.

Uploaded by

Jerson Caga
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

LOOPS 1

The document outlines the course IT 122: Introduction to C Programming 1, targeting BS Information Technology students, and taught by Mr. Gerson A. Caga. It provides an overview of the C programming language, focusing on loops, particularly the while loop, including syntax, examples, and exercises for students. The course emphasizes understanding loop constructs and their application in programming.

Uploaded by

Jerson Caga
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

COURSE OUTLINE

COURSE CODE : IT 122

TITLE : Introduction to C Programming 1

TARGET POPULATION : All BS Information Technology Students

INSTRUCTOR : MR. GERSON A. CAGA

Overview:

C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to


develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11
computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available
description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and
essentially all UNIX application programs have been written in C.

Content

• Loops
• While Loop

Objectives:
• Explain the meaning of loops
• Explain the loop constructs in C
• Create a program with while loops

Instruction to the Learner

Each chapter in this module contains a major lesson involving the use of Flowchart and its purpose.
The units are characterized by continuity, and are arranged in such a manner that the present unit is
related to the next unit. For this reason, you are advised to read this module. After each unit, there are
exercises to be given. Submission of task given will be every Monday during your scheduled class hour.
Loops

You may encounter situations when a block of code needs to be executed several number of times. In
general, statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated execution
paths.
A loop statement allows us to execute a statement or group of statements multiple times

C programming language provides the following types of loops to handle looping requirements.

LOOP TYPE DESCRIPTION


While Loop Repeats a statement or group of statements while
a given condition is true. It tests the condition
before executing the loop body.
For Loop Executes a sequence of statements multiple times
and abbreviates the code that manages the loop
variable.
Do While Loop It is more like a while statement, except that it
tests the condition at the end of the loop body.
Nested Loop You can use one or more loops inside any other
while, for, or do..while loop.

while Loop

A while loop in C programming repeatedly executes a target statement as long as a given condition is
true.

Syntax:
The syntax of a while loop in C programming language is:
while(condition)
{

} statement(s);

Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is any nonzero value. The loop iterates while the condition is true. When the
condition becomes false, the program control passes to the line immediately following the loop.

Example:

#include<stdio.h> int main ()


{
/* local variable definition */
int a = 10;

/* while loop execution */ while(


a < 20 )
{
printf("value of a: %d\n", a);
a++;
}
return 0;
}

When the above code is compiled and executed, it produces the following result:

value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a:


16 value of a: 17 value of a: 18
value of a: 19

Example:

1. Create a program to print natural numbers from 1 to n.

#include <stdio.h>
int main()
{
int i, n;

printf("Enter any number: ");


scanf("%d", &n);

printf("Natural numbers from 1 to %d : \n", n);


i = 1;
while( i<=n)
{
printf("%d\n", i);
i++;
}

return 0;
}

Output:

Enter any number: 5


Natural numbers from 1 to 10:
1
2
3
4
5

2. Create a program to print natural numbers in reverse from n to 1.


#include <stdio.h>

int main()
{
int i, start;
printf("Enter starting value:
"); scanf("%d", &start); i =
start;
while(i>=1)
{
printf("%d\n", i);
i--;
}
return 0;
}

Output:

Enter starting value: 9


Enter end value: 3
9
8
7
6
5
4
3

You might also like