& 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.

Similar documents
Decision Making. if Statement. Decision Making

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

MODULE 2: Branching and Looping

SELECTION STATEMENTS:

Decision making with if Statement : - Control Statements. Introduction: -

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

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Decision Making -Branching. Class Incharge: S. Sasirekha

Subject: PIC Chapter 2.

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

A control structure refers to the way in which the Programmer specifies the order of executing the statements

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

ESC 101N: Fundmentals of Computing ( IInd Semester) Mid Sem II Examination PM, Monday 7th March, 2011

Decision Making and Branching

UIC. C Programming Primer. Bharathidasan University

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

Week 4 Selection Structures. UniMAP Sem II-11/12 DKT121 Basic Computer Programming 1

Structured programming

DECISION CONTROL AND LOOPING STATEMENTS

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Slides adopted from T. Ferguson Spring 2016

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

Name Roll No. Section

1. Simple if statement. 2. if else statement. 3. Nested if else statement. 4. else if ladder 1. Simple if statement

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Boolean Data Lesson #2 Outline

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

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

Chapter 13 Control Structures

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

Fundamental of Programming (C)

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

What we have learned so far

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

More examples for Control statements

Chapter 13 Control Structures

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

Unit 3 Decision making, Looping and Arrays

Chapter 6. Section 6.4 Altering the Flow of Control. CS 50 Hathairat Rattanasook

Conditional Statement

Programming Language A

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

1.4 Control Structures: Selection. Department of CSE

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Introduction to Programming

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

BRANCHING if-else statements

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

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Dept. of CSE, IIT KGP

Worksheet 4 Basic Input functions and Mathematical Operators

1. The keyword main in C language is used for

Conditional Expressions

Flow Chart. The diagrammatic representation shows a solution to a given problem.

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Selection Statements. Pseudocode

Fundamentals of Programming

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Introduction. C provides two styles of flow control:

Chapter 3: Arrays and More C Functionality

Chapter 2. Introduction to C language. Together Towards A Green Environment

C: How to Program. Week /Mar/05

Control Structures. Chapter 13 Control Structures. Example If Statements. ! Conditional. if (condition) action;

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Flow Control. CSC215 Lecture

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

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

Programming and Data Structures Mid-Semester - Solutions to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring

Programming for Electrical and Computer Engineers. Loops

EECE.2160: ECE Application Programming Spring 2018

Structured Program Development

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

C Programming Basics

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

n Group of statements that are executed repeatedly while some condition remains true

CS1100 Introduction to Programming

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Lecture 6. Statements

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

Control Flow, Functions and Basic Linkage

80 Minutes CENG 230 MidtermExam :40

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Basic Assignment and Arithmetic Operators

DECISION MAKING STATEMENTS

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

Comments. Comments: /* This is a comment */

Chapter 13. Control Structures

Time:2 Hrs.Maximum Marks: 75

Week 1 Questions Question Options Answer & Explanation A. 10 B. 20 C. 21 D. 11. A. 97 B. 98 C. 99 D. a

Question Bank (SPA SEM II)

Transcription:

statement- Computer Programming and Utilization (CPU) 110003 D) Decision Control Structure (if, if, if e if, switch, ) 1 Explain if with example and draw flowchart. if is used to control the flow of execution of statements. It is two way decision making statement. It evaluates expression first and based on its result, the control is transferred to the particular statement. The general form of simple if statement is if (testt expression) statement-block statement-x the test expression is true, the statement-block will be executed; otherwise the block will be skipped and the execution will jump to the statement-x. Flowchart: Entry Expression? Statement-block Next-statement Example: #include< <stdio.h> int no; printf("enterr the number:"); scanf("%d", &no); if(no%2==0) printf("no is even"); if(no%2!=0) printf("no is odd"); 1

2 Explain if with example and draw flowchart. supports statements only for true part supports statementss for true part and false part. The general format is given below. if(test-expression) -block statement. -block statement statement-x; test-expression is true, then true-block statement is executed otherwise false-block statement is executed. Every time, either true block or false block will be executed. In both the cases, statement-x will be executed immediately after that block. Flowchart: Entry Expression? block -block Next statement Example: Check whether the given number is even or odd. #include< <stdio.h> int no; printf("enterr the number:"); scanf("%d", &no); if(no%2==0) printf("the number is even" "); printf("the number is odd") ; 2

