04 - Control Structures

  • July 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 04 - Control Structures as PDF for free.

More details

  • Words: 2,261
  • Pages: 8
Introduction Control Structure - Specifies the sequence of execution of a group of statements. Three types of control structure: 1. Sequential 2. Conditional 3. Repeating (loop)

Control Structures

/ccsrac

Sequential Control Structure

Conditional Control Structures

• Statements are executed one after the other as they appear in the source code.

Analyze the following first!

2

Scenario 1: Example: a = 1; b = 2; c = a + b;

/* first statement */ /* second statement */ /*third statement */

If it rains tomorrow, then I won’t go out; I’ll just stay home and study. Otherwise, I’ll go out on a date.

/ccsrac

3

/ccsrac

4

Conditional Control Structure Scenario 2: You are at the South gate going to the Gokongwei building. There are several paths to take. Which is the best path? SPS

Admin

W. Shaw

Activity Center

Aristo

What is common to the two scenarios?

Amphi

Registrar Velasco

Mezz-1 Admission

Z2

Field

Accounting

Aquarium

Gokongwei

Conserv Eng. Gate

N. Gate S. Gate

MM

Miguel

SJ

S-N Gate /ccsrac

Simplified Map of DLSU 5

/ccsrac

6

1

Conditional Control Structure

Conditional Control Structure

• A conditional control structure is organized in such a way that there is always a condition that has to be evaluated first.

Two basic conditional control structures: 1. if statement (including if-else statement) 2. switch statement

• The condition will either evaluate to a true or false. • The result of the condition will then dictate the course of action to be taken. • The conditional control structure allows the program to make choices depending on a condition.

/ccsrac

7

The if statement

/ccsrac

8

Examples Write a program that will allow the user to input an integer value. If the value is greater than or equal to zero, print the word POSITIVE.

Format: if( <expression> ) <statement>

/* sample program to determine whether an input integer is a positive number */ #include <stdio.h> main() { int n;

expression False True

The expression is first evaluated. statement

If it’s true, the <statement> will be executed; otherwise, control will proceed with the statement following the if.

printf(“Input an integer value n: ”); scanf(“%d”, &n); if (n >= 0) printf(“n = %d is POSITIVE\n”, n);

After if statement

} /ccsrac

9

/ccsrac

10

The if statement

Examples Write a program to input two integers. Thereafter, the program should determine if these two numbers are equivalent. If they are equivalent, print the word EQUIVALENT.

For multiple statements, use { } for grouping. Example: if (nGrade >= 90) { printf (“Very Good!”); printf (“Very Intelligent! Keep it up!”); }

/* sample program to determine whether 2 integers are equivalent */ #include <stdio.h> main() { int nFirst, nSecond;

What will happen if the grouping symbols were removed?

printf(“Input 2 integers: ”); scanf(“%d%d”, &nFirst, &nSecond); if (nFirst == nSecond) printf(“EQUIVALENT!”); } /ccsrac

11

/ccsrac

12

2

Exercises

Exercises

1. Write a program that displays the word EVEN if the number entered by the user is even.

2. Modify your answer in number 1 such that when the number entered is not even, the program will display the word ODD.

/ccsrac

13

/ccsrac

14

The if statement

Exercises 3. Write a program that asks for the height of a person. The program displays the messages “Height is ok.” and “Have fun on the Enterprise.” when the height is at least 4.5 feet.

Format: if( <expression> )

False expression

<statement1> [ else <statement2> ]

True

The expression is evaluated.

statement1

If it’s true, the <statement1> will be executed; otherwise, <statement2> will be evaluated.

/ccsrac

15

statement2

After if statement

/ccsrac

Examples

Examples

/* sample program that determines if a person can ride the Enterprise or not */ #include <stdio.h> main() { float fHeight;

/* sample program that determines if entered number is EVEN or NOT*/ #include <stdio.h> main() { int nNumber;

printf(“Enter Height> ”); scanf(“%f”, &fHeight); if (fHeight >= 4.5) { printf(“Height is OK.\n”); printf(“Have fun on the Enterprise”); } else printf(“Sorry, you’re too short ”);

16

printf(“Enter Number> ”); scanf(“%d”, &nNumber); if (nNumber % 2 == 0) printf(“Number = %d is EVEN”, nNumber); else printf(“Number = %d is ODD”, nNumber); }

} /ccsrac

17

/ccsrac

18

3

Exercises

Exercises

4. Write a program to input two integers.

5. Write a program that will allow the user to

Thereafter, the program should determine if these two numbers are equivalent. If they are equivalent, print the word EQUIVALENT; otherwise, display NOT EQUIVALENT.

/ccsrac

input an integer value. If the value is greater than zero, display the word POSITIVE. If the value is negative, then display the word NEGATIVE. If the value is zero, display the word ZERO.

19

Watch your braces! Expected Output: Enter n: POSITIVE end

3

Enter n: ZERO end

0

main() { double x; printf(“Enter a value: ”); scanf(“%lf”, &x); What’s the screen output if: if (x >= 0 && x <= 100) a) x = 90.1 d) x = 67.7 if (x > 95) b) x = 80 e) x = -10 printf(“A+\n”); c) x = 70 else if (x >= 70) if (x >= 80) if (x >= 90) printf(“A\n”); else if (x >= 85) printf(“B+\n”); else printf(“B\n”); else if (x >= 75) printf(“C+\n”); else printf(“C\n”); else printf(“D\n”); else printf(“You\’re playing hard to get!!!\n”); }

Correct Code: if (n != 0) { if (n > 0) printf(“POSITIVE”); } else printf(“ZERO”); printf(“end”); 21

/ccsrac

Common Programming Errors

Exercises

main() { : if n > 1 printf(“Greater than 1”); if (n = 1) then printf(“Equals 1”); if (n < 1); printf(“Less than 1”); }

6. Write a program which will ask for two

/ccsrac

20

Let’s Trace!

Source Code: if (n != 0) if (n > 0) printf(“POSITIVE”); else printf(“ZERO”); printf(“end”);

/ccsrac

/ccsrac

22

integers and determines which value is smaller. The program should print ‘FIRST NUMBER IS SMALLER” if the first integer is indeed smaller than the second. If the second number is smaller it should print ‘SECOND NUMBER IS SMALLER.’ If the two numbers are equivalent, print the word ‘TWO NUMBERS ARE EQUIVALENT’.

23

/ccsrac

24

4

Exercises

Exercises

7. You were hired by PAGCOR as part of a

8. In temperate countries like Japan, there are four seasons. April to June is spring, July to September is summer, October to December is autumn and January to March is winter. We wish to write a program that allows the user to input month in its numeric equivalent (i.e., January is 1, February is to 2 and so on...).

programming team in-charge of automating its BINGO game. Your task is to write a program that will accept an integer whose value is 1 to 75. Thereafter, your program should determine and print the letter that corresponds to that number. That is, the numbers from 1 to 15 correspond to ‘B’, 16 to 30 correspond to ‘I’, 31 to 45 correspond to ‘N’ , 46 to 60 correspond to ‘G’ and 61 to 75 correspond to ‘O’.

The output of the program is the season associated with that month. An example session is as follows: Input month: 5 It is spring. Have a nice day.

/ccsrac

25

Answer to number 8

/ccsrac

Answer to number 8

/* Program that gives season, given the numeric month */ #include <stdio.h> main() { int nMonth;

if (nMonth == 3) { printf(“It is winter.\n”); printf(“Have a nice day.”); } if (nMonth == 4) { printf(“It is spring.\n”); printf(“Have a nice day.”); } if (nMonth == 5) { printf(“It is spring.\n”); printf(“Have a nice day.”); } if (nMonth == 6) { printf(“It is spring.\n”); printf(“Have a nice day.”); }

printf(“Input month: ”); scanf(“%d”, &nMonth); if (nMonth == 1) { printf(“It is winter.\n”); printf(“Have a nice day.”); } if (nMonth == 2) { printf(“It is winter.\n”); printf(“Have a nice day.”); } /ccsrac

26

27

Answer to number 8

/ccsrac

28

Answer to number 8

if (nMonth == 7) { printf(“It is summer.\n”); printf(“Have a nice day.”); } if (nMonth == 8) { printf(“It is summer.\n”); printf(“Have a nice day.”); } if (nMonth == 9) { printf(“It is summer.\n”); printf(“Have a nice day.”); }

if (nMonth == 10) { printf(“It is autumn.\n”); printf(“Have a nice day.”); } if (nMonth == 11) { printf(“It is autumn.\n”); printf(“Have a nice day.”); } if (nMonth == 12) { printf(“It is autumn.\n”); printf(“Have a nice day.”); } }

/ccsrac

29

/ccsrac

30

5

Another answer

Still another answer

/* Program that gives season, given the numeric month */ #include <stdio.h> main() { int nMonth;

/* Program that gives season, given the numeric month */ #include <stdio.h> main() { int nMonth;

printf(“Input month: ”); scanf(“%d”, &nMonth); if (nMonth == 1 || nMonth == 2 || nMonth == 3) printf(“It is winter.\n”); if (nMonth == 4 || nMonth == 5 || nMonth == 6) printf(“It is spring.\n”); if (nMonth == 7 || nMonth == 8 || nMonth == 9) printf(“It is summer.\n”); if (nMonth == 10 || nMonth == 11 || nMonth == 12) printf(“It is autumn.\n”); printf(“Have a nice day.”);

printf(“Input month: ”); scanf(“%d”, &nMonth); if (nMonth >= 1 && nMonth <= 3) printf(“It is winter.\n”); else if (nMonth >= 4 && nMonth <= 6) printf(“It is spring.\n”); else if (nMonth >= 7 && nMonth <= 9) printf(“It is summer.\n”); else if (nMonth >= 10 && nMonth <= 12) printf(“It is autumn.\n”); printf(“Have a nice day.”);

}

} /ccsrac

31

The switch statement

Write a program that accepts from the user the day of the week in numeric form. The program converts this to its word equivalent (i.e. Zero (0) is for Sunday, one (1) for Monday, etc.).

Should evaluate to either a character, integer, short, or byte

switch( <expression> ) { case

Related Documents