Technical Training
C TutorialsTable of Contents
C Programming - Decision Making - Branching
C Programming - Decision Making - Branching - Page 2
C Programming - Decision Making - Branching - Page 3C Programming - Decision Making - Branching Page - 3
C Programming - Decision Making - Branching
Nested if Statement
Using “if...else statement” within another “if...else statement” is called ‘nested if statement’. “Nested if statements” is mainly used to test multiple conditions. They can be structured using following syntax:
- If(conditionA)
- {
- Statements
- }
- Else if (conditionB)
- {
- statementB
- }
- Else
- {
- statement
- }
Example #1:
Consider the case that you are asked to program a university marking system which identifies student’s grade on response to user input. Here is how grades should be grouped: 40+ E, 50-59 D , 60-69 C , 70-79 B , 80+ A
Code:
- int Mark;
- printf("Please Enter the grade between 0-100?n");
- scanf("%d",&Mark);
- if(Mark >= 40 && Mark <50)
- {
- printf("your grade is E n");
- }
- else if(Mark>=50 && Mark<60)
- {
- printf("Your grade is D n");
- }
- else if(Mark>=60 && Mark<70)
- {
- printf("Your grade is C n");
- }
- else if(Mark>=70 && Mark<80)
- {
- printf("Your grade is B n");
- }
- else if(Mark>=80 && Mark<90)
- {
- printf("Your grade is A n");
- }else
- printf("you have failed");


Figure 4 code 4 screenshot
Description:
This code checks from lowest boundary and checks for every grade, once one of conditions matches the mark the remaining conditions will be ignored. If none of the conditions match, it will then print out the message “you have failed”.The Switch Statement
“Switch statement” is another type of “conditional statement” used by programmers. Switch is widely used for menus.
- Switch (variable)
- {
- Case 1: statement1 break;
- Case 2: statement 2 break;
- Case 3:statement 3 break;
- .
- .
- .
- Default: default statement break;
- }
Tips:
“break” statement is very important. If it is not used properly all the code after the matched condition will be executed incorrectly. For example: looking at syntax 4; if the break for case 2 was missing, all the codes after and including case 2 would be executed until the next break statement was reached.
Example #1:
This example will cover a very basic character based menu that responds to a , b, c and d; it also warns the user if any other characters are used.
Code:
- char response;
- printf("Please Enter a,b,c or d?n");
- scanf("%c",&response);
- 1:switch(response)
- {
- 2:case 'a': printf("you have inputed 'a'"); break;
- 3:case 'b': printf("you have inputed 'b'"); break;
- 4:case 'c': printf("you have inputed 'c'"); break;
- 5:case 'd': printf("you have inputed 'd'"); break;
- 6:default: printf("unrecognised character"); break;
- }


Figure: 5 simple menu
Description:
“statement 1” indicates that “switch” will be applied on ‘response’. “Statement 2” defines the action needed in case ‘response’ is holding the value of ‘a’. “statement 3,4,5” define action in case ‘response’ is holding value of ‘b’, ’c’ or ‘d’ respectively. “Statement 6” is defining the action needed if none of the cases meet the conditions.
About this tutorial
All codes presented in this tutorial are snippets therefore they will not work unless they are put into acceptable structure. Acceptable structure includes stdio.h library( or any other relevant library) and main() function in the program. All the snippets in this tutorial are tested by Microsoft Visual C++ Express 2010.
C Tutorials
- C Programming - An Overview
- C Programming - Data Types : Part 1
- C Programming - Data Types : Part 2
- C Programming - Constants and Identifiers
- C Programming - Operators
- C Programming - Expressions
- C Programming - Managing Input and Output Operations
- C Programming - Decision Making - Branching
- C Programming - Decision Making - Looping
- C Programming - Arrays
- C Programming - Handling of Character String
- C Programming - Functions (Part-I)
- C Programming - Functions (Part-II)
- C Programming - Structures and Unions
- C Programming - Pointers
- C Programming - Dynamic Memory allocation
- C Programming - Linked Lists
- C Doubly Linked Lists
- C Circular Linked Lists
- C Programming - File management in C
- C Language - The Preprocessor
- Call by Value and Call by Reference
- Concept of Pixel in C Graphics
- TSR in C - An Introduction







