If statement
If is simplest decision making structure in programming.It produces the boolean result from provided condition or conditions.If boolean result is true then statements will be executed otherwise skipped.A disadvantage is that if condition is false then program will be ended without any output.The syntax of an if statement in programming is provided below.
if(condition or set of conditions)
{
Statements written here will be executed if expression or condition is true
}
Below is an example that will print some string if given condition is true.
#include <stdio.h>
#include <conio.h>
main ()
{
int a = 10;
if( a %2==0 )
{
//if condition is true then execute below statement
printf("Programming is GOOD" );
}
getch();
}
if-else statement
if-else statement works same like an if statement.An improvement over an if statement is that it contains else section that will be executed if provided condition is false.The syntax of an if-else statement in programming is provided below.
Let's take an example that will compare and display the greater number:if(condition or set of conditions) { Statements written here will be executed if expression or condition is true } else { Statements written here will be executed if above condition is false }
#include <stdio.h>
#include <conio.h>
main ()
{
int a = 10;
int b=50
if( a > b )
{
printf("a is greater then b" );
}
else
{
printf("b is greater then a" );
}
getch();
}
if-else-if statement
An if statement can be followed by an optional else-if else statement, which is very useful to test several conditions using single if-else-if statement.When using if , else-if , else statements there are few points to keep in mind:An if can have zero or one else's and it must come after any else-if but mostly after last.
An if can have zero to many else if's and they must come before the else.
Once an if or else-if succeeds, none of the remaining conditions will be tested.
Syntax:
if(condition or set of conditions)
{
Statements written here will be executed if expression or condition is true
}
else if(condition or set of conditions)
{
Statements written here will be executed if expression or condition is true
}
else if(condition or set of conditions)
{
Statements written here will be executed if expression or condition is true
}
else if(condition or set of conditions)
{
Statements written here will be executed if expression or condition is true
}
else
{
Statements written here will be executed if none of the above conditions is true
}
Let's take an example that will display grade based on student marks:
#include <stdio.h>
#include <conio.h>
main ()
{
int marks=45;
if( marks>=80 )
{
printf("A grade");
}
else if( marks>=60)
{
printf("B grade");
}
else if( marks>=40 )
{
printf("C grade");
}
else
{
printf("F grade");
}
getch();
}
After the compilation "C grade" will be printed on screen.Switch structure
A switch was introduced as a best alternative of if-else-if statement.A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.Syntax:
The syntax for a switch statement in programming language is as follows:
switch(variable or expressin){
case constant-value :
// statements here
break;
case constant-value :
// statements here
break;
default : /Optional but good for efficient working
// statements here
}
break:
Break is a keyword in programming that is used in loops or switch statement to terminate current iteration.In switch break keyword is used to skip the remaining cases when a specific condition becomes true.Otherwise remaining statements will be executed automatically.Let's take an example that display's country name based on 'id'. But this time instead of if-else-if switch statement will be used.
#include <stdio.h>
#include <conio.h>
main ()
{
int id=2;
switch(id)
{
case 1 :
printf("China" );
break;
case 2 :
printf("Saudi Arabia" );
break;
case 3 :
printf("Pakistan" );
break;
case 4 :
printf("United states" );
break;
case 5 :
printf("India" );
break;
default :
printf("Invalid id" );
}
getch();
}
Nested Structures
A structure within a structure is known as nested structure or nested statement.if-esle-if statement and switch statement can also be nested.It means we can use the same code to test another inner condition.But it makes program difficult to understand so always use nested statements with carefully.Now we will see an example that will use nested if statement to find out whether all three variables are equal or not.
#include <stdio.h>
#include <conio.h>
main ()
{
int a = 100;
int b = 100
int c = 100;
if( a == b )
{
if( b == c )
{
printf("All variables are equal" );
}
}
else
printf("All variables are not equal");
getch();
}
Now we will check the same example using nested switch statement.
#include <stdio.h>
#include <conio.h>
main ()
{
int a = 100;
int b = 100;
switch(a) {
case 100:
switch(b) {
case 100
printf("Both variables are equal");
break;
default:
printf("Both variabkes are not equal" );
}
}
getch();
}
android
,
C
,
C++