Skip to content

Commit

Permalink
control structures
Browse files Browse the repository at this point in the history
  • Loading branch information
nihalxkumar committed Mar 7, 2024
1 parent 3dba4c2 commit adcbcb7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Learning-C/Break_Continue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Break

`Break` can come in switch or a body of loop. `Break` is used to exit the loop or switch statement.

```c
for (n=1;, n<=500; n+1)
{
for (i = 2; i <= n/2; i++)

{
if (n%i == 0)
break;
}

if (i == n/2 + 1)
printf("%d\n", n);
}
```


# Continue

`Continue` can come only in body of a loop. `Continue` is used to skip the current iteration and continue with the next iteration of the loop by bringing the control at the begining of body of loop.

```c
for (i = 1; i <= 10; i++)
{
if (i == 5)
continue;
if (i == 6)
break;
printf("%d\n", i);
}
```
36 changes: 36 additions & 0 deletions src/Learning-C/Control_Statements_Goto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
jumping to a specific location in the program.

```C
int main() {
int x = 5;
if (x < 0) {
goto error;
}
// Code to be executed when x is positive
return 0;
error:
printf("Error: x is negative\n");
return -1;
}
```

```C
int complex_function(void) {
if (initialize_1() != SUCCESS) { goto out1; }
if (initialize_2() != SUCCESS) { goto out2; }
if (initialize_3() != SUCCESS) { goto out3; }

/* other statements */

return SUCCESS;

out3:
deinitialize_3();
out2:
deinitialize_2();
out1:
deinitialize_1();
return ERROR;
}
```
2 changes: 2 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
- [If-else](./Learning-C/Control_Statements_If-else.md)
- [Switch](./Learning-C/Control_Statements_Switch.md)
- [Loops](./Learning-C/Control_Statements_Loops.md)
- [Break and Continue](./Learning-C/Break_Continue.md)
- [Goto](./Learning-C/Control_Statements_Goto.md)
- [Acknowledgements](./Learning-C/Acknowledgements.md)

---
Expand Down

0 comments on commit adcbcb7

Please sign in to comment.