Conditional Statements and Loops
Conditional Statements and Loops
Conditional Statements and Loops
1. For Loop
2. While Loop
3. Do - while Loop
1. If Condition:
The if in C is the most simple decision-making statement. It
consists of the test condition and if block or body. If the given
condition is true only then the if block will be executed.
The general form of simple if ( ) statement is
OR
if(condition)
{
// Statements to execute
// if condition is true
}
// C Program to demonstrate the syntax of if statement
#include <stdio.h>
int main()
{
int num = 9;
// if statement with true condition
if (num < 10) {
printf("%d is less than 10", num);
}
// if statement with false condition
if (num> 20) {
printf("%d is greater than 20", num);
}
return 0;
}
Output:
9 is less than 10
// Program to display a number if it is negative
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0)
{
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
Output 1: Output 2:
Syntax:
if (test expression)
{
true-block statements;
}
else
{
false-block statement;
}
statement-x;
FlowChart
if (condition)
{
// Executes this block
if
// condition is true
}
else
{
// Executes this block
if
// condition is false
}
Example:
// C Program to demonstrate the use of if-else statement
#include <stdio.h>
int main()
{
// if block with condition at the start
if (5 < 10){
// will be executed if the condition is true Output:
printf("5 is less than 10."); 5 is less than
} 10.
// else block after the if block
else {
// will be executed if the condition is false
printf("5 is greater that 10.");
}
return 0;
}
Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
Output:
Enter an integer: 4
4 is an even integer.
/* program to check if the given two numbers are equal or not */
#include <stdio.h>
int main()
{
int num1 , num2;
printf(" Enter the values for Numl and Num2\n");
scanf("%d %d", &numl,&num2);
if(numl = = num2) {
printf("Entered numbers are equal\n");
}
else{
printf("Entered numbers are not equal\n");
}
return 0;
Output:
}
Enter the values for Numl and Num2
2 4
Entered numbers are not equal
3. Nested if-else Condition:
#include <stdio.h>
int main() { Output:
int i;
for (i = 0; i < 10; i++) 0
{ 1
if (i == 4) 2
{ 3
break;
}
printf("%d\n", i);
}
return 0;
}
continue statement
return 0;
}
goto statement
Syntax1 |
Syntax2
----------------------------
------------
goto label; |
label:
.
| .
.
| .
// C program to check if a number is even or not using goto statement
#include <stdio.h>
void EvenOrNot(int num)
{
if (num % 2 == 0)
goto even;
else
goto odd;
even:
printf("%d is even", num);
return;
odd:
printf("%d is odd", num);
}
int main()
{
int num = 26;
checkEvenOrNot(num);
return 0;
}
// C program to print numbers from 1 to 10 using goto statement
#include <stdio.h>
Syntax:
#include <stdio.h>
int main()
{ Output:
int i;
for (i = 0; i < 5; i++) 0
{ 1
printf("%d\n", i); 2
} 3
return 0; 4
}
While Loop
initialization_expression;
while (test_expression)
{
// body of the while
loop
update_expression;
}
The example below will print the numbers 0 to 4:
#include <stdio.h>
int main()
{
Output:
int i = 0;
while (i < 5)
0
{
1
printf("%d\n", i);
2
i++;
3
}
4
return 0;
}
do-while Loop:
This loop will execute the code block once, before checking
if the condition is true, then it will repeat the loop as long
as the condition is true.
Syntax:
initialization_expression;
do
{
// body of do-while loop
update_expression;
}
while (test_expression);
The example below will print the numbers 0 to 4:
#include <stdio.h>
int main() {
int i = 0;
do Output:
{
printf("%d\n", i); 0
i++; 1
} 2
while (i < 5); 3
return 0; 4
}