



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
C programming control Set Instructions
Typology: Essays (university)
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Author: Saurabh Shukla
What is control?
Program is a set of instructions. We know that each instruction of the program is executed by processor. Processor executes instructions one by one. What ever we write in our program will be executed in the same order as they appear in the program. So we can say processor execute instructions in a sequential manner. At a particular instant processor is executing some line of code, we say control of processor is on that line. Processor’s control moves from one line to another. We say this movement of processor control goes in sequence.
Types of control instruction l Sequence control instruction l Decision control instruction l Iterative control instruction l Switch case control instruction l goto instruction
Sequence control instruction Processor control moves in a sequential manner. We have to do nothing to implement sequence control instruction. This is just a concept that your program always run in a sequence.
Decision control instruction Decision control instruction is also known as selection control instruction. As the name implies the job is selection control instruction is to select a set of code for execution on the basis of some condition.
We can implement decision control instruction in three ways: l if l if-else l conditional operator (Ternary Operator)
Syntax of if
main() { ..… ….. if(some condition) { Statement1; Statement2;
Author: Saurabh Shukla
…. } … }
if is a keyword which let compiler to identify decision control instruction. Immediately after if some condition is there. This condition is any valid expression in C. If the result of expression is non-zero it is considered as TRUE otherwise FALSE. Immediately after this condition there is a block of code. Since this block is immediately after if, it is known as if block. Whatever we write in if block will be execute only when condition is TRUE. When condition is false control skip if block and execute statements written after if block. Example: main() { int marks; printf(“Enter marks ”); scanf(“%d”,&marks); if(marks>=33) { printf(“You are PASS”); } if(marks<33) { printf(“You are FAIL”); } } Sample Output:
Enter marks 45 You are PASS
Sample Output:
Enter marks 23 You are FAIL
In this program output depends on the value given by user. Variable marks hold the value entered by user. We have used two if statements. In the first if statement we use the condition marks>=33, thus if the marks are greater than or equal to 33 condition becomes TRUE, so if block executed, otherwise if block is skipped. Whatever may the result of first if condition, control has to reach second if statement. If marks are less than 33 condition will be TRUE and execute if block otherwise if block is skipped.
Author: Saurabh Shukla
The same program was discussed using only if statements. This one is refined version of program and hence better than that of the previous one.
Notice that, there is only one condition need to be evaluated, if the condition is TRUE if block will work otherwise else block will be executed.
Note: If there is only one statement in if block then mentioning block using curly braces is optional. Same rule is applied to else block.
Conditional operator (? :) It is also known as ternary operator which means operator need three operands to perform its operation.
Syntax: Expression1? expression2 : expression3 ;
Expression 1 is a condition, which is first evaluated as TRUE or FALSE. If the condition is TRUE executes expression2 otherwise execute expression3.
Conditional operator works similar to if-else, but we do not have to use keyword if and else.
Example: main() { int x,y; printf(“Enter two numbers”); scanf(“%d%d”,&x,&y); x>y? printf(“%d is greater”,x) : printf(“%d is greater”,y); } Output: Enter two numbers 33 45 is greater
In this program user enters two numbers which is then get stored in x and y. Notice the last line of the program that is conditional operator, which is used to select one from the two printf() statements. If x>y then value of x get printed otherwise value of y is printed.
Selective assignment
main() { int x,y,max;
Author: Saurabh Shukla
printf(“Enter two numbers”); scanf(“%d%d”,&x,&y); max=x>y? x : y; printf(“Greater number is %d”,max); }
Output is: Enter two numbers 100 Greater number is 100
In this program conditional operator is used to select one from x and y to assign value of either x or y in variable max.
If-else ladder
Syntax: if() {
} else if( )
} else if( ) {
} else {
}
Example:
main() { int year; printf(“Enter a year”); scanf(“%d”,&year);
Author: Saurabh Shukla