C Programming
C Programming
1: if
2: switch.
1. if statement.
The if statement controls conditional branching. The body of an if statement is executed if the
value of the expression is nonzero. Or if statement is used to execute the code if condition
is true. If the expression/condition is evaluated to false (0), statements inside the body of if is
skipped from execution.
Syntax : if(condition/expression)
{
true statement;
}
statement-x;
If the condition/expression is true, then the true statement will be executed otherwise the true
statement block will be skipped and the execution will jump to the statement-x. The „true
statement‟ may be a single statement or group of statement.
Example:
main()
{
int a=15,b=2;
if(b>a)
{
printf("b is greater");
}
}
Output: b is greater.
Syntax : if (condition)
{
true statement;
}
else
{
false statement;
}
statement-x;
If the condition is true , then the true statement and statement-x will be executed and if the
condition is false, then the false statement and statement-x is executed.
Or
If test expression is true, codes inside the body of if statement is executed and, codes inside
the body of else statement is skipped.
If test expression is false, codes inside the body of else statement is executed and,
codes inside the body of if statement is skipped.
Example:
// Program to check whether an integer entered by the user is odd or even
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
// True if remainder is 0
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: 7
7 is an odd integer.
Example
#include<stdio.h>
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 !=var2)
{
printf("var1 is not equal to var2");
//Below – if-else is nested inside another if block
if (var1 >var2)
{
printf("var1 is greater than var2");
}
else
{
printf("var2 is greater than var1");
}
}
Else
{
printf("var1 is equal to var2");
}
Else if ladder.
The if else-if statement is used to execute one code from multiple conditions.
Syntax : if( condition-1)
{
statement-1;
}
else if(condition-2)
{
statement-2;
}
else if(condition-3)
{
statement-3;
}
else if(condition-n)
{
statement-n;
}
else
{
default-statement;
}
statement-x;
Example:
#include <stdio.h>
int main()
{
int A, B, C;
printf("Enter three numbers: ");
scanf("%d %d %d", &A, &B, &C);
if (A >= B) {
if (A >= C)
printf("%d is the largest number.", A);
else
printf("%d is the largest number.", C);
}
else {
if (B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
}
return 0;
}
#include <stdio.h>
int main()
int A, B, C;
else
return 0;
Output:
Enter the numbers A, B and C: 2 8 1
8 is the largest number.
3. Using Ternary operator to find the largest number.
#include <stdio.h>
int main()
{
int A, B, C, largest;
return 0;
}
2. SWITCH
Switch statement : when there are several options and we have to choose only one
option from the available ones, we can use switch statement. Depending on the
selected option, a particular task can be performed. A task represents one or more
statements.
Syntax:
switch(expression)
{
case value-1:
statement/block-1;
break;
case value-2:
statement/block t-2;
break;
case value-3:
statement/block -3;
break;
case value-4:
statement/block -4;
break;
default:
default- statement/block t;
break;
}
Rules for writing switch() statement.
1 : The expression in switch statement must be an integer value or a character constant.
2 : No real numbers are used in an expression.
3 : The default is optional and can be placed anywhere, but usually placed at end.
4 : The case keyword must terminate with colon ( : ).
5 : No two case constants are identical.
#include<stdio.h>
main()
{
int a;
printf("Please enter a no between 1 and 5: ");
scanf("%d",&a);
switch(a)
{
case 1:
printf("You chose One");
break;
case 2:
printf("You chose Two");
break;
case 3:
printf("You chose Three");
break;
case 4:
printf("You chose Four");
break;
case 5: printf("You chose Five.");
break;
default :
printf("Invalid Choice. Enter a no between 1 and 5"); break;
}
}