3 Explain if....if ladder with example and draw flowchart. When multipath decisions are involved, we may use if if ladder, which takes the following general form. if(condition-1) statement-1; if(condition-2) statement-2; if(condition-n) statement-n; default statement; statement-x First condition-1 is checked and if it is true, then statement-1 will be executed and control goes to statement-x. condition-1 control goes to statement-x. condition-2 is false, then condition-3 is checked and processs repeats is false, then condition-2 is checked and if it is true then statement-2 will be executed and This process is repeated until either it finds one of the conditions is true or all the conditions are false. If all the conditions are false, then default-statement will be executed. Else part is optional in if if ladder. Flowchart: Entry Condition 1 Statement-1 Condition 2 Statement-2 Condition 3 Statement-3 Conditionn N Statement-N Default statement 3

Example: Find the class of student based on average marks. #include< <stdio.h> int m1,m2,m3,,m4,m5; float avg; printf("enterr m1,m2,m3, m4,m5:"); scanf("%d%d%d%d%d", &m1, &m2, &m3, &m4, &m5); avg=(m1+m2+m3+m4+m5)/5; if(avg>=70) printf( Distinct ); if(avg>= =60) printf("first class"); if(avg>= =50) printf("second class"); if(avg>= =40) printf("pass class"); printf("fail"); 4 Explain nested if with example and draw flowchart. When a series of decisions are involved, we may have to use more than one if statement in nested form as shown below. if(test-condition-1) if(test-condition-2) Statement-1; e Statement-2; Statement-3; test-condition-1 is true then test-condition-2 is evaluated. If it is true then Statement-1 will be executed, if it is false then Statement-2 is false then Statement-3 will be executed. will be executed. test-condition-1 4

Flowchart: Entry Condition 1? Statement-3 Condition 2? Statement-1 Statement-2 Next statement Example: Find maximum number from given three numbers. #include< <stdio.h> int a,b,c; printf("enterr value of a, b, c:"); scanf("%d%d%d", &a, &b, &c); if(a>b) if(a>c) printf("a is max"); printf("c is max"); if(b>c) printf("b is max"); printf("c is max"); 5

5 Explain switch-case statement with example. The switch statement is a multi-way decision making. It tests whether an expression matches any one of the constant values or not. General form of switch-case is as below, switch(expression) case const-expr 1: statement 1; case const-expr 2: statement 2; case const-expr 3: statement 3; default: statements expression in switch should be integer or character expression. Float or any other data type is not allowed. Each case is labeled by one or more integer-valued constant. a case matches the expression value then execution starts at thatt case. Value of all case expressions must be different. none of the cases are matched then default case is executed. default case is optional. The break statement causess an immediate exit from the switch. Example: switch (grade) case 1: printf("fall (F)\n"); case 2: printf("bad (D)\ n"); case 3: printf("good (C)\n"); case 4: printf("very Good (B)\n"); case 5: printf("excellent (A)\n"); default: printf("you have inputted false grade\ \n"); // break isn t necessary here 6

Rules for switch statement The switch expression must be integral type. Float or other dataa types are not allowed. Case labels must be constant or constant expression. Case labels must be unique. Case labels must end with colons. The break statement is optional. The default case statement is optional. Nesting of switch statement is allowed. 6 State difference between if- and switch-case if- If statement is used to select among two alternatives. If can have values based on constraints. switch The switch statement is used to select among multiple alternatives. Switch can have values based on user choice. If implements Linear search. Switch implements Binary search. Float, double, char, int and other data types can be used in if condition. Only int and char data types can be used in switch block. Write a C Program to. 1 Find out whether the given number is odd or even. #include<stdio. h> int no; printf("enter no:") ; scanf("%d",&no); if(no%2== =0) printf("no is even"); printf("no is odd"); 2 Find maximum number from given 3 numbers. #include<stdio. h> int a,b,c; printf("enter a,b,c:"); scanf("%d%d%d",&a,& &b,&c); 7

if(a>b&&a>c) printf("a is if(b>c) printf("b is printf("c is maximum"); maximum"); maximum"); 3 which asks day number and prints corresponding day name using switch-case. #include<stdio. h> int day; printf("enter day number (1-7) \n"); scanf("%d",&day); switch(day) case 1: printf("sunday") ; case 2: printf("monday") ; case 3: printf("tuesday" ); case 4: printf("wednesday"); case 5: printf("thursday"); case 6: printf("friday") ; case 7: printf("saturday"); default: printf("wrong input"); 8