C Conditional Statements (if, if-else, switch) – Beginner’s Guide

Conditional statements in c programming 

C programming lo program execute chestunnappudu oka condition ni check chesi, result batti different statements execute cheyalsina situations chala common ga untayi. Oka condition true aithe oka block of code execute cheyali, false aithe vere block execute cheyali ante conditional statements use chestam.

Conditional statements ni decision-making statements ani kuda antaru. Program ki oka condition based on decision teesukune ability ivvadam veeti main purpose. For example, student marks pass marks kanna ekkuva unnaya, oka number positive aa negative aa, rendu numbers lo edi bigger, user age eligible age kanna ekkuva unda lanti situations lo conditional statements useful ga untayi.

C programming lo basic conditional statements ga if, if-else and nested if statements ni nerchukovachu. Alage multiple conditions kosam else if ladder kuda use chestam.

Conditional statements are used to check the condition.whaether condition is true or not.if condition is true it runs one statement.there are three conditional statements in c programming language.

Ee article lo if statement, if-else statement and nested if concept ni simple explanations and practical C programs tho telusukundam.

Types of Conditional Statements

  • if statement
  • if-else statement
  • else-if ladder
  • nested if statement

1.if statement:

First we are discussing if statement .In c program we are using if statement for checking condition.if condition is true it gives output.if condition does not satisfy it does not print anything..

Syntax for if statement:

if(condition)

{

_________

_________

_________

}

Program using if statement:

#include<stdio.h>

void main()

{

int a;

Printf(“enter a value:”);

Scanf (“%d”,&a);

if(a<5)

{

Printf(“%d\n”,a);

}

}

Output:

enter a value:4

4

Another Example of if Statement

#include <stdio.h>

int main()
{
int age;

printf(“Enter your age: “);
scanf(“%d”, &age);

if (age >= 18)
{
printf(“You are eligible”);
}

return 0;

}

Output:

Enter your age: 20

You are eligible

Ee example lo age 18 or greater aithe condition true ayi message print avutundi.

Explanation:

E program lo a value ani ravataniki printf function vadamu.manam values read cheyataniki scanf function use chesamu.nenu value vachesi 4 ani enter chesanu.Aa tharwatha if statement vadi condition check chesamu.value 4 anedi 5kante thakkuva kabatti 4 ani value print ayyindi.\n is anedi next line lo output print avvataniki a backspace c program lo use chestam.

2.if-else statement:

In c language if else is also called as conditional statements.it checks the condition in if statement if it is true it print the inner program of if .if statement is false it goes to else part of a program.

Syntax for if else statement:

if(condition)

{

________

________

________

}

else

{

_______

________

________

}

Program for if else statement:

#include<stdio.h>

void main()

{

int a,b;

Printf(“enter a value”);

printf(“enter b value”);

scanf(“%d”,&a);

scanf(“%d”,&b);

if(a>b)

{

printf(“a is bigger”);

}

else

{

printf(“b is bigger”);

}

}

output:

enter a value 5

enter b value 6

b is bigger

Another Example of if-else

#include <stdio.h>

int main()
{
int number;

printf(“Enter a number: “);
scanf(“%d”, &number);

if (number % 2 == 0)
{
printf(“Even number”);
}
else
{
printf(“Odd number”);
}

return 0;

}

Output:

Enter a number: 10

Even number

Explanation:

Ikkada % modulus operator use chesi number ni 2 tho divide chesinappudu remainder 0 vastunda ani check chestunnam.

10 % 2 = 0

Kabatti condition true and Even number print avutundi.

Explanation:👇👇👇

Ikkada program lo two values manam read chesam a,b anevi.a>b ane condition ni check chesindi 6nedi peddadhi 5 kante kabatti b is bigger ani vachindi.

3. else-if Ladder

Multiple conditions ni one by one check cheyalsina situation lo else if ladder use chestam.
First if condition check avutundi. Adi false aithe else if condition check chestundi. Adi kuda false aithe next else if unte adi check chestundi. Anni conditions false aithe final else execute avutundi.

Syntax for else-if Ladder

if (condition1)
{
statement;
}
else if (condition2)
{
statement;
}
else
{
statement;
}

Program Using else-if Ladder

#include <stdio.h>
int main()
{
int marks;

printf(“Enter marks: “);

scanf(“%d”, &marks);

if (marks >= 75)
{
printf(“Distinction”);
}
else if (marks >= 60)
{
printf(“First Class”);
}
else if (marks >= 35)
{
printf(“Pass”);
}
else
{
printf(“Fail”);
}

return 0;

}

Output:

Enter marks: 68

First Class

Explanation:

Ee program lo marks ni different conditions tho compare chestunnam.

68 >= 75 false.

Next 68 >= 60 true.

Kabatti First Class print avutundi. Once oka condition true ayyaka remaining else if and else blocks execute avvavu.

4.Nested if else statement:

Neste if else statement nothing but if statement inside another if statement.it checks the if condition if it is true inner program executes.if it is false it goes to inner if statement it checks another condition if it is also false then it goes to else statement.

Syntax for nested if else statement:

if(condition)

{

______

______

else if(condition)

{

______

_______

}

else

{

_____

_____

}

}

Program:

#include<studio.h>

void main()

{

int a,b,c;

printf(“enter a value:”);

printf(“enter b value:”);

Printf (“enter c value:”);

scanf(“%d%d%d”,&a,&b,&c);

if(a>b)

{

printf(“a is bigger”);

}

else if(b>c)

{

printf(“b is bigger”);

}

else

{

printf (“c is bigger”);

}

}

output:

enter a value:4

enter b value:5

enter c value:7

c is bigger.

Explanation:👇👇👇

It takes three values a,b,c.first a>b ane condition ni check chestundi .a condition false aithe else if check chestadi adhi kooda false aithe else statement print avuthadi.

Conclusion

C programming lo conditional statements program ki decision-making ability provide chestayi. Condition ni check chesi result batti different blocks of code execute cheyadaniki veetini use chestam.

if statement single condition true ayinappudu code execute cheyadaniki useful. if-else condition true or false ane two situations ni handle cheyadaniki use chestam. Multiple conditions ni one by one check cheyadaniki else-if ladder useful ga untundi. Oka if statement lopala another if statement use chesinappudu danini nested if antam.

Ee concepts ni simple programs tho practice chesthe C programming lo decision-making logic easy ga understand avutundi. Tarvata numbers comparison, marks calculation, even or odd checking, eligibility checking and other real-world problems lo conditional statements ni apply cheyochu.

2 thoughts on “C Conditional Statements (if, if-else, switch) – Beginner’s Guide”

Leave a Comment