1) Base of hexadecimal number system? Answer : 16 2) Universal gate in digital logic? Answer : NAND 3) Memory type that is non-volatile? Answer : ROM 4) Basic building block of digital circuits? Answer : Gate 5) Device used for data storage in sequential circuits? Answer : Flip-flop 6) Architecture with shared memory for instructions and data? Answer : von Neumann 7) The smallest unit of data in computing? Answer : Bit 8) Unit that performs arithmetic operations in a CPU? Answer : ALU 9) Memory faster than main memory but smaller in size? Answer : Cache 10) System cycle that includes fetch, decode, and execute? Answer : Instruction 11) Type of circuit where output depends on present input only? Answer : Combinational 12) The binary equivalent of decimal 10? Answer : 1010 13) Memory used for high-speed temporary storage in a CPU? Answer : Register 14) Method of representing negative numbers in binary? Answer : Two's complement 15) Gate that inverts its input signal? Answer : NOT 16)...
CONTROL STATEMENTS
Till now we know that the code in the C program is executed sequentially from the first line of the program to its last line. That is, the second statement is executed after the first, the third statement is executed after the second, so on and so forth. Although this is true, in some cases we want only selected statements to be executed. Control flow statements enable programmers to conditionally execute a particular block of code. There are three types of control statements: decision control (branching), iterative (looping), and jump statements. While branching means deciding what actions have to be taken, looping, on the other hand, decides how many times the action has to be taken. Jump statements transfer control from one point to another point.
Decision Control Statements
C supports decision control statements that can alter the flow of a sequence of instructions. These statements help to jump from one part of the program to another depending on whether a particular condition is satisfied or not. These decision control statements include:
(a) if statement, (b) if–else statement,
(c) if–else–if statement, and (d) switch–case statement.
if Statement
if statement is the simplest decision control statement that is frequently used in decision making. The general form of a simple if statement is shown in Fig. below
The if block may include 1 statement or n statements enclosed within curly brackets. First the test expression is evaluated. If the test expression is true, the statements of the if block are executed, otherwise these statements will be skipped and the execution will jump to statement x.
The statement in an if block is any valid C language statement, and the test expression is any valid C language expression that evaluates to either true or false. In addition to simple relational
expressions, we can also use compound expressions formed using logical operators. Note that there is no semi-colon after the test expression. This is because the condition and statement should
be put together as a single statement.
#include <stdio.h>
int main()
{
int x=10;
if (x>0) x++;
printf("\n x = %d", x);
return 0;
}
In the above code, we take a variable x and initialize it to 10. In the test expression, we check if the value of x is greater than 0. As 10 > 0, the test expression evaluates to true, and the value of x is incremented. After that, the value of x is printed on the screen. The output of this program is
x = 11
Observe that the printf statement will be executed even if the test expression is false.
Note In case the statement block contains only one statement, putting curly brackets becomes optional. If there are more than one statement in the statement block, putting curly brackets becomes mandatory.
if–else Statement
We have studied that using if statement plays a vital role in conditional branching. Its usage is very simple. The test expression is evaluated, if the result is true, the statement(s) followed by the
expression is executed, else if the expression is false, the statement is skipped by the compiler.
What if you want a separate set of statements to be executed if the expression returns a false value? In such cases, we can use an if–else statement rather than using a simple if statement.
The general form of simple if–else statement is shown in Fig. Below
In the if–else construct, first the test expression is evaluated. If the expression is true, statement block 1 is executed and statement block 2 is skipped. Otherwise, if the expression is false, statement
block 2 is executed and statement block 1 is ignored. In any case after the statement block 1 or 2 gets executed, the control will pass to statement x. Therefore, statement x is executed in every case.
Write a program to find whether a number is even or odd.
#include <stdio.h>
int main()
{
int a;
printf("\n Enter the value of a : ");
scanf("%d", &a);
if(a%2==0)
printf("\n %d is even", a);
else
printf("\n %d is odd", a);
return 0;
}
Output
Enter the value of a : 6
6 is even
if–else–if Statement
C language supports if–else–if statements to test additional conditions apart from the initial test expression. The if–else–if construct works in the same way as a normal if statement. Its construct is given in Fig. below
Note that it is not necessary that every if statement should have an else block as C supports simple if statements. After the first test expression or the first if branch, the programmer can have as many else–if branches as he wants depending on the expressions that have to be tested. For example, the following code tests whether a number entered by the user is negative, positive, or equal to zero.
#include <stdio.h>
int main()
{
int num;
printf("\n Enter any number : ");
scanf("%d", &num);
if(num==0)
printf("\n The value is equal to zero");
else if(num>0)
printf("\n The number is positive");
else
printf("\n The number is negative");
return 0;
}
Note that if the first test expression evaluates to a true value, i.e., num=0, then the rest of the statements in the code will be ignored and after executing the printf statement that displays ‘The value is equal to zero’, the control will jump to return 0 statement.
switch–case Statement
A switch-case statement is a multi-way decision statement that is a simplified version of an if–else–if block. The general form of a switch statement is shown in Fig.below
The power of nested if–else–if statements lies in the fact that it can evaluate more than one expression in a single logical structure. switch statements are mostly used in two situations:
* When there is only one variable to evaluate in the expression
* When many conditions are being tested for
When there are many conditions to test, using the if and else–if constructs becomes complicated and confusing. Therefore, switch case statements are often used as an alternative to long if
statements that compare a variable to several ‘integral’ values (integral values are those values that can be expressed as an integer, such as the value of a char). Switch statements are also used to handle the input given by the user.
We have already seen the syntax of the switch statement. The switch case statement compares the value of the variable given in the switch statement with the value of each case statement that
follows. When the value of the switch and the case statement matches, the statement block of that particular case is executed.
Did you notice the keyword default in the syntax of the switch case statement? Default is the case that is executed when the value of the variable does not match with any of the values of the case statements. That is, default case is executed when no match is found between the values of switch and case statements and thus there are no statements to be executed. Although the default case is optional, it is always recommended to include it as it handles any unexpected case.
In the syntax of the switch–case statement, we have used another keyword break. The break statement must be used at the end of each case because if it is not used, then the case that matched and all the following cases will be executed. For example, if the value of switch statement matched with that of case 2, then all the statements in case 2 as well as the rest of the cases including default will be executed. The break statement tells the compiler to jump out of the switch case statement and execute the statement following the switch–case construct. Thus, the keyword break is used
to break out of the case statements.
Advantages of Using a switch–case Statement
Switch–case statement is preferred by programmers due to the following reasons:
* Easy to debug
* Easy to read and understand
* Ease of maintenance as compared to its equivalent if–else statements
* Like if–else statements, switch statements can also be nested
* Executes faster than its equivalent if–else construct
Programming Example
Write a program to determine whether the entered character is a vowel or not.
#include <stdio.h>
int main()
{
char ch;
printf("\n Enter any character : ");
scanf("%c", &ch);
switch(ch)
{
case ‘A’:
case ‘a’:
printf("\n %c is VOWEL", ch);
break;
case ‘E’:
case ‘e’:
printf("\n %c is VOWEL", ch);
break;
case ‘I’:
case ‘i’:
printf("\n %c is VOWEL", ch);
break;
case ‘O’:
case ‘o’:
printf("\n %c is VOWEL", ch);
break;
case ‘U’:
case ‘u’:
printf("\n %c is VOWEL", ch);
break;
default: printf("\n %c is not a vowel", ch);
}
return 0;
}
Output
Enter any character : j
j is not a vowel
Note that there is no break statement after case A, so if the character A is entered then control will execute the statements given in case a.