Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Saturday, July 23, 2022

Looping Statements in C programming for SEE and NEB

 Looping Statements in C for SEE and NEB


Looping Statements in C for SEE and NEB


Introduction:

Looping statements are an essential aspect of programming that allows us to execute a set of statements repeatedly. C programming language provides several types of looping statements that help us to achieve this. In this article, we will discuss the looping statements in C programming for SEE (Secondary Education Examination) and NEB (National Examination Board).

Looping (Repetitive Structure):

Loops follow the iteration concept. Loops allow a set of instructions to be performed until a certain condition becomes false. This condition may be predefined or opened. Different types of loops are as here under:
  • The FOR loop.
  • The WHILE loop
  • The DO……….WHILE loop

 


a) For Loop:

The for statement makes programming more convenient to count iterations of a loop and works well where the number of iterations of the loop is known before the loop is entered.

  • Loop’s initial value i.e. to set loops starting value.
  • Test condition i.e. to determine the number of repetitions
  • increment or decrement value

Syntax:

For(initialization; test-condition;increment/decrement)
{
statement;
-----------;
}

 

In the above code, "initialization" is the initial value of the loop control variable, "condition" is the condition for continuing the loop, and "increment/decrement" is the value by which the loop control variable is incremented or decremented. The statements inside the loop will be executed repeatedly until the condition becomes false.


Example 1: To display the natural numbers from 1 to 10 vertical.


#include<stdio.h>

#include<conio.h>

main()

{

int i;

for(i=1;i<=10;i++)

{

printf("\n%d",i);

}

getch();

}

 

Output:

 


 

Example 2: To display the natural numbers from 1 to 10 horizontal.


#include<stdio.h>

#include<conio.h>

main()

{

int i;

for(i=1;i<=10;i++)

{

printf("%d\t",i);

}

getch();

}

 

Output:

 


 

b) While Loop:
The while statement is used to carry out looping operations, in which a group of statements is executed repeatedly, until condition has been satisfied.

Syntax:

while(expression)

 {

  statement;

  increment/decrement;

  }


In the above code, "condition" is the condition for continuing the loop. The statements inside the loop will be executed repeatedly until the condition becomes false.


Example: WAP to display the natural numbers 1 to 10.


#include<stdio.h>

#include<conio.h>

main()

{

clrscr();

int i=1;

while(i<=10)

{

printf("%d\t",i);

i++;

}

getch();

}

 

 

c) Do…….While Loop:

The do……while loop is sometimes referred to as do……loop in ‘C’. Unlike for and while loops, this loops checks its condition at the end of the loop, that is, after the loops has been executed.  This means that do ……while loop will execute at least once, even if the condition is false initially.

 

Syntax:

do

{

  statement;

  increment/decrement;

  } while(condition);

 

In the above code, "condition" is the condition for continuing the loop. The statements inside the loop will be executed repeatedly until the condition becomes false.


Example: WAP to display the natural number 1 to 10.

#include<stdio.h>

#include<conio.h>

main()

{

clrscr();

int a=1;

do

{

printf("\n%d  ",a);

a++;

} while(a<=10);

getch();

}

 

Conclusion: 

Looping statements are essential in programming and play a crucial role in solving problems efficiently. C programming provides three types of looping statements: for loop, while loop, and do-while loop. In SEE and NEB exams, questions related to looping statements are frequently asked, and students need to have a good understanding of their syntax and logic. By mastering the looping statements, students can solve problems efficiently and write programs that are error-free and optimized.

 

You can also Read:




No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages