Control Statements CSI 3125 Preliminaries page 1 Control

  • Slides: 8
Download presentation
Control Statements CSI 3125, Preliminaries, page 1

Control Statements CSI 3125, Preliminaries, page 1

Control Statements • to control the flow of execution of program • control statements

Control Statements • to control the flow of execution of program • control statements can be put into the following categories: selection, iteration, and jump. • Selection statements allow program to choose different paths of execution • Iteration statements enable program execution to repeat one or more statements. • Jump statements allow program to execute in a nonlinear fashion. CSI 3125, Preliminaries, page 2

Selection Statements • • If statement if else statement nested statement if –else-if ladder

Selection Statements • • If statement if else statement nested statement if –else-if ladder CSI 3125, Preliminaries, page 3

Selection Statements • • if else statement Syntax if(condition) { true block statements; •

Selection Statements • • if else statement Syntax if(condition) { true block statements; • } • else • { false block statements; • } CSI 3125, Preliminaries, page 4

Selection Statements • • Nested if statement Syntax if(condition 1) { • } if

Selection Statements • • Nested if statement Syntax if(condition 1) { • } if (condition 2) { true block statements 2; } else { false block statements 2; } CSI 3125, Preliminaries, page 5

Selection Statements • • If-else- ladder statement Syntax if(condition 1) { true blck statements

Selection Statements • • If-else- ladder statement Syntax if(condition 1) { true blck statements 1; ; } else if(condition 2) { true block statements 2; } CSI 3125, Preliminaries, page 6

Switch Statements • • • • • Multiway branch statement. general form switch (expression)

Switch Statements • • • • • Multiway branch statement. general form switch (expression) { case value 1: // statement sequence break; case value 2: // statement sequence break; . . . case value. N: // statement sequence break; default: // default statement sequence } CSI 3125, Preliminaries, page 7

Iteration Statements • • while for do-while nested loop CSI 3125, Preliminaries, page 8

Iteration Statements • • while for do-while nested loop CSI 3125, Preliminaries, page 8