Simple%20Control%20Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Simple%20Control%20Structures

Description:

Simple Control Structures IF, IF-ELSE statements in C The if statement The if statement: if (expression){ statement; } How it works: The expression is evaluated ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 11
Provided by: wide73
Learn more at: https://cs.widener.edu
Category:

less

Transcript and Presenter's Notes

Title: Simple%20Control%20Structures


1
Simple Control Structures
  • IF, IF-ELSE statements in C

2
The if statement
  • The if statement
  • if (expression)
  • statement
  • How it works
  • The expression is evaluated
  • If the expression is TRUE the statement are
    executed and program continues to the next
    statement after IF
  • If the expression is FALSE, program continues to
    the next statement after IF

3
Flowchart for the if statement
4
The if-else statement
  • The if-else statement
  • if (expression)
  • statement1
  • else
  • statement2

5
IF-ELSE works as follows
  • Expression is evaluated
  • If the expression is TRUE, statement1 is executed
    and program continues to the next statement after
    if-else
  • If the expression is FALSE, statement2 is
    executes and program continues to the next
    statement after if-else

6
Flowchart for the if-else statement
7
SYNTAX ROOLES and INDENTATION
  • If the statement in IF statement is a single
    statement, the curly braces could be omitted
  • If the statement1 or/and statement2 is/are singe
    statements, the curly braces could be omitted
  • INDENTATION
  • The statement part of the if, if-else statement
    should be shifted

8
EXAMPLES
  • What would be the output of the following
    programming fragment?
  • int a 10
  • if (a gt 10)
  • a a 1
  • printf(d\n, a)
  • a 0
  • printf(d\n, a)

9
EXAMPLES
  • What would be the output of the following
    programming fragment?
  • int a 9
  • if (a gt 10)
  • a a 1
  • printf(d\n, a)
  • a 0
  • printf(d\n, a)

10
EXAMPLES
  • What would be the output of the following
    programming fragment?
  • int a 10
  • if (a gt 10)
  • a a 1
  • else
  • a 0
  • printf(d\n, a)
Write a Comment
User Comments (0)
About PowerShow.com