Control Structures Pgms
Control Structures Pgms
: 4 Date: Objective:
C language includes a wide variety of powerful and flexible control statements. Nature of the loop or control structure used determines the number of times the statements (or code block) are executed, if executed at all.
Back ground / Pre requisite: C language possesses such decision making capabilities and supports the following statements known as control or decision-making statements. 1. 2. 3. 4. if statement switch statement Conditional operator statement goto statement
ifelse Statement The if statement is a powerful decision making statement and is used to controlthe flow of execution of statements. It is basically a two-way decision statement and is used in conjunction with an expression. Syntax if (conditional) { block of statements executed if conditional is true; } else { block of statements if condition false; } Switch Statement: The switch and case statements help control complex conditional and branching operations. The switch statement transfers control to a statement within its body.
Syntax: Syntax switch (expression) { case item: statements; break; case item: statements; break; case item: statements; break; default: statement; break; }
Loops are used to repeat one statement or set statements more than one time. Most real programs contain some construct that loops within the program, performing repetitive actions on a stream of data or a region of memory. There are several ways to loop in C. For Loop For loop is a counter loop. The for loop allows automatic initialization of instrumentation of a counter variable. Syntax for (initialization; condition; increment/decrement) { statements block; } While Loop The while loop repeats a statement until the test at the top proves false. For Loop While Loop Do-While Loop
Do-while loop This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable. Syntax do { statement block; } while(condition);
Choosing the proper control statements: Picking the appropriate control structure will ensure the proper operation of the software and aid in the readability of the software. The use of a properly developed flowchart can aid in the selection of the control structure. Each control structure has its own unique program flow which is evident in a flowchart. Control structures have the common element of a decision located at some point in the flowchart. The location of the processes associated with the decision aid in the identification of the structure. In other words, the relationship of the processes and decision determine the type of control statement. Another key element in the identification of the control structure is the flow through the processes.
To write a program to find Biggest among three numbers using if-else statement. Problem Statement:
Pseudo Code/Algorithm:
Test Case:
Review Questions:
1) Which among the following is a unconditional control structure
a) do-while
b) if-else
c) goto
d) for
a) if..then
b) ++
c) ?
d) ()
To write a program to do Arithmetic Operation using Switch Case statement. Problem Statement:
Pseudo Code/Algorithm:
Test Case:
2. Which of the following cannot be checked in a switch-case statement? A.Character C.Float B.Integer D.enum
To write a program to reverse the given integer using while loop. Problem Statement:
Step 2: Enter the number, N to be reversed Step 3: While the number, N is not less than zero do 3.1 R = Find the remainder of N divided by 10 3.2 SUM = Multiply SUM by 10 and add to R 3.3 N = Find the Quotient of N divided by 10 Step 4: Print the value of SUM, which is the reversal of a given number. Test Case :
Review Questions:
1. C supports how many basic looping constructs
a) 2
b) 3
c) 4
d) 4
a) ;
b) :
c) ,
d)
To write a program to generate Armstrong numbers using do while loop. Problem Statement:
Pseudo Code/Algorithm
Step 1: Get limit of the generation N Step 2: Do the following for N times Step 2.1: SUM = sum of cubes of each digit of original number Step 2.2: If original number and SUM is same, then print the original Number.
Test Case
Review Questions: 1. Why we use do-while loop in c? Also tell any properties which you know ?
a)1
b)0
c)-1
d)NULL
Fibonacci series
To write a program to generate Fibonacci series using for loop. Problem Statement:
Pseudo Code/Algorithm
Step 1: Get limit of the generation N Step 2: If limit of the generation N is equal to Zero Then print zero Step 3: print zero and one Do the following for 2 to N times Step 3.1: C=sum of first and second First=second Second =c Step 3.2: Print the Fibonacci Series Test Case :
Review Questions:
1. Why n++ executes faster than n+1?
2. Differentiate between a for loop and a while loop? What are it uses?