What is control statements in C?
Control statements are used in C or other programming language to regulate the flow of program execution, i.e. whether or not a set of statements will be executed based on certain conditions or how many times a certain block of cedes is to be executed, again based on condition.
For Example:
/*Control statements in C*/
int x = 2;
int y = 3;
/*If condition is true, this code block will be executed*/
if((x + y) % 5 == 0){
// Do whatever
}
/*if condition is false, this code block will be executed*/
else{
//Do something else
}
-------------------OR----------------------------
int i;
for(i = 0; i < 10; i++){
/* Do anything that you want. This code block will be
* executed until i becomes > or = to 10 */
}
What is if---else statements in C?
The if---else statement in C is used to perform the operation based on some specific condition. The if statement is executed if the statement inside the condition evaluates to true, otherwise the statement inside the else block is executed.
/*Syntax of if---else statement.*/
if(Condition){
// Block-1 statement
}
/*if condition is false, this code block will be executed*/
else{
//Blcok-2 statement
}
What is nested if...else in C?
Nested if...else: Having more than one if...else statement inside the body of another if...else statement is called nested if...else statement.
Syntax | More details.
if(condition) {
//Nested if else inside the body of "if"
if(condition2) {
//Statements inside the body of nested "if"
}
else {
//Statements inside the body of nested "else"
}
}
else {
//Statements inside the body of "else"
}
Waht is if...else...if ladder statement in C?
It is used in the scenario where there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible. It is similar to the switch case statement where the default is executed instead of else block if none of the cases is matched.
Syntax
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
To be updated soon...
--
--
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.