Loop control instructions Flashcards
what are different loop control instructions
for
while
do while
For loop
include<stdio.h></stdio.h>
for ( initialisation ; condition ; updation ){
//statement
}
int main(){
for (int i=1;i<=5;i+=1){
printf(“hello world\n”);
}
}
where i stands for iterator or counter
i++ and ++i
i++ similar to i+=1 or i=i+1 increment the value after (post increment operator)
++i increment the value before using the value (pre increment operator)
similarly we have i– and –i (post and pre decrement)
character increment -print characters from a to z using character increment
include<stdio.h></stdio.h>
int main(){
for (char ch=’a’;ch<=’z’;ch++){
printf(“%c\n”,ch);
}
}
Infinite loop using for loop
include<stdio.h></stdio.h>
int main(){
for (int i=1;;i++){
printf(“hello\n”);
}
}
While loop
include<stdio.h></stdio.h>
while (condition) {
// statement
}
int main() {
int i=1;
while (i<=5)
{
printf(“hello\n”);
i++;
}
return 0;
}
Do while loop
include<stdio.h></stdio.h>
do{
//do something
} while (condition);
do first then check condition hence the loop will run 1 time no matter what
int main() {
int i=1;
do {
printf(“%d\n”,i);
i++;
} while (i<=5);
return 0; }
sum of first n natural numbers
include<stdio.h></stdio.h>
int main() {
int i,n,sum;
printf(“enter n :”);
scanf(“%d”,&n);
i=n;
while (i>=1)
{
sum+=i;
i–;
}
printf(“sum is %d”,sum);
}
this is done in reverse
find the sum of n natural number and print these number in reverse order at the same time using while loop and for loop seperately
include<stdio.h></stdio.h>
int main() {
int i=1,n,sum,j;
printf(“enter n :”);
scanf(“%d”,&n);
j=n;
while (i<=n && j>=1)
{
sum+=i;
i++;
printf(“%d\n”,j);
j–;
}
printf(“sum is %d”,sum);
}
int main() {
int n,sum=0;
printf(“enter n :”);
scanf(“%d”,&n);
for(int i=1,j=n;i<=n && j>=1;i++,j–){
sum+=i;
printf(“%d\n”,j);
}
printf(“sum is %d”,sum);
}
break in nested loops
if we use break inside a nested loop then it exits from all the loops
continue statement
skip to next iteration
int main() {
for(int i=1;i<=5;i++) {
if(i==3){
continue;
}
printf(“%d\n”,i);
}
return 0;
}
int main() {
int i = 1;
while (i <= 5) {
if (i == 3) {
i++;
continue;
}
printf(“%d\n”, i);
i++;
}
return 0;
}
Factorial of a number
include<stdio.h></stdio.h>
int main() {
int n,factorial=1;
printf(“enter n :\n”);
scanf(“%d”,&n);
for(int i=1;i<=n;i++) { factorial*=i; } printf("factorial :%d",factorial); return 0;
}
print mn matrix of “”
include<stdio.h></stdio.h>
int main() {
int rows,column,i,j;
char ch=’*’;
printf(“rows :”);
scanf(“%d”,&rows);
printf(“columns :”);
scanf(“%d”,&column);
for(i=1;i<=rows;i++){ for (j=1;j<=column;j++){ printf("%c",ch); } printf("\n"); } return 0;
}
check for prime
include <stdio.h></stdio.h>
int main() {
int n, i, is_prime = 1;
printf(“Enter a positive integer: “);
scanf(“%d”, &n);
if (n <= 1) { is_prime = 0; } else { for (i = 2; i <= n / 2; i++) { if (n % i == 0) { is_prime = 0; break; } } } if (is_prime == 1) { printf("%d is a prime number\n", n); } else { printf("%d is not a prime number\n", n); } return 0; }
prime numbers between a range
include <stdio.h></stdio.h>
int main() {
int start, end, i, j, is_prime;
printf("Enter the start of the range: "); scanf("%d", &start); printf("Enter the end of the range: "); scanf("%d", &end); printf("Prime numbers between %d and %d are:\n", start, end); // Check each number in the range for (i = start; i <= end; i++) { // Assume the current number is prime is_prime = 1; // Check if the current number is divisible by any number from 2 to i/2 for (j = 2; j <= i/2; j++) { if (i % j == 0) { is_prime = 0; break; } } // If the current number is prime, print it if (is_prime == 1 && i != 1) { printf("%d ", i); } } return 0; }