• Tidak ada hasil yang ditemukan

CS1100 – Introduction to Programming Lecture 13 Instructor

N/A
N/A
Protected

Academic year: 2024

Membagikan "CS1100 – Introduction to Programming Lecture 13 Instructor"

Copied!
21
0
0

Teks penuh

(1)

CS1100 – Introduction to Programming Lecture 13

Instructor: Shweta Agrawal ([email protected])

(2)

Mini-calculator using While loop

Input operator and two operands, output result till quit.

#include<stdio.h> int main() {

int x; int y; char op;

printf("Input the operator \t"); scanf ("%c", &op); while (op != ’q’) {

printf("Input the first integer \t"); scanf ("%d", &x); printf("Input the second integer \t"); scanf ("%d", &y); switch (op) {

case ’+’: printf("x+y = %d\n", x+y); break; case ’%’: printf("x mod y = %d\n", x%y); break; case ’/’: printf("x/y = %.2f\n", (x*0.1)/y); break; case ’q’: ; break;

default : printf("invalid operator\n"); break; }

getchar(); printf("Input the operator \t"); scanf ("%c", &op);

} }

(3)

Mini-calculator using While loop

Input operator and two operands, output result till quit.

#include<stdio.h>

int main() {

int x; int y; char op;

printf("Input the operator \t"); scanf ("%c", &op);

while (op != ’q’) {

printf("Input the first integer \t"); scanf ("%d", &x);

printf("Input the second integer \t"); scanf ("%d", &y);

switch (op) {

case ’+’: printf("x+y = %d\n", x+y); break;

case ’%’: printf("x mod y = %d\n", x%y); break;

case ’/’: printf("x/y = %.2f\n", (x*0.1)/y); break;

case ’q’: ; break;

default : printf("invalid operator\n"); break;

}

getchar(); printf("Input the operator \t");

scanf ("%c", &op);

} }

(4)

Mini-calculator: Case Insensitive Quit

#include<stdio.h>

int main() { // read op.

while ((op != ’q’) || (op != ’Q’)) { // all the same stuff.

} }

Is the condition correct?

We want to stop when op is either ’q’ or ’Q’. (op == ’q’

||

op == ’Q’) then quit the loop.

We want to enter the loop when the above condition is false!

!(op == ’q’

||

op == ’Q’) same as (op != ’q’ && op != ’Q’)

(5)

Mini-calculator: Case Insensitive Quit

#include<stdio.h>

int main() { // read op.

while ((op != ’q’) || (op != ’Q’)) { // all the same stuff.

} }

Is the condition correct?

We want to stop when op is either ’q’ or ’Q’. (op == ’q’

||

op == ’Q’) then quit the loop.

We want to enter the loop when the above condition is false!

!(op == ’q’

||

op == ’Q’) same as (op != ’q’ && op != ’Q’)

(6)

Mini-calculator: Case Insensitive Quit

#include<stdio.h>

int main() { // read op.

while ((op != ’q’) || (op != ’Q’)) { // all the same stuff.

} }

Is the condition correct?

We want to stop when op is either ’q’ or ’Q’.

(op == ’q’

||

op == ’Q’) then quit the loop.

We want to enter the loop when the above condition is false!

!(op == ’q’

||

op == ’Q’) same as (op != ’q’ && op != ’Q’)

(7)

Mini-calculator: Case Insensitive Quit

#include<stdio.h>

int main() { // read op.

while ((op != ’q’) || (op != ’Q’)) { // all the same stuff.

} }

Is the condition correct?

We want to stop when op is either ’q’ or ’Q’.

(op == ’q’

||

op == ’Q’) then quit the loop.

We want to enter the loop when the above condition is false!

!(op == ’q’

||

op == ’Q’) same as (op != ’q’ && op != ’Q’)

(8)

Mini-calculator: Case Insensitive Quit

#include<stdio.h>

int main() { // read op.

while ((op != ’q’) || (op != ’Q’)) { // all the same stuff.

} }

Is the condition correct?

We want to stop when op is either ’q’ or ’Q’.

(op == ’q’

||

op == ’Q’) then quit the loop.

We want to enter the loop when the above condition is false!

!(op == ’q’

||

op == ’Q’) same as (op != ’q’ && op != ’Q’)

(9)

Mini-calculator: Case Insensitive Quit

#include<stdio.h>

int main() { // read op.

while ((op != ’q’) || (op != ’Q’)) { // all the same stuff.

} }

Is the condition correct?

We want to stop when op is either ’q’ or ’Q’.

(op == ’q’

||

op == ’Q’) then quit the loop.

We want to enter the loop when the above condition is false!

!(op == ’q’

||

op == ’Q’)

same as (op != ’q’ && op != ’Q’)

(10)

Mini-calculator: Case Insensitive Quit

#include<stdio.h>

int main() { // read op.

while ((op != ’q’) || (op != ’Q’)) { // all the same stuff.

} }

Is the condition correct?

We want to stop when op is either ’q’ or ’Q’.

(op == ’q’

||

op == ’Q’) then quit the loop.

We want to enter the loop when the above condition is false!

!(op == ’q’

||

op == ’Q’) same as (op != ’q’ && op != ’Q’)

(11)

Mini-calculator: using a true expression

Let the while loop run forever!

while (1) {

printf("Input the operator \t"); scanf ("%c", &op); printf("Input the first integer \t"); scanf ("%d", &x); printf("Input the second integer \t"); scanf ("%d", &y); switch (op) {

case ’+’: printf("x+y = %d\n", x+y); break; // other cases..

}

getchar();

//printf("Input the operator \t"); //scanf ("%c", &op);

}

How does the program terminate?

We can make a check and

break

out of the loop!

(12)

