Lab Session # 3 Conditional Statements. ALQUDS University Department of Computer Engineering

Similar documents
LAB 4.1 Relational Operators and the if Statement

BRANCHING if-else statements

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Fundamental of Programming (C)

Fundamentals of Programming

- If you want to repeat the same blocks of code over and over you have two choices. Copy

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Decision Making -Branching. Class Incharge: S. Sasirekha

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Chapter 4: Making Decisions

Chapter 4: Making Decisions

Chapter 10 C Structures, Unions, Bit Manipulations

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Boolean Expressions (Conditions)

Control Structure: Selection

Lecture 5 Tao Wang 1

& Technology. Expression? Statement-x. void main() int no; scanf("%d", &no); if(no%2==0) if(no%2!=0) Syllabus for 1. of execution of statements.

SELECTION. (Chapter 2)

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Repetition Algorithms

Decision Making and Branching

LECTURE 04 MAKING DECISIONS

conditional statements

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Fundamentals of Programming Session 9

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

LOOPS. 1- Write a program that prompts user to enter an integer N and determines and prints the sum of cubes from 5 to N (i.e. sum of 5 3 to N 3 ).

What we have learned so far

The following expression causes a divide by zero error:

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Programming Language A

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Following is the general form of a typical decision making structure found in most of the programming languages:

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Question 2. [2 points] Which of the following is a correct statement to obtain user input? (Assume that fleems is an int variable.

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Midterm Exam

Introduction. C provides two styles of flow control:

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis.

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes:

Control Structures in Java if-else and switch

Fundamentals of Programming CS-110. Lecture 2

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

QUIZ: What value is stored in a after this

Objectives. Structure. Munster Programming Training

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Other Loop Options EXAMPLE

General Instructions. You can use QtSpim simulator to work on these assignments.

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

University of Massachusetts Amherst, Electrical and Computer Engineering

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

CSci 1113 Midterm 1. Name: Student ID:

CGS 3460 Summer 07 Midterm Exam

Question 1. [5 points] Circle and briefly explain the error(s) in the following code:

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

AP Computer Science A Summer Assignment 2017

Lab Session # 6 Functions. ALQUDS University Department of Computer Engineering

CPSC 310: Sample Final Exam Study Questions 2014S1 (These are in addition to the Study Questions listed at the end of some lectures)

Programming Language A

Score score < score < score < 65 Score < 50

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Fundamentals of Programming

Week 0. Net Salary =Earnings- deductions; Read the employee number, Basic Print employee Number, Earnings,Deductions and Net salary.

IT 1033: Fundamentals of Programming Loops

Relational Operators EXAMPLE. C++ By

THINK like a Programmer

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

ALGORITHMS AND FLOWCHARTS

Lab 3: Control Flow / Conditional Statements. Based on the presentation made by Graham Northup

بسم اهلل الرمحن الرحيم

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Database Concepts Using Microsoft Access

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

Introduction to C Programming

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Decisions. Arizona State University 1

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

Structured programming. Exercises 3

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

UNIVERSITI SAINS MALAYSIA. CIT502 Object-Oriented Programming and Software Engineering

WebAdvisor Step by Step Registration Instructions for Students

Unit: Rational Number Lesson 3.1: What is a Rational Number? Objectives: Students will compare and order rational numbers.

4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20;

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Transcription:

2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 3 Conditional Statements ALQUDS University Department of Computer Engineering Objective: Our objective for today s lab session is to introduce you to the C programing language. The materials supplied during this period will teach you to: 1. Study the componed if statements. 2. Learn how to use the switch construct. 3. Study about the type of assignments. Experiment 3.1 The else if ladder There is another way of putting ifs together when multipath decisions are involved. A multipath decision is a chain of ifs in which the statement associated with each else is an if. It takes the following general form: if (condition 1) statement 1 ; else if (condition 2) statement 2; else if (condition 3) statement 3; else if (condition n) statement n; else default statement; statement x; This construct is known as the else if ladder. The conditions are evaluated from the top (of the ladder), downwards. As soon as a true condition is found, the statement associated with is executed and the control is transferred to the statement x (skipping the rest of the ladder). 1

When all the n conditions become false, then the final else containing the default statement will be executed. Step 1: Let us consider an example of grading the students in an academic institution. The grading is done according to the following rules: 90-100 Excellent 80-89 Very good 70-79 Good 60-69 Bad Mark 00-59 Fail. Write a program using the else if ladder according the last rules. - - - - - - - - - - - - - - - - Step 2: C program that asks the user to enter his/her birth of day as follows. Your output should take the form: Today Date is: DD/MM/YYYY Your Birth of Date is DD/MM/YYYY Note: Suppose 1 Month=30 days. Your Age in Years is xx years, yy months, zz days Ex: Enter Today date in the format DD/MM/YYYY: 15/3/2009 Enter Your Birth of Date in the format DD/MM/YYYY: 10/2/1990 2

Output : Today Date is: 15/3/1990. Your Birth of Date is: 10/2/1990 Your Age is 19 years, 1 month(s), 5 days Algorithm C program --------- -------------------------------------------- 3

Experiment 3.2 The C switch construct is similar to Pascal's case statement and it allows multiple choice of a selection of items at one level of a conditional where it is a far neater way of writing multiple if statements: switch (expression) { case item 1 : statement 1 ; case item 2 : statement 2 ; } case item n : statement n ; default: statement; In each case the value of item i must be a constant, variables are not allowed. The break is needed if you want to terminate the switch after execution of one choice. Otherwise the next case would get evaluated. Note: This is unlike most other languages. The default case is optional and catches any other cases. Switch(x){ case 1: case 2: case 3: printf("pass") case 4: printf("not Pass") default: printf("kl") break } 4

Step 1: Compile and execute the program. Record the results and comment the results. Switch(x){ case 1: printf("the value of X is One ") case 2: printf("the value of X is Two ") case 3: printf("the value of X is Three ") default: printf("x not equal one or two or three.") } - - - - Step 2: Delete the break statement after the first case, record the results and comments the results. - -. Step 3: Write break after default statements in the last line, then record the results. - - 5

Experiment 3.3 Write a C program that asks the user to enter an integer number from 20 to 70, then prints the English words of the number. Ex. if the user enters 35. You enter the number: thirty five. Algorithm C program --------- -------------------------------------------- 6

Experiment 3.4 Conditional expressions Ternary Operators. exp0 = exp1? exp2 : exp3 This is equivalent to.. if (Exp1) exp0 = exp2; else exp0 = exp3; Step 1: Compile and execute the program. Record the results and comment the results. int main() { } int i; printf("insert the value of i: "); scanf("%d",&i); if (i==0) printf("i=0"); else printf("other value"); return 0; - - Step 2: Rewrite the last program using Ternary operation. - - - -. 7

Post lab #3: 1- Write a concise interactive and effecient C program that calculates a worker's weekly pay determined by the number of hours worked and the hourly rate. Any hour after the first 40 are paid at 1.5 times the regular hourly rate. The user should enter the number of hours worked and the hourly rate. 2-Write a program that asks the user for three integers and then responds by printing the integers in ascending order. 3- Write a C program that asks the user for a 12-hour-format time, then displays the time in 24- hour time. Ex. if the user enters 5:10 P the program responds with: the time in 24 time is: 17:10 if the user enters 5:10 A the program responds with: the time in 24 time is: 5:10 4- Using the switch statements write a c program that convert numerical grade into a letter grade. Ex. Enter the grade in numbers: 84 Grading: B Hint: Use the following grade scale: A: 90-100, B: 80-89, C: 70-79, D: 60-69, F:0-59, otherwise print an error massage. Note: Validate user input. 5- (Bonus question) Write a C program that determines the number of digits in a number. Ex. Enter a number: 4532 The number 4532 has 4 digits. Hint.you may assume that the number no more than four digits 8