Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
2 views
Module 2
Fyug second semester C programming
Uploaded by
ABDUL MAJEED.K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Module 2 For Later
Download
Save
Save Module 2 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
2 views
Module 2
Fyug second semester C programming
Uploaded by
ABDUL MAJEED.K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save Module 2 For Later
Carousel Previous
Carousel Next
Save
Save Module 2 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 15
Search
Fullscreen
Decision Making with .: Statements in C + Decision-making is an essential concept in programming that allows us to control the flow of execution based on specific conditions. + InC, decision-making statements enable the program to choose different paths based on. conditions + The i statement and its variations (i £-e1se, nested if-else, and else~if ladde2) help us execute different code blocks depending on whether a condition is true (non ero) or false (rero). Cc ‘There are four main types of 1 statements in C: 2. Types of ic Statements 1. Simple if Statement 2. if-else Statement 3. Nested if-else Statement 4. else-it Ladder 3. Simple is Statement Definition + The simplest form of decision-making + Executes a block of code only if the condition is true + If the condition is false, the block is skipped. Syntax Lf (condi // Code to execute if the condit onie true Syntax Explanation * if: The keyword used for decision-making. ‘© condition: A logical expression that evaluates to true (non-zero) of £3 (zero). © Curly Braces (}: Encloses the code block to execute if the condition is true. (Optional for single statements) Example #include
int num if (num 0) “Number is Explanation + um > ois true (since 10 > + The code inside {) executes, and "Number is positive” is printed Output: Nunber is posit 4. is-e1se Statement Definition + Extends the i¢ statement to execute one block if the condition is true, and another if it is false. Syntax iE (condition le to execute if the condition is true ute Lf the condition is false Example int num if (num>=0) { , Explanation * pum >= 0 is false (since -5 >= 0 is false). © block executes, printing "Negative number". Output: Negative nunber 5. Nested ir-eise StatementDefinit © An Le or Lr-elce inside another i¢ or + Used for multiple level checks. Syntax if (condi nt) (condition2) {/ Code if both conditios pelse ( // Code if cond onl is true but condit. istalse // Code if conditionl is false ' Example #nclude
int num =; if (num >=0) { £ (nui Explanation © pum>= 0 is true, © Inside it, non 0 is also true, so "Number is zero" is printed, Definition + Used when multiple conditions need to be checked one after another. «Ifa condition is true, its corresponding block executes and the rest are skipped} elseif (condition2) { // Code for condi ndi tions are true int mark if (mark print } else if (marks >= 80) pEintE ("Grade B\n") 7 } else if (marks >= 70) “Grade C\n") 7 belse ( PEinté ("Grade D\n") 7 b , Explanation e omar 0 is false. © marks >= 30 is false. @ narke >= 10 is true, so "Grade C" is printed Output: Summary Statement Type Purpose Executes When fimpie se [Burra bloc oF code ita sndonis ion is true lLe-e1se Jeuns one block if true, another false [ERE 1 Block: False ~ wise [Nested if-else |ir-cise inside another ti-cise JOne condition inside another le1se-it Ladder|Checks multiple conditions in sequence [First true condition executes 8. Key Points to Remember Always use curly braces {} for multiple statements inside i, else, or ©«Conditions must evaluate to true (non-zero) or false (zero). + The else block (if present) executes only if all conditions are false. «Nested if statements should be properly indented for better readability + The else-ig ladder executes only the first true condition and skips the rest. Switch Statement in C + The switch statement in C is a multi-way decision-making statement. + Itallows one variable to be tested against multiple possible values. + Itis an alternative to multiple if-else statements, making the code more readable and efficient © The switch statement evaluates an expression and executes the block of code that matches a specified case, + Ifno cases match, an optional defautt case is executed Syntax witeh (expre: t ase valuel 7/ Cade to execute Lf expression == valuel break; values 1/ Code to execute if no case matches Syntax Explanation «switch (expression) ion is evaluated first. © It must be an integer, char, or enum (not sat of string), © case value: © Each case represents a possible value for the expression © expression matches value, the corresponding block executes.© break; Stops execution after a case is matched. © If'missing, the execution falls through to the next case. © dofause: © Executes only if none of the cases match. It is optional but recommended. Example 1: Basic switcn Statement #include
mint day = 37 MWeekend\n") 5 Explanation xecutes. o aay= 3,80 ca + Since break; is present, execution stops after "iesinesday" is printed Output: Wednesday 6. Example 2: «wiecn Without preax (Fall-through Behavior) #include
nain()Explanation xecules, ‘+ Since break is missing, execution falls through to the next cases. Output: Two The Other Example 3: switcn With char # ude
char grade= "5"; eturn 0 ' Output: switch vs if-else Feature if-else [Data Type [Works with in [Works with all data types [Multiple [Efficient for checking a single variable [Can handle complex conditions[Conditions [against multiple values \Code Readability |Cleaner and easier to read }Becomes lengthy with multiple conditions WWa-through [Executes next cases if breax is missing _ [N° fallthrough, each condition is separate Loops in C ‘+ Loops are control structures that repeat a block of code multiple times. + They are used when we need to execute a set of statements multiple times with a condition. © Types of Loops in C: 1. while loop 2. do-while loop 3. for loop 4. Nested Loops (Loop inside another loop) 1. wnize Loop Defini nm + The whtLe loop executes repeatedly as long as the given condition is true. + If the condition becomes false, the loop terminates. Syntax while (condition) { /{ Code to execute ' Syntax Explanation # while (condition): The loop runs as long as the condition is true. + Inside (), we write the statements that execute repeatedly. Example #include
nile (i<=5 "ed\n itty // Inc. ent infinite loop2. ao-wnsre Loop Definition * Similar to while, but executes at least once, even if the condition is false. + Condition is checked after executing the loop body Syntax > / Code to execute puhtle (condition); Syntax Explanation + The loop executes once first + Then, it checks the condition. + If true, it repeats; if false, it stops Example ude
peinte(mad\n", 40 bunite ( : y Output Key Difference («nite Vs do-white) Feature while loop do-whiie loop [Condition Checking [Before executing the body [After executing the bod) [Minimum Executions [0 times (if false at start) [At least 1 time4. cor Loop Definition + The most commonly used loop, best for situations where the number of iterations is known. + It includes initialization, condition, and update in a single line. Syntax for (initializat: // Code to exe ) Syntax Explanation nz conditions update) { © Initialization: Runs once before the loop starts. + Condition: Checked before each iteration. + Update: Runs after each iteration. Example hide
Comparison (white vs for) Feature while loop for loop |Use Case |When number of iterations is unknown |When number of iterations is fixed [Syntax [More flexible but longer [Compact and easier to read 5. Nested Loops Definition +A loop inside another loop is called a nested loop.+ Used for working with multidimensional data (like matrices, pattems) Syntax for (initializations conditions update) { for [initializations conditions update / Inner loop code ' i Example: Printing a Star Pattern Hinclude
Mor inti= “pein see) 1 3 5e+) printt ' ' Output ine after inner loop Explanation ‘© Outer loop (i) runs 3 times (rows). «Inner loop (j) runs 5 times per outer loop iteration (columns), 6. Summary Table Loop Type ‘When to Use Execution Condition \while |When condition-based looping is (Runs while the condition is true needed eee [Nie the loop must execute at least |, tes first, then checks conditi leor bing the number of iterations is feeetges dition is false INested |When working with tables, Each inner loop completes for every outer [Loops |matrices, patterns jloop iteration Key Points to Remember [19 Use white when the number of iterations is unknown. (19 Use do-whi1e when the loop must execute at least once. (09 Use for when the number of iterations is known in advance. [19 Nested loops are useful for multidimensional problems but can be inefficient if deeply nested.Jumps in Loops — soroy vreary ANA contine i C + InC, jump statements are used to alter the flow of execution in loops and functions. ‘© The main jump statements in C are: 1. goto — Transfers control to a labeled statement. 2. break — Exits the loop or switch statement, 3. continue — Skips the remaining statements of the current iteration and moves to the next iteration, 1. coto Statement Definition © The goto statement jumps to a labeled statement in the program. + It can transfer control forward or backward within the same function. + Not recommended as it makes code harder to read (considered bad practice). Syntax goto Label: lab 7 Code to execute Syntax Explanation + goto Label; moves the control to Labet : in the program, * label: is a user-defined name followed by a colon (:). Example finclude
for lintiziri if 3) break; // Exit Loop when i peinte("sd\n", i); + Loop breaks when 3, so numbers 3, 4, 5 are not printed 3. continue Statement Defini «The continue statement skips the remaining code in iteration and moves to the next iteration. le the loop for the current + Unlike prea, it does not exit the loop but simply skips the rest of the current iteration Syntax Syntax Explanation © When continues is encountered, the remaining statements in the eurrent iteration areskipped, and the loop moves to the next iteration Example: Skipping a specific iteration tinclude
int main printé ( \n' for (inti itt , printé("\nUsingcontinue:\n") ¢ for (inti=1zi i++) 3) continu med", ids ' Output g break:+ break exits the loop when i == «continue skips iteration i == 3 but continues the loop. 6. Summary Table some, | ‘Fumetion Effect on Execution Use Case jumps to a labeled |Unconditionally jumps to another _ [Avoid unless necessary joto i tatement wart of the code (bad practice) eee Exits the loop of stops loop execution completely a we need to exit continue Skips the current [Moves to next iteration, skipping When we need to skip iteration jremaining statements [certain iterations 7. Key Points to Remember Use break when you want to exit a loop early. C0 Use continue when you want to skip an iteration but keep looping. 1 Avoid goto whenever possible (can make code confusing). 1 Never forget to use proper conditions with break and continue to prevent infinite loops.
You might also like
CnotesUnit2
PDF
No ratings yet
CnotesUnit2
16 pages
UNIT II
PDF
No ratings yet
UNIT II
24 pages
Decision Making Statements
PDF
No ratings yet
Decision Making Statements
32 pages
ITP Week 3
PDF
No ratings yet
ITP Week 3
11 pages
C Language Ch2
PDF
No ratings yet
C Language Ch2
21 pages
Syntax If, Switch, For, While, Do, Break, Continue
PDF
No ratings yet
Syntax If, Switch, For, While, Do, Break, Continue
12 pages
UNIT 2
PDF
No ratings yet
UNIT 2
23 pages
Arrays, Functions
PDF
No ratings yet
Arrays, Functions
46 pages
Unit-2 PSWC
PDF
No ratings yet
Unit-2 PSWC
14 pages
Unit 2 CP
PDF
No ratings yet
Unit 2 CP
37 pages
c_unit_2
PDF
No ratings yet
c_unit_2
26 pages
C Statement
PDF
No ratings yet
C Statement
7 pages
WINSEM2024-25_BECE320E_ETH_VL2024250506209_2025-01-06_Reference-Material-I
PDF
No ratings yet
WINSEM2024-25_BECE320E_ETH_VL2024250506209_2025-01-06_Reference-Material-I
62 pages
Decision Making
PDF
No ratings yet
Decision Making
14 pages
C Programming 3
PDF
No ratings yet
C Programming 3
10 pages
Unit 3control Statements
PDF
No ratings yet
Unit 3control Statements
11 pages
Ip Iy Iis Unit 2
PDF
No ratings yet
Ip Iy Iis Unit 2
13 pages
C-Unit 3
PDF
No ratings yet
C-Unit 3
16 pages
Presented By: M.Yugandhar Cse Department
PDF
No ratings yet
Presented By: M.Yugandhar Cse Department
33 pages
03_PPS
PDF
No ratings yet
03_PPS
60 pages
C Programing
PDF
No ratings yet
C Programing
15 pages
Presentation Decision Making and Branching and Loop 1518762275 277498
PDF
No ratings yet
Presentation Decision Making and Branching and Loop 1518762275 277498
40 pages
Unit-2.1 Control Statements
PDF
No ratings yet
Unit-2.1 Control Statements
58 pages
Unit 2 - Programming in C
PDF
No ratings yet
Unit 2 - Programming in C
22 pages
Programming in C' and Data Structure: Dr. Anand Kumar M.S. Engineering College Bangalore
PDF
No ratings yet
Programming in C' and Data Structure: Dr. Anand Kumar M.S. Engineering College Bangalore
41 pages
test1 (2)
PDF
No ratings yet
test1 (2)
37 pages
3 Control Statements
PDF
91% (11)
3 Control Statements
34 pages
LDP-UNIT-2(BCA-BSC)-Sem-1 (1)
PDF
No ratings yet
LDP-UNIT-2(BCA-BSC)-Sem-1 (1)
43 pages
Lecture Module 3
PDF
No ratings yet
Lecture Module 3
52 pages
Unit 3 SPC . (4)
PDF
No ratings yet
Unit 3 SPC . (4)
51 pages
PPS UNIT II Control Statements
PDF
No ratings yet
PPS UNIT II Control Statements
33 pages
CTSD-CO2
PDF
No ratings yet
CTSD-CO2
32 pages
UNIT - 3 Control Structure
PDF
No ratings yet
UNIT - 3 Control Structure
15 pages
Control Structures - PPTX - 1
PDF
No ratings yet
Control Structures - PPTX - 1
63 pages
L4_Control flow
PDF
No ratings yet
L4_Control flow
6 pages
Unit 3
PDF
No ratings yet
Unit 3
45 pages
Simple If If ..Else Nested If ..Else Else .If
PDF
No ratings yet
Simple If If ..Else Nested If ..Else Else .If
25 pages
Ch-9-Decision Control Looping Statements
PDF
No ratings yet
Ch-9-Decision Control Looping Statements
16 pages
Inbound 5982262989302361480
PDF
No ratings yet
Inbound 5982262989302361480
24 pages
Unit2 Controlstmts1
PDF
No ratings yet
Unit2 Controlstmts1
45 pages
C Unit-2
PDF
No ratings yet
C Unit-2
7 pages
C lecture 2.1
PDF
No ratings yet
C lecture 2.1
35 pages
UNIT 3
PDF
No ratings yet
UNIT 3
15 pages
CP - Unit-II
PDF
No ratings yet
CP - Unit-II
75 pages
CPF_NOTES
PDF
No ratings yet
CPF_NOTES
19 pages
PC UNIT-2 Material
PDF
No ratings yet
PC UNIT-2 Material
66 pages
Unit Ii
PDF
No ratings yet
Unit Ii
24 pages
Chapter 3
PDF
No ratings yet
Chapter 3
52 pages
Branching and Looping
PDF
No ratings yet
Branching and Looping
56 pages
Decision Making
PDF
No ratings yet
Decision Making
44 pages
Unit3 Control Statements
PDF
No ratings yet
Unit3 Control Statements
29 pages
Unit-4 Control Structures
PDF
No ratings yet
Unit-4 Control Structures
48 pages
Unit3CONTROLSTRUCTUREANDLOOPINGpdf__2024_09_18_16_10_41
PDF
No ratings yet
Unit3CONTROLSTRUCTUREANDLOOPINGpdf__2024_09_18_16_10_41
53 pages
Unit-2
PDF
No ratings yet
Unit-2
89 pages
C Programming
PDF
No ratings yet
C Programming
25 pages
c Programming-chapter 05
PDF
No ratings yet
c Programming-chapter 05
40 pages
PPS - Unit 2
PDF
No ratings yet
PPS - Unit 2
57 pages
Amit'
PDF
No ratings yet
Amit'
45 pages
Nested Conditional and Iterative Statements
PDF
No ratings yet
Nested Conditional and Iterative Statements
11 pages
module 3 unit 1,2,3,4
PDF
No ratings yet
module 3 unit 1,2,3,4
18 pages
Module 1
PDF
No ratings yet
Module 1
22 pages
KAS-Prelims-Mains-Syllabus-2025
PDF
No ratings yet
KAS-Prelims-Mains-Syllabus-2025
15 pages
Unit 1 Question
PDF
No ratings yet
Unit 1 Question
11 pages