Mini-calculator: using a true expression

Let the while loop run forever!

while (1) {

printf("Input the operator \t"); scanf ("%c", &op);

printf("Input the first integer \t"); scanf ("%d", &x);

printf("Input the second integer \t"); scanf ("%d", &y);

switch (op) {

case ’+’: printf("x+y = %d\n", x+y); break;

// other cases..

}

getchar();

//printf("Input the operator \t");

//scanf ("%c", &op);

}

How does the program terminate?

We can make a check and

break

out of the loop!

(13)

Mini-calculator: using a true expression

Let the while loop run forever!

while (1) {

printf("Input the operator \t"); scanf ("%c", &op);

printf("Input the first integer \t"); scanf ("%d", &x);

printf("Input the second integer \t"); scanf ("%d", &y);

switch (op) {

case ’+’: printf("x+y = %d\n", x+y); break;

// other cases..

}

getchar();

//printf("Input the operator \t");

//scanf ("%c", &op);

}

How does the program terminate?

We can make a check and

break

out of the loop!

(14)

Mini-calculator: using a true expression

Let the while loop run forever and

break

it when needed.

while (1) {

printf("Input the operator \t"); scanf ("%c", &op); if (op == ’q’ || op == ’Q’) {

break; }

printf("Input the first integer \t"); scanf ("%d", &x); printf("Input the second integer \t"); scanf ("%d", &y); switch (op) {

case ’+’: printf("x+y = %d\n", x+y); break; // other cases..

}

getchar(); }

How does the program terminate?

break

takes you out of the current statement (it could be switch /

loop).

(15)

Mini-calculator: using a true expression

Let the while loop run forever and

break

it when needed.

while (1) {

printf("Input the operator \t"); scanf ("%c", &op);

if (op == ’q’ || op == ’Q’) { break;

}

printf("Input the first integer \t"); scanf ("%d", &x);

printf("Input the second integer \t"); scanf ("%d", &y);

switch (op) {

case ’+’: printf("x+y = %d\n", x+y); break;

// other cases..

}

getchar();

}

How does the program terminate?

break

takes you out of the current statement (it could be switch /

loop).

(16)

Mini-calculator: using a true expression

Let the while loop run forever and

break

it when needed.

while (1) {

printf("Input the operator \t"); scanf ("%c", &op);

if (op == ’q’ || op == ’Q’) { break;

}

printf("Input the first integer \t"); scanf ("%d", &x);

printf("Input the second integer \t"); scanf ("%d", &y);

switch (op) {

case ’+’: printf("x+y = %d\n", x+y); break;

// other cases..

}

getchar();

}

How does the program terminate?

break

takes you out of the current statement (it could be switch /

loop).

(17)

Stepping back : Stages of Program Design ...

To solve a problem using a computer program :

Need to develop an idea of the sequence of statements and the flow of the control through the statements to achieve a given task.

Algorithm : The description of the idea - a step-by-step procedure to solve the problem.

Pseudo-code : A language-independent but program-like description of an algorithm.

Flowchart : A diagramatic representation of the algorithm is

called a flowchart.

(18)

Stepping back : Stages of Program Design ...

To solve a problem using a computer program :

Need to develop an idea of the sequence of statements and the flow of the control through the statements to achieve a given task.

Algorithm : The description of the idea - a step-by-step procedure to solve the problem.

Pseudo-code : A language-independent but program-like description of an algorithm.

Flowchart : A diagramatic representation of the algorithm is

called a flowchart.

(19)

Stepping back : Stages of Program Design ...

To solve a problem using a computer program :

Need to develop an idea of the sequence of statements and the flow of the control through the statements to achieve a given task.

Algorithm : The description of the idea - a step-by-step procedure to solve the problem.

Pseudo-code : A language-independent but program-like description of an algorithm.

Flowchart : A diagramatic representation of the algorithm is

called a flowchart.

(20)

Stepping back : Stages of Program Design ...

To solve a problem using a computer program :

Need to develop an idea of the sequence of statements and the flow of the control through the statements to achieve a given task.

Algorithm : The description of the idea - a step-by-step procedure to solve the problem.

Pseudo-code : A language-independent but program-like description of an algorithm.

Flowchart : A diagramatic representation of the algorithm is

called a flowchart.

(21)

Stepping back : Stages of Program Design ...

To solve a problem using a computer program :

Need to develop an idea of the sequence of statements and the flow of the control through the statements to achieve a given task.

Algorithm : The description of the idea - a step-by-step procedure to solve the problem.

Pseudo-code : A language-independent but program-like description of an algorithm.

Flowchart : A diagramatic representation of the algorithm is

called a flowchart.

Referensi

Dokumen terkait

• The basic calculus measurement, the rate of change of a function at a point, is useful in the quantitative analysis of business problems.. • A convenient way to express how a

The definite integral of a constant times a function is equal to the constant times the definite

This function will implement the basic sentinel loop from our original program to input a sequence of numbers. We will use an initially empty list as an accumulator to collect

Chapter 3 Flow of Control: Branching 175 3.1 THE if-else STATEMENT 176 The Basic if-else Statement 177 Boolean Expressions 184 Comparing Strings 189 Nested if-else Statements 194

The idea of a corresponding unit test is then to run the algorithm for somen values, compute the error the absolute value of the difference between the exact analytical result and the

6.2 Newton’s Method 191 A fundamental idea of numerical methods for nonlinear equations is to construct a series of linear equations since we know how to solve linear equations and

edu.my ABSTRACT In order to develop approaches to solve a fuzzy linear programming problem, it is necessary to study first the formulation of membership functions and then the

• In view of huge task of Government to develop more PV plant ot meet the 2025 target, the Government would still need to build more power plants together with the participation of