Join Regular Classroom : Visit ClassroomTech

C Programming – CodeWindow.in

Related Topics

C Programing

if (condition)
{
    // Code to be executed if the condition is true
}
The condition in the if statement should evaluate to either true (non-zero) or false (zero). If the condition is true, the code inside the block will be executed. If the condition is false, the code inside the block will be skipped.
Example:
int num = 10;

if (num > 0) {
    printf("The number is positive.\n");
}
  1. if-else Statement: The if-else statement allows you to execute one block of code if the condition is true and another block of code if the condition is false.
Syntax:
if (condition)
{
    // Code to be executed if the condition is true
}
else
{
    // Code to be executed if the condition is false
}
Example:
int num = -5;

if (num > 0) {
    printf("The number is positive.\n");
}
else {
    printf("The number is non-positive.\n");
}
  1. if-else if-else Statement (Multiple Conditions): You can use multiple else if clauses after the initial if statement to check additional conditions. The first matching condition’s block will be executed.
Syntax:
if (condition1)
{
    // Code to be executed if condition1 is true
}
else if (condition2)
{
    // Code to be executed if condition2 is true
}
// ...
else
{
    // Code to be executed if none of the conditions are true
}
Example:
int num = 5;

if (num > 0) {
    printf("The number is positive.\n");
}
else if (num < 0) {
    printf("The number is negative.\n");
}
else {
    printf("The number is zero.\n");
}
  1. switch Statement: The switch statement is used to select one of many code blocks to be executed based on the value of an expression.
Syntax:
switch (expression)
{
    case constant1:
        // Code to be executed if expression matches constant1
        break;
    case constant2:
        // Code to be executed if expression matches constant2
        break;
    // ...
    default:
        // Code to be executed if expression does not match any constant
}
The switch statement compares the value of the expression with the various case constants. If a match is found, the corresponding block of code will be executed. The break statement is used to exit the switch block after the matching case block is executed.
Example:
char grade = 'B';

switch (grade)
{
    case 'A':
        printf("Excellent!\n");
        break;
    case 'B':
        printf("Good job!\n");
        break;
    case 'C':
        printf("You can do better.\n");
        break;
    default:
        printf("Invalid grade.\n");
}
Note that the expression in the switch statement must evaluate to an integer or character type. The case constants must be constants and should not have the same value.
These are the basic syntaxes for decision control statements in C. They allow you to make your programs more flexible and responsive to different conditions and inputs.
if (condition)
{
    // Code to be executed if the condition is true
}
Example:
int num = 10;

if (num > 0) {
    printf("The number is positive.\n");
}
  1. if-else Statement: The if-else statement extends the if statement and provides an alternative block of code to be executed if the condition is false.
Syntax:
if (condition)
{
    // Code to be executed if the condition is true
}
else
{
    // Code to be executed if the condition is false
}
Example:
int num = -5;

if (num > 0) {
    printf("The number is positive.\n");
}
else {
    printf("The number is non-positive.\n");
}
  1. if-else if-else Statement (Multiple Conditions): The if-else if-else statement allows you to check multiple conditions in a sequence and execute different blocks of code based on the first matching condition.
Syntax:
if (condition1)
{
    // Code to be executed if condition1 is true
}
else if (condition2)
{
    // Code to be executed if condition2 is true
}
// ...
else
{
    // Code to be executed if none of the conditions are true
}
Example:
int num = 5;

if (num > 0) {
    printf("The number is positive.\n");
}
else if (num < 0) {
    printf("The number is negative.\n");
}
else {
    printf("The number is zero.\n");
}
  1. switch Statement: The switch statement is used to select one of many code blocks to be executed based on the value of an expression. It is suitable when you have multiple cases to compare against a single expression.
Syntax:
switch (expression)
{
    case constant1:
        // Code to be executed if expression matches constant1
        break;
    case constant2:
        // Code to be executed if expression matches constant2
        break;
    // ...
    default:
        // Code to be executed if expression does not match any constant
}
Example:
char grade = 'B';

switch (grade)
{
    case 'A':
        printf("Excellent!\n");
        break;
    case 'B':
        printf("Good job!\n");
        break;
    case 'C':
        printf("You can do better.\n");
        break;
    default:
        printf("Invalid grade.\n");
}
Each type of decision control statement serves a specific purpose and allows you to control the flow of execution based on different conditions and scenarios in your C programs.

```
#include

int main() {
int age;

printf("Enter your age: ");
scanf("%d", &amp;age);

if (age &gt;= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote yet.\n");
}

return 0;
}
```
In this example, the user is prompted to enter their age. If their age is greater than or equal to 18, the program prints a message saying they’re eligible to vote. Otherwise, the program prints a message saying they’re not eligible to vote yet.

```
switch (expression) {
case constant-expression:
// statements
break;
case constant-expression:
// statements
break;
default:
// statements
}
```
In this statement, the `expression` is evaluated and the value of the expression is used to determine which `case` to execute. Each `case` is associated with a constant expression. If the value of the `expression` matches one of the constant expressions in the `case` statement, the corresponding statements are executed. If none of the `case` statements match, the `default` statement is executed (optional).
Note: The `break` keyword is used to terminate the execution of the `switch` statement after each `case`. If `break` is not used, the execution will continue to the next `case`, regardless of whether the condition is true or not.
Example usage:
```
switch (num) {
case 1:
printf("Number is one");
break;
case 2:
printf("Number is two");
break;
case 3:
printf("Number is three");
break;
default:
printf("Number is not in range");
}
```
In this example, if the value of the `num` variable is 1, the first `case` statement will be executed and “Number is one” will be printed. If the value of `num` is not 1, but 2, the second `case` statement will be executed and “Number is two” will be printed. If the value of `num` is not 1 or 2, but 3, the third `case` statement will be executed and “Number is three” will be printed. If the value of `num` is not within the range of 1-3, the `default` statement will be executed and “Number is not in range” will be printed.
```
int main() {

int num = 10;

if (num &gt; 0) {
printf("Number is positive\n");
if (num % 2 == 0) {
printf("Number is even\n");
} else {
printf("Number is odd\n");
}
} else if (num &lt; 0) {
printf(&quot;Number is negative\n&quot;);
} else {
printf(&quot;Number is zero\n&quot;);
}

return 0;
}
```
In this example, we have a nested if-else statement inside the first if statement. The first if statement checks whether the number is positive, negative or zero. If it’s positive, the inner if-else statement checks whether it’s even or odd. If it’s negative, it prints “Number is negative”. If it’s zero, it prints “Number is zero”.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories