Branching & Looping

 Branching & Loopin


Decision making with if:- 



In C programming language, the "if" statement is used for decision-making and allows the program to execute a certain block of code based on a specified condition. The basic syntax of the "if" statement is:

if (condition) {
    // code to be executed if the condition is true
}
In this syntax, the "condition" is a logical expression that evaluates to either true or false. If the condition is true, then the code inside the curly braces will be executed. If the condition is false, then the code inside the curly braces will be skipped.

To make the "if" statement shorter, you can use the ternary operator. The ternary operator is a shorthand way of writing an "if-else" statement in a single line. The syntax of the ternary operator is:

(condition) ? expression1 : expression2;

In this syntax, if the condition is true, then "expression1" will be executed. If the condition is false, then "expression2" will be executed. Here's an example:

int a = 5;
int b = (a > 3) ? 10 : 20;
In this example, the condition is "a > 3". If the condition is true, then "b" will be assigned the value of 10. If the condition is false, then "b" will be assigned the value of 20.

if-else:- 

In C language, "if-else" is a conditional statement used for decision making in programming. It allows a program to execute a certain block of code if a condition is true, and another block of code if the condition is false.

The syntax for "if-else" statement in C is as follows:

if (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false }

The "condition" is an expression that evaluates to either true or false. If the condition is true, the code inside the first block of curly braces will be executed, and if the condition is false, the code inside the second block of curly braces will be executed.

Here's an example of how "if-else" statement can be used in C to check whether a number is even or odd:

#include <stdio.h> int main() { int num; printf("Enter an integer: "); scanf("%d", &num); if (num % 2 == 0) { printf("%d is even", num); } else { printf("%d is odd", num); } return 0; }

In this example, the program asks the user to enter an integer, and then checks whether the integer is even or odd using the "if-else" statement. If the integer is even, it prints a message saying that the integer is even, and if the integer is odd, it prints a message saying that the integer is odd.

Switch Statement:-

In C language, the "switch" statement is a control structure used for decision making. It allows a program to select one of several code blocks to execute, based on the value of a given expression or variable.

The syntax for the "switch" statement in C is as follows:

switch(expression) { case value1: // code to be executed if expression matches value1 break; case value2: // code to be executed if expression matches value2 break; . . . case valueN: // code to be executed if expression matches valueN break; default: // code to be executed if none of the values match expression }

The "expression" is a variable or expression whose value will be tested against the cases. Each "case" label specifies a value that the expression will be compared to. If the value of the expression matches one of the values specified in the case labels, the code block associated with that case label will be executed. The "default" label specifies a code block that will be executed if none of the case labels match the expression. Here's an example of how the "switch" statement can be used in C to print the name of a month based on its number:

#include <stdio.h>

int main() {
  int month_number;
  printf("Enter a month number (1-12): ");
  scanf("%d", &month_number);
  
  switch(month_number) {
    case 1:
      printf("January");
      break;
    case 2:
      printf("February");
      break;
    case 3:
      printf("March");
      break;
    .
    .
    .
    case 12:
      printf("December");
      break;
    default:
      printf("Invalid month number");
  }
  
  return 0;
}

GOTO statement:-

The "GOTO" statement is a command in the C programming language that allows the program to transfer control to a labeled statement within the same function. It is generally considered a bad programming practice because it can make the code difficult to understand and maintain. Instead, structured programming constructs such as loops and conditional statements are recommended

While loop:-

A "while" loop is a control flow statement in the C programming language that allows a section of code to be repeated as long as a certain condition is true. The basic syntax of a while loop is as follows:

while (condition) {
    // code to be executed while the condition is true
}

The "condition" is a boolean expression that is evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed. This process is repeated until the condition becomes false, at which point the program continues executing from the line immediately following the end of the loop.

In simple terms, a while loop repeatedly executes a block of code while a certain condition is true.

Do While loop:-

A "do-while" loop is a control flow statement in the C programming language that allows a section of code to be repeated as long as a certain condition is true. The key difference between a do-while loop and a while loop is that a do-while loop always executes the code inside the loop at least once, even if the condition is initially false.

The basic syntax of a do-while loop is as follows:

do {
    // code to be executed at least once
} while (condition);

The code inside the loop is executed first, and then the "condition" is evaluated. If the condition is true, the loop repeats and the code inside the loop is executed again. This process continues until the condition becomes false, at which point the program continues executing from the line immediately following the end of the loop.

In simple terms, a do-while loop is similar to a while loop, but it always executes the code inside the loop at least once before checking the condition.

FOR Loop:-

A "for" loop is a control flow statement in the C programming language that allows a section of code to be repeated for a specific number of times. The basic syntax of a for loop is as follows:

for (initialization; condition; increment) {
    // code to be executed for each iteration
}

The "initialization" is an expression that is executed before the loop starts, typically used to initialize a loop counter variable. The "condition" is a boolean expression that is evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed. The "increment" is an expression that is executed at the end of each iteration, typically used to update the loop counter variable.

In simple terms, a for loop is used to execute a block of code repeatedly for a specific number of times. The loop counter variable is typically used to keep track of the number of iterations and control the loop's behavior.

Break and Continue statements:-

The "break" and "continue" statements are control flow statements in the C programming language that allow a program to modify the behavior of loops.

The "break" statement is used to exit a loop immediately, regardless of whether the loop's condition has been satisfied. When the break statement is encountered, the program immediately exits the loop and continues executing the code immediately following the loop.

The "continue" statement is used to skip the current iteration of a loop and move on to the next iteration. When the continue statement is encountered, the program jumps to the beginning of the loop and starts the next iteration, skipping any code that would normally be executed for the current iteration.

In simple terms, the break statement is used to exit a loop prematurely, while the continue statement is used to skip the current iteration of a loop and move on to the next iteration. These statements can be used to modify the behavior of loops and provide more control over the program's flow.

Array :-

An array in C language is a collection of elements of the same data type, arranged in a contiguous block of memory. Each element in the array can be accessed using an index, which is a non-negative integer that represents the position of the element in the array.

Arrays are useful for storing and manipulating large amounts of data, such as a list of numbers or a string of characters. They can also be used to represent matrices, tables, and other complex data structures.

To create an array in C, you first declare the type of the array elements, followed by the name of the array, and then the size of the array in square brackets. For example, to create an array of 10 integers, you would write:

int myArray[10];

You can then access individual elements of the array using their index. The first element of the array is at index 0, the second element is at index 1, and so on. For example, to assign the value 42 to the third element of the array, you would write:

myArray[2] = 42;

In this example, we use the index 2 to refer to the third element of the array, because array indices in C start from 0, not 1.

Arrays in C are a fundamental data structure that is widely used in programming. Understanding how to use arrays is an important skill for any C programmer.

One dimensional array : -

A one-dimensional array in C is a collection of elements of the same data type that are stored in a contiguous block of memory.

You can create a one-dimensional array by specifying the data type of the elements, followed by the name of the array, and then the size of the array in square brackets. For example, to create an array of 5 integers, you would write:

int myArray[5];

To access individual elements of the array, you use the array name followed by the index of the element you want to access in square brackets. For example, to assign the value 42 to the third element of the array, you would write:

myArray[2] = 42;

In C, array indices start from 0, so the first element of the array is at index 0, the second element is at index 1, and so on.

One-dimensional arrays are useful for storing and manipulating collections of data that can be represented as a list or a sequence, such as a list of numbers or a string of characters. They are a simple and powerful data structure that is widely used in programming.

Two dimensional array:-

A two-dimensional array in C is a collection of elements of the same data type arranged in a rectangular grid or table with rows and columns.

To create a two-dimensional array, you specify the data type of the elements, followed by the name of the array, and then the number of rows and columns in square brackets. For example, to create a 2x3 array of integers, you would write:

int myArray[2][3];

To access individual elements of the array, you use two indices: one for the row and one for the column. For example, to assign the value 42 to the element in the first row and second column, you would write:

myArray[0][1] = 42;

In C, array indices start from 0, so the first row and first column of the array are at index 0, and so on.

Two-dimensional arrays are useful for representing matrices, tables, and other two-dimensional data structures. They are a simple and powerful data structure that is widely used in programming.

Multidimensional array :-

A multidimensional array in C is a collection of elements of the same data type arranged in a multidimensional grid or table with multiple dimensions or axes.

To create a multidimensional array, you specify the data type of the elements, followed by the name of the array, and then the number of dimensions and the size of each dimension in square brackets. For example, to create a 3x4x5 array of integers, you would write:

int myArray[3][4][5];

To access individual elements of the array, you use multiple indices, one for each dimension or axis. For example, to assign the value 42 to the element in the second row, third column, and fourth depth, you would write:

myArray[1][2][3] = 42;

In C, array indices start from 0, so the first row, column, or depth of the array is at index 0, and so on.

Multidimensional arrays are useful for representing complex data structures, such as three-dimensional graphics, scientific simulations, and multidimensional tables. They are a flexible and powerful data structure that is widely used in programming.

Initializing array:-

In C, you can initialize an array at the time of its declaration by specifying a list of values enclosed in curly braces. This is called an array initializer or an array literal.

To initialize a one-dimensional array, you specify the data type of the elements, followed by the name of the array, and then the list of values in curly braces. For example, to create an array of 5 integers and initialize it to the values 1, 2, 3, 4, and 5, you would write:

int myArray[5] = {1, 2, 3, 4, 5};

To initialize a two-dimensional array, you can use nested curly braces to specify the values for each row. For example, to create a 2x3 array of integers and initialize it to the values 1, 2, 3, 4, 5, and 6, you would write:

int myArray[2][3] = {{1, 2, 3}, {4, 5, 6}};

To initialize a multidimensional array, you can use nested curly braces to specify the values for each dimension or axis. For example, to create a 2x3x4 array of integers and initialize it to the values 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12, you would write:

int myArray[2][3][4] = {{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}, {{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}};












Post a Comment

Previous Post Next Post