C - Loops
C - Loops
C - Loops
Types of loops
1. for
2. while
3. do while
1. for loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute
a specific number of times.
Syntax
Flow Chart
Example
#include <stdio.h>
int main ()
{
int a;
/* for loop execution */
for( a = 10; a < 20; a = a + 1 )
{
printf("value of a: %d\n", a);
}
return 0;
}
Output
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
while loop in C
A while loop in C programming repeatedly executes a target statement as long as a given
condition is true.
Syntax
while(condition)
{
statement(s);
}
Here, the key point to note is that a while loop might not execute at all. When the condition is
tested and the result is false, the loop body will be skipped and the first statement after the while
loop will be executed.
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;
}
Output
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
goto statement in C
A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled
statement in the same function.
Syntax
goto label;
..
.
label: statement;
Example
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
LOOP:do {
if( a == 15) {
/* skip the iteration */
a = a + 1;
goto LOOP;
}
printf("value of a: %d\n", a);
a++;
}
while( a < 20 );
return 0;
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
LECTURE 09
C tokens:
C tokens are the basic buildings blocks in C language which are constructed together to write a C
program. Each and every smallest individual units in a C program are known as C tokens.
Types of C tokens
where,
main – identifier
{,}, (,) – delimiter
int – keyword
x, y, total – identifier
main, {, }, (, ), int, x, y, total – tokens
Constant
Constant in C means the content whose value does not change at the time of execution of a program.
In the C programming languages, const is a type qualifier: a keyword applied to a data type that
indicates that the data is constant (does not vary). You can use const prefix to declare constants
with a specific type as follows –
e.g
Types of Constants
1. Integer Number Constant [ e.g int, unsigned int, long int, long long int]
2. Single Character Constant [ e.g ‘A’ , ‘B’, ‘C’]
3. String Constant [“Khalid” , “Lahore”]
4. Float Constant [float, doule]
Example
#include<stdio.h>
#include<conio.h>
void main()
{
const int std_reg_no;
const char std_gener;
std_reg_no = 5;
printf(“Value of you have stored is= %d”, std_reg_no);
std_reg_no = 7; .
}
Output
Error: Cannot modify a const object
C – Literals
Constants refer to fixed values that the program may not alter during its execution. These fixed
values are also called literals.
Example
#include<stdio.h>
#include<conio.h>
void main()
{
const int std_reg_no;
const char std_gener;
std_reg_no = 5;
printf(“Value of you have stored is= %d”, std_reg_no);
}
C Strings are array of characters ended with null character (‘\0’). This null character indicates the end of the
string. Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in C.
All of these functions read a character as input and return an integer value.
getch():
getch() is a nonstandard function and is present in conio.h header file which is mostly used by MS-DOS
compilers like Turbo C. It reads a single character from keyboard. But it does not use any buffer, so the
entered character is immediately returned without waiting for the enter key.
Syntax:
int getch();
Example:
#include <stdio.h>
#include <conio.h>
int main()
{
char a;
a= getch();
printf("%c",a);
}
Output
Input: g (Without enter key)
getche()
Like getch(), this is also a non-standard function present in conio.h. It reads a single character from the
keyboard and displays immediately on output screen without waiting for enter key.
Syntax:
int getche(void);
Example:
#include <stdio.h>
#include <conio.h>
int main()
{
printf("%c", getche());
return 0;
}
Output
Input: g(without enter key as it is not buffered)
In computer hardware and software product development, documentation is the information that
describes the product to its users. It gives a comprehensive procedural description of a program. It
shows as to how software is written. Program documentation includes hard-copy or electronic
manuals that enable users, program developers, and operators to interact successful with a
program. It consists of the
The term is also sometimes used to mean the source information about the product contained in
design documents, detailed code comments, white papers, and blackboard session notes.
. Program documentation even has the capability to sustain any later maintenance or development
of the program. The program documentation describes what exactly a program does by mentioning
about the requirements of the input data.