Control statement
As we know that statements in a program executed one by one sequentially. If first statement execute then second statement executed after that third and so on. If we do not want to programs statement execute sequentially then we should use control statement . In case of control statement program execute the particular statement or block. Statement execution depend on the given condition. Basically these are three types of control statement .
1. Decision control statement .
2. Iterative statement (looping).
3. Jump statement.
Decision control statement
In the decision control statement execution of programs statement can be alter. In the program a particular statement can be executed according to given condition.
These statements are :-
1. If statement .
2. If else statement .
3. If else if statement (nested if else).
4. Switch-case statement.
If statement
It is simplest decision control statement in which if condition true then if block executed.
Syntax of if statement :-
if(condition)
{
statement 1;
statement 2;
….…………….
}
statement x;
Diagrammatically :
In if statement when condition is true then if block executed otherwise statement out of if bock executed. In syntax if condition is true then if block’s statements statement 1 and other next statement executed after that out of block all statements executed but when condition is false then if block skip and out of all statements executed. For example :
1.) #include<stdio.h>
int main()
{
int a=10;
if(a==10)
{
printf("hello");
}
printf("word");
return 0;
}
2.) #include<stdio.h>
int main()
{
int a=10;
if(a==5)
{
printf("hello");
}
printf("word");
return 0;
}
In example 1 if condition is true then if block’s statement executed after that out of block statements executed.But in example 2 condition is false so if block skipped after that out of block statements executed.
Suppose there is no block but if condition is given and many statements after condition then what happens. In this case condition is applicable only on first statement .For example:-
1.) #include<stdio.h>
int main()
{
int a=5;
if(a==5)
a=5+7;
printf("%d",a);
printf("%d",a);
return 0;
}
2.) #include<stdio.h>
int main()
{
int a=5;
if(a==10)
a=5+7;
printf("%d",a);
printf("%d",a);
return 0;
}
In the example 1 condition is true so first statement executed and sum of 5 and 7 assigned in variable a after that all statement executed and updated value from 5 to 12 get printed as output 1212. But in case example 2 condition false but keep in mind condition is applicable only first statement so first statement skipped and no value get updated so previous value of variable a printed as output 55.
Notes :
Keep in mind value of variable updated from 5 to 12 when sum of 5 and 7 assigned in variable a.
Give your answers in comment box to given questions that is based on above rules :
1.) #include<stdio.h>
int main()
{
int a=15,b=15,c;
if(a=b)
{
c=a+b;
}
printf("%d",c);
printf("5");
return 0;
}
2.) #include<stdio.h>
int main()
{
int x,y,z=8;
x=5;
y=10;
if(2==5)
z=x+y;
printf("%d",z);
return 0;
}
3.) #include<stdio.h>
int main()
{
int a=2,b=5;
if(a=b)
printf("Suraj");
printf("Neeraj");
return 0;
}
4.) #include<stdio.h>
int main()
{
int a=2,b=5;
if(a==b)
printf("Suraj");
printf("Neeraj");
return 0;
}
5.) #include<stdio.h>
int main()
{
if(5)
{
printf("Suraj");
}
printf("\nSazid");
return 0;
}
Control statement
As we know that statements in a program executed one by one sequentially. If first statement execute then second statement executed after that third and so on. If we do not want to programs statement execute sequentially then we should use control statement . In case of control statement program execute the particular statement or block. Statement execution depend on the given condition. Basically these are three types of control statement .
1. Decision control statement .
2. Iterative statement (looping).
3. Jump statement.
Decision control statement
In the decision control statement execution of programs statement can be alter. In the program a particular statement can be executed according to given condition.
These statements are :-
1. If statement .
2. If else statement .
3. If else if statement (nested if else).
4. Switch-case statement.
If statement
It is simplest decision control statement in which if condition true then if block executed.
Syntax of if statement :-
if(condition)
{
statement 1;
statement 2;
….…………….
}
statement x;
Diagrammatically :
In if statement when condition is true then if block executed otherwise statement out of if bock executed. In syntax if condition is true then if block’s statements statement 1 and other next statement executed after that out of block all statements executed but when condition is false then if block skip and out of all statements executed. For example :
1.) #include<stdio.h>
int main()
{
int a=10;
if(a==10)
{
printf("hello");
}
printf("word");
return 0;
return 0;
}
2.) #include<stdio.h>
int main()
{
int a=10;
if(a==5)
{
printf("hello");
}
printf("word");
return 0;
return 0;
}
In example 1 if condition is true then if block’s statement executed after that out of block statements executed.But in example 2 condition is false so if block skipped after that out of block statements executed.
Suppose there is no block but if condition is given and many statements after condition then what happens. In this case condition is applicable only on first statement .For example:-
1.) #include<stdio.h>
int main()
{
int a=5;
if(a==5)
a=5+7;
printf("%d",a);
printf("%d",a);
return 0;
return 0;
}
2.) #include<stdio.h>
int main()
{
int a=5;
if(a==10)
a=5+7;
printf("%d",a);
printf("%d",a);
return 0;
return 0;
}
In the example 1 condition is true so first statement executed and sum of 5 and 7 assigned in variable a after that all statement executed and updated value from 5 to 12 get printed as output 1212. But in case example 2 condition false but keep in mind condition is applicable only first statement so first statement skipped and no value get updated so previous value of variable a printed as output 55.
Notes :
Keep in mind value of variable updated from 5 to 12 when sum of 5 and 7 assigned in variable a.
Give your answers in comment box to given questions that is based on above rules :
1.) #include<stdio.h>
int main()
{
int a=15,b=15,c;
if(a=b)
{
c=a+b;
}
printf("%d",c);
printf("5");
return 0;
return 0;
}
2.) #include<stdio.h>
int main()
{
int x,y,z=8;
x=5;
y=10;
if(2==5)
z=x+y;
printf("%d",z);
return 0;
return 0;
}
3.) #include<stdio.h>
int main()
{
int a=2,b=5;
if(a=b)
printf("Suraj");
printf("Neeraj");
return 0;
return 0;
}
4.) #include<stdio.h>
int main()
{
int a=2,b=5;
if(a==b)
printf("Suraj");
printf("Neeraj");
return 0;
return 0;
}
5.) #include<stdio.h>
int main()
{
if(5)
{
printf("Suraj");
}
printf("\nSazid");
return 0;
return 0;
}
0 Comments
Any query comment in comment box thank you
Emoji