Control Statements PDF
Document Details
Uploaded by ImpartialRiemann
Shoolini University
Tags
Related
- Chapter - 2.1.pdf
- PPS Important Questions for Mid-1 PDF
- PPS Assignment: Control Statements & Programs (PDF)
- Chapter 8. Statement-Level Control Structures PDF
- Introduction to IT Systems (DES00001) Module 5- Part 2 PDF
- SYSC 2006: Imperative Programming Lecture 3 - Flow of Control (Carleton University)
Summary
This document explains various control statements in programming, including conditional statements (if, else-if, else), branching statements (switch), and looping statements (while, do-while, for). It also covers jump control statements (break, continue, goto) and includes important notes on using these constructs in C code.
Full Transcript
# Control Statements ## Control statement Types - Conditional control statements - Branching statements - Loops ### Conditional control statement - **if(condition)** ``` { -------- -------- code } --- --- ``` ### Branching statements - **switch()** ``` switch(val) { ca...
# Control Statements ## Control statement Types - Conditional control statements - Branching statements - Loops ### Conditional control statement - **if(condition)** ``` { -------- -------- code } --- --- ``` ### Branching statements - **switch()** ``` switch(val) { case 1: code 1; case 3: code 3; case 25: code 25 } ``` - **1.** After matching any case label, it will execute rest all the below case labels code. - ****case label must be constant type. - **constant/exp** - **int/char/enum** type of constant. - **case a: --> invalid** **Valid Cases** - case 3:valid - case 2*6:valid - case 's':valid - case "s": valid - case _ray.hope:valid - case printf("_ray.hope"): valid **Invalid Cases** - case 1+2.4:invalid - case a: --> invalid - **else** - else is not mandatory and else will always be executes only if the cond is false in if(cond) - **if-else-if Ladder** - we can't write any code in between if-else-if ladder. ### Loops - **while() loop** - while/entry controlled/pre-test loop ```c //code while(condition) { --------- --------- update aux_var; } ``` - **while(1)** ```c //code while(1) { code; } ``` - infinite loop - **while(0)** ```c //code while(0) { code -->dead loop } ``` - dead loop - **do-while loop/exit control/post-test** ```c //code do { code; update aux-var; } while(condition); ``` - note** if the condition is false still loop will run one time. - **do** ```c //code do { --- }while(1) ``` - do while(1) --> infinite loop - **for loop** ```c //code for(intialization;condition;update) { ------code } ``` - **for(i=0; i++;)** --> infinite loop - by default inside for loop cond is always true. - **-->** you can skip any part in for loops. - **but by default condition is always true.** - **for()** --> error - **for(;;)** --> infinite loop - **for(i=0;i<5;i++)** ```c //code for(i=0;i<5;i++) { code1; --> code1, code 2 exe 5times code 2; } ``` - **for(i=0;i<5;i++)** ```c //code for(i=0;i<5;i++) { code1; } ``` - code1 exe 5times, code2 exe 1 time - **for(i=0;i<5;i++);** --> no body for this loop - **-->** loop will run 5 time but doing nothing ```c //code for(i=0;i<5;i++); { code1; --> code1, code 2 exe 1 times code 2; } ``` - **case labels can also be range type ** - **case a ... b:** --> it will check all values from a to b. ## Jump Control Statements - **break:** control will come out of loops/switch body - **continue:** it will skip the current iteration - **goto:** goto transfers the control to the given label; # Miscellaneous Notes - **default case is not mandatory** but if you are writing you can place default case anywhere inside a switch block. - **duplicate case labels are not allowed**. - **in switch it will only match with case labels**. If there is any other code written instead of case labels, compiler never execute that code. This is called unreachable code. - **switch(1)** is valid. - **switch(a+b)** is valid. - **switch(f)** is invalid. - **switch(5+1.1)** is invalid. - **switch(c)** is valid. - **switch(a>b)** is valid. - **switch(a&b)** is valid. - **switch(a+b+c+f)** is invalid.