Types of Conditional Statements in C
1.if in C
Syntax of if Statement
if(condition)
{
// Statements to execute if
// condition is true
}
Flowchart of if Statement
Example of if in C
// C program to illustrate If statement
#include <stdio.h>
int main()
{
int i = 10;
if (i > 15)
{
printf("10 is greater than 15");
}
printf("I am Not in if");
}
2. if-else in C
Syntax of if else in C
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Flowchart of if-else Statement
Example of if-else
// C program to illustrate If statement
#include <stdio.h>
int main()
{
int i = 20;
if (i < 15)
{
printf("i is smaller than 15");
}
else
{
printf("i is greater than 15");
}
return 0;
}
3. Nested if-else in C
Syntax of Nested if-else
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
Flowchart of Nested if-else
Example of Nested if-else
// C program to illustrate nested-if statement
#include <stdio.h>
int main()
{
int i = 10;
if (i == 10)
{
// First if statement
if (i < 15)
printf("i is smaller than 15\n");
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
return 0;
}
4. if-else-if Ladder in C
Syntax of if-else-if Ladder
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Flowchart of if-else-if Ladder
Example of if-else-if Ladder
// C program to illustrate nested-if statement
#include <stdio.h>
int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
5. switch Statement in C
Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
Note: The switch expression should evaluate to either integer or
character. It cannot evaluate any other data type.
Flowchart of switch
Example of switch Statement
// C Program to illustrate the use of switch statement
#include <stdio.h>
int main()
{
// variable to be used in switch statement
int var = 2;
// declaring switch cases
switch (var) {
case 1:
printf("Case 1 is executed");
break;
case 2:
printf("Case 2 is executed");
break;
default:
printf("Default Case is executed");
break;
}
return 0;
}
6. Conditional Operator in C
Syntax of Conditional Operator
(condition) ? [true_statements] : [false_statements];
Flowchart of Conditional Operator
Example of Conditional Operator
// C Program to illustrate the use of conditional operator
#include <stdio.h>
// driver code
int main()
{
int var;
int flag = 0;
// using conditional operator to assign the value to var
// according to the value of flag
var = flag == 0 ? 25 : -25;
printf("Value of var when flag is 0: %d\n", var);
// changing the value of flag
flag = 1;
// again assigning the value to var using same statement
var = flag == 0 ? 25 : -25;
printf("Value of var when flag is NOT 0: %d", var);
return 0;
}
C – Loops
// C program to illustrate need of loops
#include <stdio.h>
int main()
{
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
return 0;
}
for Loop
Syntax:
for (initialize expression; test expression; update expression)
{
//
// body of for loop
//
}
for loop Equivalent Flow Diagram:
// C program to illustrate for loop
#include <stdio.h>
// Driver code
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
printf( "Hello World\n");
}
return 0;
}
While Loop
Syntax:
initialization_expression;
while (test_expression)
{
// body of the while loop
update_expression;
}
Flow Diagram for while loop:
// C program to illustrate
// while loop
#include <stdio.h>
// Driver code
int main()
{
// Initialization expression
int i = 2;
// Test expression
while(i < 10)
{
// loop body
printf( "Hello World\n");
// update expression
i++;
}
return 0;
}
do-while Loop
Syntax:
initialization_expression;
do
{
// body of do-while loop
update_expression;
} while (test_expression);
// C program to illustrate
// do-while loop
#include <stdio.h>
// Driver code
int main()
{
// Initialization expression
int i = 2;
do
{
// loop body
printf( "Hello World\n");
// Update expression
i++;
// Test expression
} while (i < 1);
return 0;
}
Continue Statement in C
Syntax of continue in C
continue;
Flowchart of continue in C
// C program to explain the use
// of continue statement with for loop
#include <stdio.h>
int main()
{
// for loop to print 1 to 8
for (int i = 1; i <= 8; i++) {
// when i = 4, the iteration will be skipped and for
// will not be printed
if (i == 4) {
continue;
}
printf("%d ", i);
}
printf("\n");
int i = 0;
// while loop to print 1 to 8
while (i < 8) {
// when i = 4, the iteration will be skipped and for
// will not be printed
i++;
if (i == 4) {
continue;
}
printf("%d ", i);
}
return 0;
}
// C program to explain the use
// of continue statement with nested loops
#include <stdio.h>
int main()
{
// outer loop with 3 iterations
for (int i = 1; i <= 3; i++) {
// inner loop to print integer 1 to 4
for (int j = 0; j <= 4; j++) {
// continue to skip printing number 3
if (j == 3) {
continue;
}
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Break Statement in C
Syntax of break in C
break;
// C Program to demonstrate break statement with for loop
#include <stdio.h>
int main()
{
// using break inside for loop to terminate after 2
// iteration
printf("break in for loop\n");
for (int i = 1; i < 5; i++) {
if (i == 3) {
break;
}
else {
printf("%d ", i);
}
}
// using break inside while loop to terminate after 2
// iteration
printf("\nbreak in while loop\n");
int i = 1;
while (i < 20) {
if (i == 3)
break;
else
printf("%d ", i);
i++;
}
return 0;
}
// C program to illustrate
// using break statement
// in Nested loops
#include <stdio.h>
int main()
{
// nested for loops with break statement
// at inner loop
for (int i = 1; i <= 6; ++i) {
for (int j = 1; j <= i; ++j) {
if (i <= 4) {
printf("%d ", j);
}
else {
// if i > 4 then this innermost loop will
// break
break;
}
}
printf("\n");
}
return 0;
}
// C program to illustrate
// using break statement
// in Nested loops
#include <stdio.h>
int main()
{
// nested for loops with break statement
// at inner loop
for (int i = 1; i <= 6; ++i) {
for (int j = 1; j <= i; ++j) {
if (i <= 4) {
printf("%d ", j);
}
else {
// if i > 4 then this innermost loop will
// break
break;
}
}
printf("\n");
}
return 0;
}
// C program to demonstrate difference between
// continue and break
#include <stdio.h>
int main()
{
printf("The loop with break produces output as: \n");
for (int i = 1; i <= 7; i++) {
// Program comes out of loop when
// i becomes multiple of 3.
if (i == 3)
break;
else
printf("%d ", i);
}
printf("\nThe loop with continue produces output as: \n");
for (int i = 1; i <= 7; i++) {
// The loop prints all values except
// those that are multiple of 3.
if (i == 3)
continue;
printf("%d ", i);
}
return 0;
}
goto Statement in C
Syntax:
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;
// C program to print numbers
// from 1 to 10 using goto statement
#include <stdio.h>
// function to print numbers from 1 to 10
void printNumbers()
{
int n = 1;
label:
printf("%d ", n);
n++;
if (n <= 10)
goto label;
}
// Driver program to test above function
int main()
{
printNumbers();
return 0;
}