Decision Making
The code to execute based on some condition. This ability is called decision
making and the statements used for it are called conditional statements.
These statements evaluate one or more conditions and make the decision
whether to execute a block of code or not.
Types of Conditional Statements
1. if in C
The if statement is the simplest decision-making statement. It is used to
decide whether a certain statement or block of statements will be executed
or not i.e if a certain condition is true then a block of statements is executed
otherwise not.
A condition is any expression that evaluates to either a true or false
#include <stdio.h>
int main() {
int i = 10;
// If statement
if (i < 18) {
printf("Eligible for vote");
}
}
Output
Eligible for vote
2. if-else in C
The if statement alone tells us that if a condition is true, it will execute a
block of statements and if the condition is false, it won’t. But what if we want
to do something else when the condition is false? Here comes the C else
statement. We can use the else statement with the if statement to execute a
block of code when the condition is false. The if-else statement consists of
two blocks, one for false expression and one for true expression.
#include <stdio.h>
int main() {
int i = 10;
if (i > 18) {
printf("Eligible for vote");
}
else {
printf("Not Eligible for vote");
}
return 0;
}
Output
Not Eligible for vote
3. Nested if-else in C
A nested if in C is an if statement that is the target of another if statement.
Nested if statements mean an if statement inside another if statement. Yes, C
allow us to nested if statements within if statements, i.e, we can place an if
statement inside another if statement.
#include <stdio.h>
int main(){
int i = 10;
if (i == 10) {
if (i < 18)
printf("Still not eligible for vote");
else
printf("Eligible for voten");
}
else {
if (i == 20) {
if (i < 22)
printf("i is smaller than 22 toon");
else
printf("i is greater than 25");
}
}
return 0;
}
Output
Still not eligible for vote
4. if-else-if Ladder in C
The if else if statements are used when the user has to decide among
multiple options. The C if statements are executed from the top down. As
soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the C else-if ladder is
bypassed. If none of the conditions is true, then the final else statement will
be executed. if-else-if ladder is similar to the switch statement.
#include <stdio.h>
int main() {
int i = 20;
// If else ladder with three conditions
if (i == 10)
printf("Not Eligible");
else if (i == 15)
printf("wait for three years");
else if (i == 20)
printf("You can vote");
else
printf("Not a valid age");
return 0;
}
Output
You can vote
5. switch Statement in C
The switch case statement is an alternative to the if else if ladder that can be
used to execute the conditional code based on the value of the variable
specified in the switch statement. The switch block consists of cases to be
executed based on the value of the switch variable.
#include <stdio.h>
int main() {
// variable to be used in switch statement
int var = 18;
// declaring switch cases
switch (var) {
case 15:
printf("You are a kid");
break;
case 18:
printf("Eligible for vote");
break;
default:
printf("Default Case is executed");
break;
}
return 0;
}
Output
Eligible for vote
.
6. Conditional Operator in C
The conditional operator is used to add conditional code in our program. It is
similar to the if-else statement. It is also known as the ternary operator as it
works on three operands.
#include <stdio.h>
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: %dn", var);
return 0;
}
Output
Value of var when flag is 0: 25
7. Jump Statements
A) break
This loop control statement is used to terminate the loop. As soon as the
break statement is encountered from within a loop, the loop iterations stop
there, and control returns from the loop immediately to the first statement
after the loop.
#include <stdio.h>
int main() {
int arr[] = { 1, 2, 3, 4, 5, 6 };
int key = 3;
int size = 6;
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
printf("Element found at position: %d",
(i + 1));
break;
}
}
return 0;
}
Output
Element found at position: 3
B) continue
This loop control statement is just like the break statement. The continue
statement is opposite to that of the break statement, instead of terminating
the loop, it forces to execute the next iteration of the loop.
As the name suggests the continue statement forces the loop to continue or
execute the next iteration. When the continue statement is executed in the
loop, the code inside the loop following the continue statement will be
skipped and the next iteration of the loop will begin.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
// If i is equals to 6,
// continue to next iteration
// without printing
if (i == 6)
continue;
else
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 7 8 9 10
C) goto
The goto statement in C also referred to as the unconditional jump statement
can be used to jump from one point to another within a function.
#include <stdio.h>
int main() {
int n = 1;
label:
printf("%d ", n);
n++;
if (n <= 10)
goto label;
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
D) return
The return in C returns the flow of the execution to the function from where it
is called. This statement does not mandatorily need any conditional
statements. As soon as the statement is executed, the flow of the program
stops immediately and returns the control from where it was called. The
return statement may or may not return anything for a void function, but for a
non-void function, a return value must be returned.
#include <stdio.h>
int sum(int a, int b) {
int s1 = a + b;
return s1;
}
int main()
{
int num1 = 10;
int num2 = 10;
int sumOf = sum(num1, num2);
printf("%d", sumOf);
return 0;
}
Output
20
Looping
Loops in C programming are used to repeat a block of code until the
specified condition is met. It allows programmers to execute a statement or
group of statements multiple times without writing the code again and again.
Types of Loops in C
for Loop
for loop is an entry-controlled loop, which means that the condition is
checked before the loop's body executes.
Syntax
for (initialization; condition; updation) {
// body of for loop
}
The various parts of the for loop are:
● Initialization: Initialize the variable to some initial value.
● Test Condition: This specifies the test condition. If the condition
evaluates to true, then body of the loop is executed. If evaluated
false, loop is terminated.
● Update Expression: After the execution loop’s body, this expression
increments/decrements the loop variable by some value.
● Body of Loop: Statements to repeat. Generally enclosed inside {}
braces.
#include <stdio.h>
int main() {
// Loop to print numbers from 1 to 5
for (int i = 0; i < 5; i++) {
printf( "%d ", i + 1);
}
return 0;
}
Output
1 2 3 4 5
while Loop
A while loop is also an entry-controlled loop in which the condition is
checked before entering the body.
Syntax
while (condition) {
// Body of the loop
}
Only the condition is the part of while loop syntax, we have to initialize and
update loop variable manually.
#include <stdio.h>
int main() {
// Initialization expression
int i = 0;
// Test expression
while(i <= 5) {
printf("%d ", i + 1);
// update expression
i++;
}
return 0;
}
Output
1 2 3 4 5 6
do-while Loop
The do-while loop is an exit-controlled loop, which means that the condition
is checked after executing the loop body. Due to this, the loop body will
execute at least once irrespective of the test condition.
Syntax
do {
// Body of the loop
} while (condition);
Like while loop, only the condition is the part of do while loop syntax, we
have to do the initialization and updating of loop variable manually.
#include <stdio.h>
int main() {
// Initialization expression
int i = 0;
do
{
// loop body
printf( "%d ", i);
// Update expression
i++;
// Condition to check
} while (i <= 10);
return 0;
}
Output
0 1 2 3 4 5 6 7 8 9 10

