Control Structures
- Flow of program is a sequence.
- Why control structures comes in action, because at certain situation you have to take decisions and work according to condition and Flow of program changes.
- Decision making is process where we see that certain condition is occurred or not ,and to do work according to the condition.
- When a program breaks the sequential flow and jumps to an another piece of the program is called Branching
- Branching is subdivided in Two part, Conditional and Unconditional.
- Conditional Branching
- In conditional Branching the condition is checked first then control is pass to another piece of program.
- example: If, Ternary operator, switch statements.
- Unconditional Branching
- Looping
If-Else
Statement
if Statement:
- It is a Two way decision Statement, which depends upon some condition.
- Condition Evaluates in the form of true or false.
- If condition Evaluates True than, it will
do the work that is written in the block of If statement. - If condition evaluates False than it will
Ignore the block of if statement, and goto to the next step.
Syntax:
if(condition)
{
//block of "if statement"
//it will come in action ,when 'condition' Evaluates to true.
}
if(condition) { //block of "if statement" //it will come in action ,when 'condition' Evaluates to true. }
Lets understand it by an Simple program.
#include<stdio.h>
void main()
{
int var1 = 5;
if(var1 > 4) // '>' greater than is a relational operator
{
// 'condition' is Evaluated to true.
// Now it will execute whatever is written in this block.
printf("%d is greater than 4",var1);
//block of "if statement"
}
}
#include<stdio.h> void main() { int var1 = 5; if(var1 > 4) // '>' greater than is a relational operator { // 'condition' is Evaluated to true. // Now it will execute whatever is written in this block. printf("%d is greater than 4",var1); //block of "if statement" } }
Output:
5 is greater than 4
Example:
#include<stdio.h>
void main()
{
int var1 = 5;
int var2 = 3;
if(var1 > 4) // '>' greater than is a relational operator
{
// 'condition' is Evaluated to true.
// Now it will execute whatever is written in this block.
printf("%d is greater than 4",var1);
//block of "if statement"
}
// another 'if ' block.
if(var1 == 5) //'==' is equal to operator.
{ //which evaluates true if both the operants are same.
printf("var1 is equal to 5");
}
// another 'if' block.
if(var2 < 5)
{
printf("var2 is less than 5");
}
// another 'if' block.
if((var1 > 5) &&( var2 < 5)) //'&&' is a logical 'AND' operator.
{ //which evaluates true only if both the condition are true.
//var1 > 5 : first condition.
//var2 < 5 :second condition.
//for now both the conditions are true.
printf("var1 is greater than 5 and var2 is less than 5");
}
}
#include<stdio.h> void main() { int var1 = 5; int var2 = 3; if(var1 > 4) // '>' greater than is a relational operator { // 'condition' is Evaluated to true. // Now it will execute whatever is written in this block. printf("%d is greater than 4",var1); //block of "if statement" } // another 'if ' block. if(var1 == 5) //'==' is equal to operator. { //which evaluates true if both the operants are same. printf("var1 is equal to 5"); } // another 'if' block. if(var2 < 5) { printf("var2 is less than 5"); } // another 'if' block. if((var1 > 5) &&( var2 < 5)) //'&&' is a logical 'AND' operator. { //which evaluates true only if both the condition are true. //var1 > 5 : first condition. //var2 < 5 :second condition. //for now both the conditions are true. printf("var1 is greater than 5 and var2 is less than 5"); } }
Output:
5 is greater than 4
var1 is equal to 5
var2 is less than 5
var1 is greater than 5 and var2 is less than 5
var1 is equal to 5
var2 is less than 5
var1 is greater than 5 and var2 is less than 5
else
Statement
- If condition in if statement evaluates false, than else block comes in action.
Syntax:
if(condition)
{
// this block will execute if condition evaluates to true.
}
else
{
// this block will execute if above 'if' block condition evaluates to false.
//block of 'else' statement
}
if(condition) { // this block will execute if condition evaluates to true. } else { // this block will execute if above 'if' block condition evaluates to false. //block of 'else' statement }
You will understand this by an example,so lets see an example.
#include<stdio.h>
void main()
{
int var1 = 3;
if(var1 < 4)
{ //this condition will evaluate to false,because 3 is less than 4.
printf("var1 is greater than 4");
//so,this piece of code will not execute.
// and control will go to the else block.
}
else
{
// As above condition evaluated to false.
//control has come to this block, and it will execute this piece of code.
printf("var1 is less than 4");
}
}
#include<stdio.h> void main() { int var1 = 3; if(var1 < 4) { //this condition will evaluate to false,because 3 is less than 4. printf("var1 is greater than 4"); //so,this piece of code will not execute. // and control will go to the else block. } else { // As above condition evaluated to false. //control has come to this block, and it will execute this piece of code. printf("var1 is less than 4"); } }
Output:
var1 is less than 4
Ternary Operator
- This operator works like If-else statements.
-
If your blocks of if-else statement has only one line of code than
for that situation this operator is best. - We can write the above same code in single line.
Syntax:
#include<stdio.h>var
void main()
{
int var1 = 5;
var1 > 4 ? printf("var1 is greater than 4") : printf("var1 is less than 5");
// ':' is barrier between the two possibilities of output
// if 'var>4' evaluates to true ,it will print 'var1 is greater than 4'
// if 'var>4' evaluates to false ,it will print 'var1 is less than 4'
}
#include<stdio.h>var void main() { int var1 = 5; var1 > 4 ? printf("var1 is greater than 4") : printf("var1 is less than 5"); // ':' is barrier between the two possibilities of output // if 'var>4' evaluates to true ,it will print 'var1 is greater than 4' // if 'var>4' evaluates to false ,it will print 'var1 is less than 4' }
Output:
var1 is greater than 4
#include<stdio.h>var
void main()
{
int var1 = 5;
var1 < 4 ? printf("var1 is greater than 4") : printf("var1 is less than 5");
// ':' is barrier between the two possibilities of output
// if 'var>4' evaluates to true ,it will print 'var1 is greater than 4'
// if 'var>4' evaluates to false ,it will print 'var1 is less than 4'
}
#include<stdio.h>var void main() { int var1 = 5; var1 < 4 ? printf("var1 is greater than 4") : printf("var1 is less than 5"); // ':' is barrier between the two possibilities of output // if 'var>4' evaluates to true ,it will print 'var1 is greater than 4' // if 'var>4' evaluates to false ,it will print 'var1 is less than 4' }
Output:
var1 is less than 5
Further Concepts
- What is loops?
- Why Loops?
- Types of Loops
- What is while loop?
- What is do-while loop?
- What is for loops
- break keyword
- continue keyword
- difference between break and keywords
- Nested if-else statement
- Practice Programs
Practice Programs
- Programs Regarding If-else Statements
- Program Regarding nested If - statements
- Programs Regarding loops
Further Topics
- What are Array?
- Why Array?
- Types of Array
- How to declare array ?
- how to Initialize an array?
- What is One Dimensional Array
- Concept of One Dimensional Array
- Concept of Initializing One Dimensional Array
- How to take array elements as input from the User
- What is Two Dimensional Array ?
- Concept of Two Dimensional Array
- How to initialize Two Dimensional Array?
- How to take Elements of Two dimensional array from the User?
- How to display 2D Array As an Table?
- Practice Program Regarding Arrays
- What are keywords?
- What are Identifiers?
- What are data types ?
- What are Variables?
- Constants In C
- Escape Sequences
- different types of Declaring constants
- Keyword 'const'
- Practice Programs
0 Comments
Post a Comment