0% found this document useful (0 votes)
6 views

C Programming c2

The document discusses various constructs for decision making and looping in C programming. It provides examples of if/else statements, else if ladders, switch statements, ternary operators, while loops, do-while loops, for loops, and ways to break, continue, or jump within loops using break, continue, and goto statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C Programming c2

The document discusses various constructs for decision making and looping in C programming. It provides examples of if/else statements, else if ladders, switch statements, ternary operators, while loops, do-while loops, for loops, and ways to break, continue, or jump within loops using break, continue, and goto statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Certainly!

Here are examples of each in C programming:

**Decision Making:**

1. **Simple if statement:**

```c

if (condition) {

// Code to execute if the condition is true

```

2. **if...Else statement:**

```c

if (condition) {

// Code to execute if the condition is true

} else {

// Code to execute if the condition is false

```

3. **Nesting of if...Else statement:**

```c

if (condition1) {

if (condition2) {

// Code to execute if both conditions are true


} else {

// Code to execute if condition2 is false

} else {

// Code to execute if condition1 is false

```

4. **Else if ladder:**

```c

if (condition1) {

// Code to execute if condition1 is true

} else if (condition2) {

// Code to execute if condition2 is true

} else {

// Code to execute if all conditions are false

```

5. **Switch-Case statement:**

```c

switch (expression) {

case value1:

// Code to execute if expression equals value1

break;
case value2:

// Code to execute if expression equals value2

break;

default:

// Code to execute if expression doesn't match any case

```

6. **? : Operator:**

```c

result = (condition) ? value_if_true : value_if_false;

```

**Decision Making and Looping:**

1. **While statement:**

```c

while (condition) {

// Code to repeat as long as the condition is true

```

2. **Do statement:**

```c

do {
// Code to execute at least once

} while (condition);

```

3. **For statement:**

```c

for (initialization; condition; iteration) {

// Code to repeat until the condition is false

```

4. **Jumps in loops:**

- **Break:**

```c

while (condition) {

// Code

if (some_condition) {

break; // Exits the loop prematurely

// More code

```

- **Continue:**

```c
for (int i = 0; i < 10; i++) {

if (i == 5) {

continue; // Skips the rest of the loop and moves to the next iteration

// Code to execute for each iteration

```

- **Goto statement:**

```c

for (int i = 0; i < 5; i++) {

// Code

if (some_condition) {

goto label; // Jumps to a labeled section of code

// More code

label:

// Code to be executed after the goto statement

```

You might also like