Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
What do you mean by odd loops in C language?
In C programming language, control statements are used to repeat a set of statements, while conditional statements are used for decision-making. These decision-making statements are also used to determine one or more conditions that decides whether a set of statements should be executed. They are as follows ?
ForĀ loop and while loop, the condition specifies the number of times the loop can be executed.
The "for loop"
The for loop is an entry-controlled loop that executes statements until the specified condition is met. Each element of the for loop is placed inside the parenthesis along with the specified keyword.
for (k = 1; k<=5; k++)
Here, the loop will execute until k<=5, whenever k>5, the control will exit the loop.
So, in this case, the for-loop condition specifies that the loop will execute 5 times.
Example
This C code determines the integer k and uses a for loop to print the values from 1 to 5. This loop runs while k<=5, incrementing k each time.
main() {
int k;
for (k = 1; k <= 5; k++) {
printf("%d", k);
}
}
When the above program is executed, it produces the following output ?
1 2 3 4 5
The "while loop"
The while loop in C is used to evaluate a test condition and iterates over the loop until the specified condition returns true. It is also called a pre-tested loop because it is commonly used for a specified number of iterations.
while (k< = 5)
Here, the loop will execute until k<=5, whenever k>5 the control will exit the loop. So, in this case, the while-loop condition specifies that the loop will execute 5 times.
Example
Following is the C program for the while loop ?
main() {
int k;
k = 1;
while (k <= 5) {
printf("%d", k);
k++;
}
}
The result is generated as follows ?
1 2 3 4 5
What are the Odd Loops in C?
Sometimes a user may not know how many times a loop needs to be executed. If we want to execute a loop for an unknown number of times, the concept of indefinite loops should be implemented. This can be done using for-loops, while-loops, or do-while-loops.
Example: C program for the odd loop
Following is the C program that prompts the user to enter a number, checks if it's even or odd, and asks if we want to test another number. This loop continues until the user specifies '0'.
#include <stdio.h>
int main() {
int number;
number = 1;
while (number == 1) // odd loop don?t know how many times loop executes{
printf("enter a number:");
scanf("%d",&number);
if((number%2)==0)
printf("number is even");
else
printf("number is odd");
printf("do you want to test any number");
printf("if yes then press '1'");// if press 1 loop executes again
printf("else press '0'");//if press 0 exist from loop
scanf("%d",&number);
}
return 0;
}
The result is generated as follows ?
enter a number: 3 number is odd do you want to test any number if yes then press '1' else press '0' 1 enter a number: 4 number is even do you want to test any number if yes then press '1' else press '0' 1 enter a number: 9 number is odd do you want to test any number if yes then press '1' else press '0' 0
