American International University-Bangladesh (AIUB)
Faculty of Science & Technology
Department of Computer Science
LAB MANUAL 03
CSC1102 Programming Language 1 [EEE]
TITLE
A Brief Introduction to Decision Control Structures and Scope of Variables in C
PREREQUISITE
• To know about the C operators
• To know about C variables
• To be able to use printf() and scanf()
OBJECTIVE
• To know about If statement
• To know about If else statement
• To know about nested If else
• To know about else if clause
• To know about local variable and global variable
• To know about scope of variable
• To be able to solve the exercises, lab work and assignments at the end of this manual
THEORY
DECISION CONTROL STRUCTURE
In the start of learning C, We use sequence control structure, in which various steps are executed
sequentially i.e. in the same order in which they appear in the program. In fact, to execute the
instructions sequentially, we don't have to do anything at all. By default, instructions in a program
are executed sequentially. However, in serious programming situations, seldom do we want the
instructions to be executed sequentially. Many a times we want a set of instructions to be
executed in one situation and an entirely different set of instructions to be executed in another
situation. This kind of situation is dealt in C programs using a decision control instruction.
Decision control instruction can be implemented in C using:
• The if statement
• The if-else statement
• The nested if-else
• The else if clause
The if, if-else, nested if-else and else if clause are used to make one-time decisions in C
Programming, that is, to execute some code/s and ignore some code/s depending upon the test
expression.
THE IF STATEMENT
The if statement checks whether the text expression inside parenthesis () is true or not. If the
test expression is true, statement/s inside the body of if statement is executed but if test is
false, statement/s inside body of if is ignored. Syntax
if (test expression) {
statement/s to be executed if test expression is true;
}
Flowchart
Below is a C program to print the number entered by user only if the number entered is
negative.
#include <stdio.h>
int main()
{
int num;
printf("Enter a number to check.\n");
scanf("%d",&num);
if(num<0) {
printf("Number = %d\n",num);
}
printf("The if statement in C programming is easy.");
return 0;
}
THE IF-ELSE STATEMENT
The if-else statement is used if the programmer wants to execute some statement/s when the
test expression is true and execute some other statement/s if the test expression is false.
Syntax
if (test expression) {
statements to be executed if test expression is true;
}
else {
statements to be executed if test expression is false;
}
Flowchart
Below is a C program to check whether a number entered by user is even or odd.
#include <stdio.h>
int main()
{
int num;
printf("Enter a number you want to check.\n");
scanf("%d",&num);
if((num%2)==0)
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
}
THE NESTED IF –ELSE
Nested if-else statement is same like if else statement, where new block of if else statement is
defined in existing if or else block statement. It is used when user want to check more than one
conditions at a time.
Syntax
if(condition is true)
{
if(condition is true)
{
statement;
}
else
{
statement;
}
}
else
{
statement;
}
Below is a C program that checks the username and password of a user.
#include <stdio.h>
void main()
{
char username;
int password;
printf("Username:");
scanf("%c",&username);
printf("Password:");
scanf("%d",&password);
if(username=='a'){
if(password==12345){
printf("Login successful");
}
else{
printf("Password is incorrect, Try again.");
}
}
else{
printf("Username is incorrect, Try again.");
}
}
THE ELSE IF CLAUSE
The if-else statements are too complicated for humans to write when they are nested more
deeply. To reduce this complication of statements else if clause can be used.
Syntax
if(test expression1 is true)
statement1;
else if(test expression2 is true)
statement2;
else if(test expression3 is true)
statement3;
The else if clause is the combination of if else statement. At the start if test expression1 of if
statement evaluates false, then the program control moves downward till any of the else if
statement evaluates true. After evaluating true any of the else if statement it executes the
statements and exits the clause.
Below is a C program that checks whether an input year is leap year or not.
#include <stdio.h>
void main()
{
int year;
printf("Enter the year:");
scanf("%d",&year);
if(year%400==0){
printf("It is a leap year");
}
else if(year%100==0){
printf("It is not a leap year");
}
else if(year%4==0){
printf("It is a leap year");
}
else{
printf("It is not a leap year");
}
}
SCOPE OF VARIABLE
A scope in any programming is a region of the program where a defined variable can have its
existence and beyond that variable cannot be accessed. There are three places where variables
can be declared in C programming language:
1. Inside a function or a block which is called local variables,
2. Outside of all functions which is called global variables.
3. In the definition of function parameters which is called formal parameters.
Only the local and global variables will be explained here.
LOCAL VARIABLES
Variables that are declared inside a function or block are called local variables. They can be used
only by statements that are inside that function or block of code. Local variables are not known
to functions outside their own.
GLOBAL VARIABLES
Global variables are defined outside of a function, usually on top of the program. The global
variables will hold their value throughout the lifetime of your program and they can be accessed
inside any of the functions defined for the program.
A global variable can be accessed by any function. That is, a global variable is available for use
throughout your entire program after its declaration.
Below is a program that will help understand the concept of Local and Global variable.
#include<stdio.h>
int i=6;
int a=7;
void main()
{
printf("%d \n", i);
if(a<=7)
{
int i=4;
printf("%d \n", i);
}
printf("%d \n", i);
printf("%d \n", i);
}
EXERCISE
I. By default instructions in a program are executed .
II. The if statement checks whether the text expression inside is true or not.
III. A scope in any programming is a region of the program where a defined variable can
have its existence and beyond that variable cannot be .
IV. Variables that are declared inside a function or are called local variables.
V. The global variables will hold their value throughout the of your
program.
LAB WORK
I. Write a program that asks the user to give input salary. If salary is greater than 10000TK
than the program outputs CONGRATULATION.
II. Write a program that takes length and breadth as input from the user and prints the
area of a rectangle as output. If the area is greater than 200 square meters the program
outputs THE AREA IS VERY LARGE else the program outputs THE AREA IS JUST RIGHT.
Hint: area=length*breadth
III. There are three integer numbers where a= 13 b=11 c=12
Write a program where if a is greater than b the program outputs A IS GREATER THAN B
and if a is greater than c the program outputs A IS ALSO GREATER THAN C. The program
also checks the else conditions.
Hint: use nested if else to do this program.
IV. Write a program that calculates the CGPA of a student who is in 1 st Semester.
Course Grade
Electric Circuit 1 3.75
Math 1 4.0
Physics 1 3.75
English 1 3.5
If CGPA is 3.75 to 4.00 the program outputs GOOD
If CGPA is 3.5 to 3.74 the program outputs SATISFACTORY
If CGPA is 3.0 to 3.49 the program outputs NEED TO IMPROVE If
CGPA is less than or equal to 2.99 then program outputs POOR
Hint: use else if clause to do this program.
ASSIGNMENT
I. Write a program that calculates the voltage of the given circuit. The value of R and I
have to be taken input from the user. If voltage is greater or equal to 10v then the
program will output DECREASE THE VOLTAGE. Hint: use if statement to do this program.
V=IR
II. Write a program that calculates the equivalent resistance of the following circuit. Input
is taken from the user.
Where R1=? Ohm, R2=? Ohm and R3=? Ohm.
If the equivalent resistance R is greater than 40 ohm the program outputs GOOD else
the program outputs BAD. Hint: use if else statement to do this program.
III. The voltage of three circuits was taken as input from the user.
Voltage1=?
Voltage 2=?
Voltage3=?
Write a program where if Voltage1 is greater than Voltage2 the program outputs
VOLATAGE1 IS GREATER THAN VOLTAGE2 and if Voltage1 is greater than Voltage3 the
program outputs VOLTAGE1 IS ALSO GREATER THAN VOLTAGE3. The program also
checks the else conditions.
Hint: use nested if else to do this program.
IV. Write a program that calculates the equivalent resistance R of the following circuit
Where R1=12 ohm, R2=3 ohm, R3=9 ohm and R4=16 ohm.
If V=.1 volt calculate the current flow, I for R.
If I is less than or equal to 1 print I IS LESS THAN OR EQUAL TO 1
If I is less than or equal to 2 print I IS LESS THAN OR EQUAL TO 2
If I is less than or equal to 3 print I IS LESS THAN OR EQUAL TO 3
If I is less than or equal to 4 print I IS LESS THAN OR EQUAL TO 4
If I is less than or equal to 5 print I IS LESS THAN OR EQUAL TO 5
If I is greater than 5 print “I IS GREATER THAN 5”
Hint: use else if clause to do this program.