Decision Making in C Programming Language

  • 1.
    Decision Making The codeto execute based on some condition. This ability is called decision making and the statements used for it are called conditional statements. These statements evaluate one or more conditions and make the decision whether to execute a block of code or not. Types of Conditional Statements 1. if in C The if statement is the simplest decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statements is executed otherwise not. A condition is any expression that evaluates to either a true or false #include <stdio.h>
  • 2.
    int main() { inti = 10; // If statement if (i < 18) { printf("Eligible for vote"); } } Output Eligible for vote 2. if-else in C The if statement alone tells us that if a condition is true, it will execute a block of statements and if the condition is false, it won’t. But what if we want to do something else when the condition is false? Here comes the C else statement. We can use the else statement with the if statement to execute a block of code when the condition is false. The if-else statement consists of two blocks, one for false expression and one for true expression. #include <stdio.h> int main() { int i = 10; if (i > 18) { printf("Eligible for vote"); } else { printf("Not Eligible for vote"); } return 0;
  • 3.
    } Output Not Eligible forvote 3. Nested if-else in C A nested if in C is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, C allow us to nested if statements within if statements, i.e, we can place an if statement inside another if statement. #include <stdio.h> int main(){ int i = 10; if (i == 10) { if (i < 18) printf("Still not eligible for vote"); else printf("Eligible for voten"); } else { if (i == 20) { if (i < 22) printf("i is smaller than 22 toon"); else printf("i is greater than 25"); } } return 0;
  • 4.
    } Output Still not eligiblefor vote 4. if-else-if Ladder in C The if else if statements are used when the user has to decide among multiple options. The C if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. if-else-if ladder is similar to the switch statement. #include <stdio.h> int main() { int i = 20; // If else ladder with three conditions if (i == 10) printf("Not Eligible"); else if (i == 15) printf("wait for three years"); else if (i == 20) printf("You can vote"); else printf("Not a valid age"); return 0; } Output You can vote
  • 5.
    5. switch Statementin C The switch case statement is an alternative to the if else if ladder that can be used to execute the conditional code based on the value of the variable specified in the switch statement. The switch block consists of cases to be executed based on the value of the switch variable. #include <stdio.h> int main() { // variable to be used in switch statement int var = 18; // declaring switch cases switch (var) { case 15: printf("You are a kid"); break; case 18: printf("Eligible for vote"); break; default: printf("Default Case is executed"); break; } return 0; } Output Eligible for vote .
  • 6.
    6. Conditional Operatorin C The conditional operator is used to add conditional code in our program. It is similar to the if-else statement. It is also known as the ternary operator as it works on three operands. #include <stdio.h> 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: %dn", var); return 0; } Output Value of var when flag is 0: 25 7. Jump Statements A) break This loop control statement is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop. #include <stdio.h>
  • 7.
    int main() { intarr[] = { 1, 2, 3, 4, 5, 6 }; int key = 3; int size = 6; for (int i = 0; i < size; i++) { if (arr[i] == key) { printf("Element found at position: %d", (i + 1)); break; } } return 0; } Output Element found at position: 3 B) continue This loop control statement is just like the break statement. The continue statement is opposite to that of the break statement, instead of terminating the loop, it forces to execute the next iteration of the loop. As the name suggests the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin. #include <stdio.h> int main() {
  • 8.
    for (int i= 1; i <= 10; i++) { // If i is equals to 6, // continue to next iteration // without printing if (i == 6) continue; else printf("%d ", i); } return 0; } Output 1 2 3 4 5 7 8 9 10 C) goto The goto statement in C also referred to as the unconditional jump statement can be used to jump from one point to another within a function. #include <stdio.h> int main() { int n = 1; label: printf("%d ", n); n++; if (n <= 10) goto label;
  • 9.
    return 0; } Output 1 23 4 5 6 7 8 9 10 D) return The return in C returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was called. The return statement may or may not return anything for a void function, but for a non-void function, a return value must be returned. #include <stdio.h> int sum(int a, int b) { int s1 = a + b; return s1; } int main() { int num1 = 10; int num2 = 10; int sumOf = sum(num1, num2); printf("%d", sumOf); return 0; } Output 20
  • 10.
    Looping Loops in Cprogramming are used to repeat a block of code until the specified condition is met. It allows programmers to execute a statement or group of statements multiple times without writing the code again and again. Types of Loops in C for Loop for loop is an entry-controlled loop, which means that the condition is checked before the loop's body executes. Syntax for (initialization; condition; updation) { // body of for loop } The various parts of the for loop are: ● Initialization: Initialize the variable to some initial value.
  • 11.
    ● Test Condition:This specifies the test condition. If the condition evaluates to true, then body of the loop is executed. If evaluated false, loop is terminated. ● Update Expression: After the execution loop’s body, this expression increments/decrements the loop variable by some value. ● Body of Loop: Statements to repeat. Generally enclosed inside {} braces. #include <stdio.h> int main() { // Loop to print numbers from 1 to 5 for (int i = 0; i < 5; i++) { printf( "%d ", i + 1); } return 0; } Output 1 2 3 4 5 while Loop A while loop is also an entry-controlled loop in which the condition is checked before entering the body. Syntax while (condition) { // Body of the loop }
  • 12.
    Only the conditionis the part of while loop syntax, we have to initialize and update loop variable manually. #include <stdio.h> int main() { // Initialization expression int i = 0; // Test expression while(i <= 5) { printf("%d ", i + 1); // update expression i++; } return 0; } Output 1 2 3 4 5 6 do-while Loop The do-while loop is an exit-controlled loop, which means that the condition is checked after executing the loop body. Due to this, the loop body will execute at least once irrespective of the test condition. Syntax do { // Body of the loop } while (condition);
  • 13.
    Like while loop,only the condition is the part of do while loop syntax, we have to do the initialization and updating of loop variable manually. #include <stdio.h> int main() { // Initialization expression int i = 0; do { // loop body printf( "%d ", i); // Update expression i++; // Condition to check } while (i <= 10); return 0; } Output 0 1 2 3 4 5 6 7 8 9 10