Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views9 pages

Conditional and Iretation

The document provides an overview of conditional statements and iteration statements in C programming, detailing types such as if-else, switch, and various loop structures like for and while. It includes examples and practice tests to reinforce understanding of how to implement these constructs in code. The document emphasizes the importance of these statements for decision-making and repetitive tasks in programming.

Uploaded by

agamerz341
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
0% found this document useful (0 votes)
14 views9 pages

Conditional and Iretation

The document provides an overview of conditional statements and iteration statements in C programming, detailing types such as if-else, switch, and various loop structures like for and while. It includes examples and practice tests to reinforce understanding of how to implement these constructs in code. The document emphasizes the importance of these statements for decision-making and repetitive tasks in programming.

Uploaded by

agamerz341
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
You are on page 1/ 9
Conditional] Statements What is Conditional Statement? © Conditional statements in C programming constructs that allow a program to execute different blocks of code based on whether a certain condition is true or false. Types of Conditional Statement oi ifelse ifelse ladder © nested if else © switch) It allows a program to evaluate ifa given condition is true or false and execute a black of code accordingly. if condition is true then execute if block otherwise go to the rest code, Example: PoC r cae © Ittests a condition: if the condition is true, one black of cade is executed, and if the condition is false, another black is executed Example: Sena rc ee aS Cras { SO e UG ee Temi Ce Nested if else Nested else-if statements in C are a way to create more decision-making structures in your code. They involve using if statements within the branches of other if or else statements. Example: io Sem sur ae eat an) ie BC staan ve If else Ladder ‘© if we want to check the multiple conditions then ladder if else can used. Ifthe previous condition returns false then only next candition will be checked. Syntax: Example: write a program that take a number from user. And display output according to given conditions: If number greater 100 then display “invalid marks” © else if number greater 95 then display “super” else if number greater 80 then display “excellent” else if number greater 60 then display “average” else if number greater 34 then display “just passed” Otherwise, display “you are failed! noreoy sys Pistia peer Coun 4 srry 4 etree tea) (num 24) [Goel eee The switch statement in C is a contral flow structure that allows for the selection of code blocks to execute based on the value of a specified expression. The switch statementin € requires the expression to be of an integral data type, such as int, char, short, long, or ‘an enumeration type (enum) Example?: write a program that take 3 number frem user. if numbers 1 then display: “first number”. If numberis2 then display: “second number" If number is 5 ther display: “fith number". Otherwise display: “invalid number’ Ta CoTyexs or eee eee roe F Parad Pare Pea cee tas a Petcenreres ECR Practice Test 1) Write a program that take two number from user. and display greater number. 2} Write a program that take 5 subject marks from user. and calculate average of 5 subject. And apply some conditions Hfaverage greater 80 then display “Excellent”. else if average greater 60 then display “Good” else if average greater 50 then display “average”. else if average greater 34 then display “just pass”. else display “you are failed” hint: using if else ladder. 3} Write a program that input a number from user. and according to the number take decision like: Hf number equal to 1, then take two value from user. and display sum. else if number equal to 2, then take two value from user. and display multiplication, else if number equal to 3, then take two value from user and calculate modulus and. display modulus Otherwise display: “invalid number, please enter carrect number”. hint: using switch case, 4) Write a program that takes two numbers from user. and swap values. And display values Example: if two variable x any y. Before Swap X=10; ¥=20; ‘After Swap X=20; ¥=10; 5) Write a program that take 3 numbers from user and display greater of numbers. Iteration Statemen What is Iteration Statements? ‘+ Loops in Care used to execute the block of code several times according to the condition given in the loop. © Itmeans it executes the same code multiple times so it saves code and also helps to traverse the elements of an array. Types of Loops ‘+ Entry Control Loop or (Pre Tested Loop) while © for ‘+ Exit Control Loop or (Post Tested Loop) © dowhite Entry Control Loop ‘+ Imentry controlled loops, the test condition is evaluated before entering the loop body '# The for Loop and the while Loop are examples of entry-controlled loops. while loop pererey main(){ (crest Example 1 prerea yy CTULOTe omy print#("\n Hi "); x Peer Pierre p Fe errors mene Path while (xe=50){ er for loop ‘+ The "For" loop is used to repeat a specific piece of code in a program until a specific condition is satisfied + The for loop statement is very specialized. We use a for loop when we already know the number of iterations of that particular piece of code we wish to execute. * The for loop statement is very specialized. We use a for loop when we already know the number of iterations of that particular piece of code we wish to execute. Pee ete) © initialize counter: It will initialize the loop counter value. It is usually i=0. © test counter: This is the test condition, which if found true, the laop continues, otherwise terminates. + tncrement/decrement counter: Incrementing or decrementing the counter * Set of statements: This is the body or the executable part of the for loop or the set of statements that has to repeat itself. Example 1: write a c program that display number from 1 to 20, Pets Gane are) Exit Control Loop ‘+ In exit-controlled loops, the test condition is tested at the end of the loop. ‘* Regardless of whether the test condition is true or false, the loop bady will execute at le ‘* The do-while loop is an example of an exit-controlled loop. do while loop ree (test_ condition); Perrier main() eta ee ReneS Example 2: (SST Ena A CoG 1C3) ue Statement Break statement is used to break the logp or switch case statements execution and brings the control to the, next block of code after that particular loop or switch case it was used in, ‘* Break statements are used to bring the program control out of the loop it was encountered in. © The break statement is used inside loops or switch statements in C Language. Example 1: perry cr Urey Continue: ‘The continue statement is used inside loops in C Language. When a continue statement is encountered inside the loop, the control jumps to the beginning of the loop for the next iteration, skipping the execution of statements inside the body of the loop after the continue statement. © Itis used to bring the control to the next iteration of the loop. ‘Typically, the continue statement skips some code inside the loop and lets the program move on with the next iteration, Perc rcery Practice Test 1) write a program that take a number from user. And display, starting from 1 and end from that number that entered by user. 2) write a program that take a number from user. And display their table. 3) Write a C program to print all even numbers between 1 to 100. 4) Write a C program to print all odd number between 1 to 100. 5) Write a C program to find sum of all natural numbers between 1 to n. and n enter by user.

You might also like