You are on page 1of 81

Computing Fundamentals

Dr. Muhammad Yousaf Hamza


Deputy Chief Engineer, PIEAS
Conditional Operator

Dr. Yousaf, PIEAS


Conditional Operator
if ( a < b)
c = a + 5;
else
c = b + 8;

// We can do this in compact form as


c = a < b ? a + 5 : b + 8; //Ternary conditional operator (?:)
Evaluate first expression. If true, evaluate second,
otherwise evaluate third.

Dr. Yousaf, PIEAS


Conditional Operator
• Ternary conditional operator (?:)

– Takes three arguments (condition, value if true,


value if false)

– Our pseudocode could be written:


grade >= 60 ? printf( “Passed\n” ) :
printf( “Failed\n” );

Dr. Yousaf, PIEAS


Conditional Operator

• The conditional operator essentially allows you to embed an


“if” statement into an expression
• Generic Form
exp1 ? exp2 : exp3 if exp1 is true
value is exp2
(exp3 is not evaluated)
if exp1 is false,
value is exp3
(exp2 is not evaluated)
Dr. Yousaf, PIEAS
Conditional Operator
• Example:
z = (x > y) ? x : y;
• This is equivalent to:
if (x > y)
z = x;
else
z = y;

Dr. Yousaf, PIEAS


switch statement

Dr. Yousaf, PIEAS


// No use of switch statement
// use of if-else
#include <stdio.h>
int main()
{
int day;
printf("The day number 1 means Monday\n");
printf("Please enter the number of day. \n The number must be
any integer value from 1 to 7\n");
scanf("%d",&day);
if (day == 1)
printf("Monday\n");
else if (day == 2)
printf("Tuesday\n");
else if (day == 3)
printf("Wednesday\n");
// You may complete it yourself
Dr. Yousaf, PIEAS
The if/else Selection Structure
Nested if/else structures
• In previous example, the nested if/else structure is
to be used.
• Test for multiple cases by placing if/else selection
structures inside if/else selection structures
• Deep indentation usually not used in practice
• How can we solve this example more conveniently?

• switch statement is a convenient way to code it.

Dr. Yousaf, PIEAS


Switch Statement
If you have a large decision tree, and all
the decisions depend on the value of
the same variable, you will probably
want to consider a switch statement
instead of a ladder of if...else or else if
constructions.

Dr. Yousaf, PIEAS


// Example of switch statement
#include <stdio.h>
int main()
{
int day;
printf("The day number 1 means
Monday\n");
printf("Please enter the number of day. \n
The number must be any integer value from
1 to 7\n");
scanf("%d",&day);
// Contd. (next page)
Dr. Yousaf, PIEAS
switch(day)
{
case 1: // 1 is one of possible value of day and so on.
printf("Monday\n");
break;
case 2:
printf("Tuesday\n"); break;
// please write cases 3 to 6 yourself
case 7:
printf("Sunday\n");
break;
default:
printf(“You eneterd a wtong number. The number must be an
integer value from 1 to 7\n");
}
getchar(); return 0;
}
Dr. Yousaf, PIEAS
Example of switch statement
#include <stdio.h> case -2:
int main()
{ printf(“I am very smart”);
int num; break;
printf(“Enter an integer from -5 // Write cases -1 to 9 yourself
to 9”) ;
scanf(“ %d”, &num) default:
switch(num) printf(“Default value is:
{ %d", num);
case -5:
printf(“PIEAS is Beautiful"); }
break; getchar();
case -4: return 0;
printf(“CFP is my favorite
subject”); }
break;
case -3:
printf(“I am studying at the
University”);
break;
Dr. Yousaf, PIEAS
Example of switch statement
#include <stdio.h> case 1:
int main()
{ printf(“I am very smart”);
int num; break;
printf(“Enter an integer from -5 // Write cases 2 to 7 yourself
to 4”) ;
scanf(“ %d”, &num) default:
switch(num+3) printf(“Default value is:
{ %d", num);
case -2:
printf(“PIEAS is Beautiful"); }
break; getchar();
case -1: return 0;
printf(“CFP is my favorite
subject”); }
break;
case 0:
printf(“I am studying at
University”);
break;
Dr. Yousaf, PIEAS
• switch
– Useful when a variable or expression is tested for
all the values which can happen and different
actions are taken
• Format
– Series of case labels and an optional default case
switch ( value )
{
case 1:
actions
break
case 2:
actions
break
default:
actions
} Dr. Yousaf, PIEAS
The switch Structure
• Flowchart of the switch structure
true case a break
case a
action(s)
false
true case b break
case b
false action(s)

.
.
.
true case z break
case z
false action(s)

default
action(s)
Dr. Yousaf, PIEAS
switch Statement
• The expression used in a switch statement must have an integral or
character type.
• You can have any number of case statements within a switch. Each case
is followed by the value of decision variable to be compared and a colon.
• The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a integer or a character.
• When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which appears at
the end of the switch. The default case can be used for performing a task
when none of the cases is true. No break is needed in the default case.

Dr. Muhammad Yousaf Hamza


switch Statement
• Case doesn’t always need to have order 1, 2, 3 and so on. They can have
any integer value after case keyword. Also, case doesn’t need to be in an
ascending order always, you can specify them in any order as per the
need of the program.
• The expression provided in the switch should result in an integer value
otherwise it would not be valid.
• Nesting of switch statements are allowed, which means you can have
switch statements inside another switch. However nested switch
statements should be avoided as it makes program more complex and
less readable.
• Duplicate case values are not allowed.
• The default statement is optional, if you don’t have a default in the
program, it would run just fine without any issues. However it is a good
practice to have a default statement so that the default executes if no
case is matched. This is especially useful when we are taking input from
user for the case choices, since user can enter wrong value, we can
remind the user with a proper error message that we can set in the
default statement. Dr. Muhammad Yousaf Hamza
switch Statement
• An expression must always execute to a result.
• Case labels must be constants and unique.
• Case labels must end with a colon ( : ).
• There can be only one default label.
• switch is a decision making construct in 'C.'
• A switch is used in a program where multiple decisions are
involved.
• A switch must contain an executable test-expression.
• Case label must be constants and unique.
• The default is optional.
• Multiple switch statements can be nested within one
another.

Dr. Muhammad Yousaf Hamza


Example of switch statement
#include <stdio.h> case 'c':
int main() printf("Case C");
{ break;
char ch='b'; case 'z':
switch (ch) printf("Case Z ");
{ break;
case 'd': default:
printf("Case D ");
printf("Default ");
break;
case 'b': }
printf("Case B"); getchar();
break; return 0;
}

Dr. Yousaf, PIEAS


Example of switch statement
#include <stdio.h> // write case ‘-’ Yourself
int main() case ‘*’:
{
char operator;
printf(“%d*%d=%d”,num1,num2,
int num1,num2; num1*num2);
printf(“\n Enter the operator (+, -, break;
*, /):”); case ‘/’:
scanf(“%c”,&operator); printf(“%d / %d =
printf(“\n Enter the Two
numbers:”);
%d”,num1,num2,num1/num2);
scanf(“%d%d”,&num1,&num2); break;
switch (operator) default:
{ printf(“\n Enter the operator
case ‘+’: only”);
printf(“%d+%d=%d”,num1,num2,n break;
um1+num2);
break;
}
getchar(); return 0; }
Dr. Yousaf, PIEAS
Calculator using switch statement
Develop a C code that asks the user to select
the option for maths operation (+,-,*,/,%,sqrt
etc) then ask the user to enter the numbers and
display the result.

Do it Yourself

Dr. Yousaf, PIEAS


Importance of Computational and
Simulation Work

Dr. Yousaf, PIEAS


Importance of Computational Work
• Computational and simulation (C&S) work has become a
fundamental tool in the fields of science and engineering. It
has geared the scientific research. It is enabling and very
important technology in almost every area of life.

• The computational and simulation work can lead to the


successful as well as economical development of scientific
systems.

• C&S helps for making the correct decisions among the many
available choices. It also helps to model prototypes and
simulators. C&S has been approved as a powerful tool for
scientific research. Due to very attractive benefits, the
computational and simulation has been technically accepted
across the globe. It has not only increased the efficiency of
scientific research but also made it faster and cheaper.

Dr. Yousaf, PIEAS


Importance of Computational Work
The potential benefits of Simulations are:
• The trained students can understand the assumptions
and implementation procedures to handle a problem
in efficient way at their work places.
• They can have deep knowledge of a system through
quantitative analysis by performing extensive
simulations.
• The knowledge that the students gain can be applied
in various scientific applications. Different parameters
involved in the working of a system can be
understood efficiently.
• As the experience of C&S work enhances the
visualization of a system, it can be used as a
diagnostic tool for measuring the efficiency and
performance of various systems.
Dr. Yousaf, PIEAS
Importance of Computational Work
• The trained students can foresee the response
of a system during normal and hypothetical
accidental situations.
• The trained students can identify the role of
most important components in a system
through C&S work.
• It would help them to estimate the system
parameters prior to the actual system design. If
a component for a system is to be selected
which can perform the best among the various
types, then the optimum choice for the selection
of the component can be made without having
the need of buying actual physical components.
Dr. Yousaf, PIEAS
Importance of Computational Work
• The trained student can design/test various modules
and then can integrate them in efficient way to
prepare a prototype. After carefully testing of such
prototype, the new developments can be made more
efficiently and economically.
• Having experience in C&S, the trained students can
predict the changes in a system under certain
circumstances. By this knowledge some pre-emptive
measurements can be taken to avoid any accident at
the projects.
• C&S work allows for efficient if-then-else analyses
without actually performing the experiments. It
would save the time/money/labor at the projects.
• As the decision making abilities are enhanced through
C&S, the trained students can make technical
decisions more conveniently and correctly.
Dr. Yousaf, PIEAS
Usefulness
The computational and simulation work is
helpful in electrical systems. Some of the
applications are mentioned below.
• For programming of the controllers to control
certain processes
• Models of state estimators and filters
• Models of uncertainties, disturbances and
noises
• Models of faults
• For power network design
• For voice and sound recognition
• For processing of the digital data.
Dr. Muhammad Yousaf Hamza
That remarkable change has come about mainly
because of developments in the computational and
computer sciences and the rapid advances in
computing equipment and systems. There are other
reasons. For example, a host of
technologies are on the horizon that we cannot hope
to understand, develop, or utilize without
simulation. Many of those technologies are critical
to the nation’s continued leadership in science and
engineering. Clearly, research in SBES is quickly
becoming indispensable to our country’s security
and well-being.

Dr. Muhammad Yousaf Hamza


We can expect dramatic advances on a broad
front: medicine, materials science, homeland
security, manufacturing, engineering
design, and many others.

Dr. Muhammad Yousaf Hamza


SBES is a discipline indispensable to the nation’s
continued leadership in science and engineering. It is
central to advances in biomedicine, nanomanufacturing,
homeland security, microelectronics, energy and
environmental sciences, advanced materials, and product
development.
There is ample evidence that developments in these new
disciplines could significantly impact virtually every
aspect of human experience.

Dr. Muhammad Yousaf Hamza


SBES: A National Priority for Tomorrow’s
Engineering and Science

Dr. Muhammad Yousaf Hamza


Importance of Computer Simulations
in Mechanical Engineering
Importance of Computer Simulations
in Physics and Medical Physics

Dr. Yousaf, PIEAS


Computer Applications for Physics
 In the physics field, the analytical solutions of physical
equations often cannot be obtained but the numerical
solutions can be obtained by computers.
 This helps the understanding of phenomena. Fast
plastic deformation which cannot be performed by
experiments and atomic vibrations which are difficult
to be directly observed can be simulated using
computers.
 Computing can play an important varied role in
advancing physics learning.
 We point out the role of computational techniques
namely Simulation, Multimedia, Telematics, Virtual
Reality and Computer based labs which may deal with
difficulties and increase the Yousaf
Dr. Muhammad learning
Hamza process
Computer Applications for Metallurgy
and Materials Engineering
 The application of computer in material science and
engineering is developing increasingly.
 To use the technology correlatively, for example, data
processing, simulation techniques, mathematical
model and database etc.
 Through the process of establishing the mechanism
model, using a computer data analysis process in
materials science, the model predicts the optimal
design to achieve.
 Computer application technology continues to evolve,
gradually and comprehensively solve the major
technical problems in materials science and
engineering.
Dr. Muhammad Yousaf Hamza
Computer Applications for Metallurgy
and Materials Engineering
 The technology prediction made in 1987 by the
Science and Technology Agency of Japan has selected
the priority areas in materials as
 analysis and control on the atomic and molecular levels
 the design and synthesis of materials by computer science
 development of soft devices.
 This may be developed in virtual laboratories in the
field of materials science and engineering.
 The water resistance of ships and the air resistance of airplanes and
space vehicles are calculated by fluid mechanics.
 Simulations are used and huge wind tunnels or water tanks are used
as the last means.
 Computers are used for the design of electronic circuits. Computer
aided molecular design (CAMD) has been widely applied to the
chemical field molecular mechanics and molecular dynamics

Dr. Muhammad Yousaf Hamza


Relationship of various software to be
used in engineering applications
 The computer science has always been an
unavoidable part of human life since it was introduced
to the world for the first time.
 The field of study has a lot of applications which
impacts the everyday life of the human beings.
 Computer science in one way or other is connected to
each and every human’s daily routine and is a field of
study which makes life easier for the human beings.

Dr. Muhammad Yousaf Hamza


Importance of Computer Simulations
in MRE

Dr. Yousaf, PIEAS


Importance of Computer Simulations
in Chemistry

Dr. Yousaf, PIEAS


289 Pages Book
Chemistry
Importance of Computer Simulations
in Process/Chemical Technology
Importance of Computer Simulations
in Metallurgical

Dr. Yousaf, PIEAS


122 Pages Book
Computer Applications for Computer
Sciences
 Computer science is basically the study of science that
revolves around the computers.
 It is the study of the theory, experimentation, and
engineering that form the basis for the design and use of
computers.
 It is the field of study that deals with computers and
computational systems.
 Unlike the other engineering programs, computer
science deals with the theory, layout, development, and
applications of software’s and the systems of software’s.
 Computer science and its application can bring about a
positive change in the world.
 It provides a platform for the people to implement their
creative and innovative ideas
Dr. Muhammad and
Yousaf concepts.
Hamza
Computer Applications for Computer Sciences
 Computer science is not limited to a few subjects and
topics. It is a vast field of study which provides an
individual the opportunity to explore and experiment in
various fields of study. The major areas of study in
computer science include:
 Artificial intelligence (AI).
 Systems and the networks of computers.
 Cyber security.
 Database systems or database management systems (DBMS).
 Human computer communication and interaction.
 Visualization and graphics.
 Numeric analysis.
 Programming languages.
 Software engineering.
 Bioinformatics.
 Theory of computing.

Dr. Muhammad Yousaf Hamza

You might also like